http://poj.org/problem?id=1222
题意:5*6矩阵中有30个灯,操作一个灯,周围的上下左右四个灯会发生相应变化 即由灭变亮,由亮变灭,如何操
作使灯全灭?
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7800 | Accepted: 5075 |
Description
Input
Output
Sample Input
2 0 1 1 0 1 0 1 0 0 1 1 1 0 0 1 0 0 1 1 0 0 1 0 1 0 1 1 1 0 0 0 0 1 0 1 0 1 0 1 0 1 1 0 0 1 0 1 1 1 0 1 1 0 0 0 1 0 1 0 0
Sample Output
PUZZLE #1 1 0 1 0 0 1 1 1 0 1 0 1 0 0 1 0 1 1 1 0 0 1 0 0 0 1 0 0 0 0 PUZZLE #2 1 0 0 1 1 1 1 1 0 0 0 0 0 0 0 1 0 0 1 1 0 1 0 1 1 0 1 1 0 1方法一;;
#include <iostream> #include <stdio.h> #include <algorithm> #include <string.h> using namespace std; const int dx[5]= {-1,0,0,0,1}; const int dy[5]= {0,-1,0,1,0}; int tile[10][10]; int opt[10][10]; int flip[10][10]; int get(int x,int y) { int c=tile[x][y]; for(int d=0; d<5; d++) { int x2=x+dx[d],y2=dy[d]+y; if(0<=x2&&x2<5&&y2>=0&&y2<6) { c+=flip[x2][y2]; } } return c%2; } int calc () { for(int i=1; i<5; i++) { for(int j=0; j<6; j++) { if(get(i-1,j)!=0) { flip[i][j]=1; } } } for(int j=0; j<6; j++) if(get(4,j)!=0) return -1; int res=0; for(int i=0; i<5; i++) { for(int j=0; j<6; j++) { res+=flip[i][j]; } } return res; } void solve() { int res=-1; for(int i=0; i<1<<6; i++) { memset(flip,0,sizeof(flip)); for(int j=0; j<6; j++) { flip[0][j]=i>>j&1; } int num=calc(); if(num>=0&&(res<0||res>num)) { res=num; memcpy(opt,flip,sizeof(flip)); } } if(res<0) { printf("01\n"); } else for(int i=0; i<5; i++) { for(int j=0; j<6; j++) { printf("%d%c",opt[i][j],j+1==6?'\n':' '); } } } int main() { int t,T=1; cin>>t; while(t--) { for(int i=0; i<5; i++) for(int j=0; j<6; j++) cin>>tile[i][j]; cout<<"PUZZLE #"<<T++<<endl; solve(); } return 0; }
#include <iostream> #include <string.h> #include <algorithm> #include <stdio.h> #include <math.h> using namespace std; const int N = 35; int gcd(int a,int b) { return b ? gcd(b,a%b):a; } int lcm(int a,int b) { return a / gcd(a,b) * b; } void Gauss(int a[][N],int n,int m,int &r,int &c) { r = c = 0; for(; r<n && c<m; r++,c++) { int maxi = r; for(int i=r+1; i<n; i++) if(abs(a[i][c]) > abs(a[maxi][c])) maxi = i; if(maxi != r) { for(int i=r; i<m+1; i++) swap(a[r][i],a[maxi][i]); } if(a[r][c] == 0) { r--; continue; } for(int i=r+1; i<n; i++) { if(a[i][c] != 0) { int x = abs(a[i][c]); int y = abs(a[r][c]); int LCM = lcm(x,y); int tx = LCM / x; int ty = LCM / y; if(a[i][c] * a[r][c] < 0) ty = -ty; for(int j=c; j<m+1; j++) a[i][j] = ((a[i][j] % 2 * tx % 2 - a[r][j] % 2 * ty % 2) % 2 + 2) % 2; } } } } int Rewind(int a[][N],int x[],int r,int c) { for(int i=r-1; i>=0; i--) { int t = a[i][c] % 2; for(int j=i+1; j<c; j++) { if(a[i][j] != 0) t -= a[i][j] % 2 * x[j] % 2; } x[i] = t / a[i][i] % 2; x[i] = (x[i] + 2) % 2; } return 0; } int a[N][N]; int x[N]; int main() { int cas = 1; int n,m,T; scanf("%d",&T); while(T--) { n = m = 30; memset(a,0,sizeof(a)); for(int i=0; i<5; i++) { for(int j=0; j<6; j++) { if(i >= 1) a[6*i+j][6*(i-1)+j] = 1; if(i <= 3) a[6*i+j][6*(i+1)+j] = 1; if(j >= 1) a[6*i+j][6*i+j-1] = 1; if(j <= 4) a[6*i+j][6*i+j+1] = 1; a[6*i+j][6*i+j] = 1; scanf("%d",&a[6*i+j][30]); } } int r,c; int cnt = 0; Gauss(a,n,m,r,c); Rewind(a,x,r,c); printf("PUZZLE #%d\n",cas++); for(int i=0; i<30; i++) { cnt++; if(cnt % 6) printf("%d ",x[i]); else printf("%d\n",x[i]); } } return 0; }