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
Line 1: Two space-separated integers: M and N
Lines 2.. M+1: Line i+1 describes the colors (left to right) of row i of the grid with N space-separated integers which are 1 for black and 0 for white
Output
Lines 1.. M: Each line contains N space-separated integers, each specifying how many times to flip that particular location.
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
英文的意思:自行百度在线翻译。
题解:因为直接dfs会超时,所以我们dfs第一行的所有情况,因为如果第一行确定了,那么下边的每一行也能确定,因为题目要求翻得最少,翻转的位置要最少,其实翻转的个数最少就能确定。
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int h[20][20],i[20][20],j[20][20],k[20][20],flag,tt,gg;
char xx[400],zz[400];
void dffs(int n,int end,int st)
{
int a,b,c,d,e,g;
if(n==end+1)
{
for(a=1;a<=st;a++)
{
for(b=1;b<=end;b++)
{
i[a][b]=h[a][b];
}
}
e=-1;
for(b=1;b<=end;b++)
{
xx[++e]='0'+j[1][b];
}
for(a=2;a<=st;a++)
{
for(b=1;b<=end;b++)
{
if(i[a-1][b]==1)
{
j[a][b]=1;
xx[++e]='0'+j[a][b];
i[a-1][b]=0;
i[a][b]=abs(i[a][b]-1);
if(b!=1)
{
i[a][b-1]=abs(i[a][b-1]-1);
}
if(b!=end)
{
i[a][b+1]=abs(i[a][b+1]-1);
}
if(a!=st)
i[a+1][b]=abs(i[a+1][b]-1);
j[a][b]=0;
}
else xx[++e]='0';
}
}
c=0;
g=0;
for(d=1;d<=end;d++)
{
if(i[st][d]==1)
c++;
}
if(c==0)
{
tt++;
flag++;
for(a=0;a<e;a++)
{
if(xx[a]=='1')
g++;
}
if(flag==0)
{
strcpy(zz,xx);
gg=g;
}
else if(flag!=0)
{
if(gg>g)
{
gg=g;
strcpy(zz,xx);
}
else if(gg==g)
{
if(strcmp(zz,xx)>0)
{
strcpy(zz,xx);
}
}
}
}
return ;
}
h[1][n]=abs(h[1][n]-1);
j[1][n]=1;
if(n!=1)
{
h[1][n-1]=abs(h[1][n-1]-1);
}
if(n!=end)
{
h[1][n+1]=abs(h[1][n+1]-1);
}
h[2][n]=abs(h[2][n]-1);
dffs(n+1,end,st);
h[1][n]=abs(h[1][n]-1);
j[1][n]=0;
if(n!=1)
{
h[1][n-1]=abs(h[1][n-1]-1);
}
if(n!=end)
{
h[1][n+1]=abs(h[1][n+1]-1);
}
h[2][n]=abs(h[2][n]-1);
dffs(n+1,end,st);
}
int main()
{
int a,b,c,d,e;
scanf("%d %d",&a,&b);
for(c=1;c<=a;c++)
{
for(d=1;d<=b;d++)
{
scanf("%d",&h[c][d]);
i[c][d]=h[c][d];
k[c][d]=h[c][d];
}
}
flag=-1;
tt=0;
memset(j,0,sizeof(j));
dffs(1,b,a);
e=-1;
if(tt!=0)
{for(c=1;c<=a;c++)
{
for(d=1;d<=b;d++)
{
if(d==b)
printf("%c\n",zz[++e]);
else printf("%c ",zz[++e]);
}
}
}
else printf("IMPOSSIBLE\n");
return 0;
}