Description
Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an M × N grid (1 ≤ M ≤ 15; 1 ≤ N ≤ 15) of square tiles, each of which is colored black on one side and white on the other side.
As one would guess, when a single white tile is flipped, it changes to black; when a single black tile is flipped, it changes to white. The cows are rewarded when they flip the tiles so that each tile has the white side face up. However, the cows have rather large hooves and when they try to flip a certain tile, they also flip all the adjacent tiles (tiles that share a full edge with the flipped tile). Since the flips are tiring, the cows want to minimize the number of flips they have to make.
Help the cows determine the minimum number of flips required, and the locations to flip to achieve that minimum. If there are multiple ways to achieve the task with the minimum amount of flips, return the one with the least lexicographical ordering in the output when considered as a string. If the task is impossible, print one line with the word "IMPOSSIBLE".
Input
Output
Sample Input
4 4 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1
Sample Output
0 0 0 0 1 0 0 1 1 0 0 1 0 0 0 0
简单搜索,直接上代码,不过话说这道题和山东省的省赛的题目确实是很像,都是第一行确定后剩下的行全部就确定了,所以总的来说还是只要确定第一行就行
甚至可以是第一行直接二进制分解出第一行就行了。。。
#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<queue> #include<stack> #include<string> #include<map> #include<set> #include<ctime> #define eps 1e-6 #define MAX 100005 #define INF 0x3f3f3f3f #define LL long long #define pii pair<int,int> #define rd(x) scanf("%d",&x) #define rd2(x,y) scanf("%d%d",&x,&y) #define rd3(x,y,z) scanf("%d%d%d",&x,&y,&z) ///map<int,int>mmap; ///map<int,int >::iterator it; using namespace std; int dir[][2]={{-1,0},{1,0},{0,-1},{0,1},{0,0}}; int mmap[20][20]; int res[20][20]; int tmpres[20][20]; int M,N; int step; bool judge(){ for(int i=0;i<N;i++) if(mmap[M-1][i]!=0) return false; return true; } void update(int x,int y ){ int tmpx,tmpy; for(int i=0;i<5;i++){ tmpx = x+dir[i][0],tmpy=y+dir[i][1]; if(tmpx<0 ||tmpx>=M || tmpy<0 || tmpy>=N) continue; else mmap[tmpx][tmpy]=!mmap[tmpx][tmpy]; } } void dfs(int x,int y,int sstep){ // cout<<x<<' '<<y<<' '<<sstep<<endl; if(sstep>=step) return; if(y>=N) x++,y=0; if(x>=M){ if(judge()){ for(int i=0;i<M;i++) for(int j=0;j<N;j++) res[i][j]=tmpres[i][j]; step=sstep; } return ; } if(x==0){ tmpres[x][y]=0; dfs(x,y+1,sstep); update(x,y); tmpres[x][y]=1; dfs(x,y+1,sstep+1); update(x,y); tmpres[x][y]=0; } else{ if(mmap[x-1][y]==1){ tmpres[x][y]=1; update(x,y); dfs(x,y+1,sstep+1); update(x,y); tmpres[x][y]=0; } else dfs(x,y+1,sstep); } } int main() { while(~rd2(M,N)) { for(int i = 0; i < M; ++i)//数据输入 for(int j = 0; j < N; ++j) rd(mmap[i][j]); step=INF; memset(res,0,sizeof(res)); memset(tmpres,0,sizeof(tmpres)); dfs(0,0,0); if(step==INF) printf("IMPOSSIBLE\n"); else{ for(int i=0;i<M;i++){ for(int j=0;j<N;j++) printf("%d ",res[i][j]); printf("\n"); } } } return 0; }