[kuangbin带你飞]专题一 简单搜索 - D - Fliptile
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int x, y; 7 bool g[20][20]; 8 bool k[20][20]; 9 bool ans[20][20]; 10 bool flag; 11 int d[5][2] = {{0,-1},{0,1},{-1,0},{1,0},{0,0}}; 12 bool judge(int cur) 13 { 14 for(int i = 1; i <= x; i++) 15 for(int j = 1; j <= y; j++) 16 { 17 k[i][j] = g[i][j]; 18 } 19 for(int j = 0; j < y; j++) 20 { 21 k[0][j+1] = (cur>>j)%2; 22 } 23 24 for(int i = 1; i <= x; i++) 25 for(int j = 1; j <= y; j++) 26 { 27 if(k[i-1][j] == false) continue; 28 ans[i][j] = true; 29 for(int z = 0; z <= 4; z++) 30 { 31 k[i+d[z][0]][j+d[z][1]] = !k[i+d[z][0]][j+d[z][1]]; 32 } 33 } 34 35 for(int j = 1; j <= y; j++) 36 { 37 if(k[x][j]) return false; 38 } 39 return true; 40 } 41 int main() 42 { 43 // freopen("in.in","r",stdin); 44 while(scanf("%d%d",&x,&y)!=EOF) 45 { 46 flag = false; 47 for(int i = 1; i <= x; i++) 48 for(int j = 1; j <= y; j++) 49 { 50 scanf("%d",&g[i][j]); 51 } 52 for(int i = 0; i < (1<<y); i++) 53 { 54 memset(ans,0,sizeof(ans)); 55 56 if(judge(i)) 57 { 58 flag = true; 59 break; 60 } 61 62 } 63 if(flag) 64 { 65 for(int i = 1; i <= x; i++) 66 { 67 cout<<ans[i][1]; 68 for(int j = 2; j <= y; j++) 69 { 70 cout<<" "<<ans[i][j]; 71 } 72 printf("\n"); 73 } 74 } 75 else printf("IMPOSSIBLE\n"); 76 } 77 78 79 return 0; 80 }
本文为博主原创文章,未经博主允许不得转载。