(POJ-3279) Fliptile (dfs classic---can also enumerate)

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 



Well, I think this question is very good. It is a classic question type for getting started with dfs. The key is that flipping once will affect the surrounding state. In addition to using dfs, you can also use enumeration (switch problem),
here I Only dfs is used (enums don't orz yet).
If dfs is searched, it must not be searched by one path. We can search all the flips of the first row of dfs, and then from the first row, we can deduce the state of the
following row, as long as the row corresponding to the upper row has black in the lower row. This position is flipped because it affects the surrounding, and all the upper rows are also flipped.
As for the lexicographical order, because the first line of dfs is searched directly to the end, the subsequent dfs situations are flipped from the rightmost to the left. If there is the same minimum value, then the record must be the rightmost Case.
Note the backtracking of the path
  1 #include<bits/stdc++.h>
  2 
  3 using namespace std;
  4 
  5 int m,n;
  6 int ans;
  7 
  8 int maps[16][16];
  9 int vis[16][16];
 10 int vistmp[16][16];
 11 int way[5][2] = {0,0,1,0,0,1,0,-1,-1,0};
 12 void inif()
 13 {
 14     memset(vis,0,sizeof(vis));
 15     memset(vistmp,0,sizeof(vistmp));
 16     for(int i=1;i<=m;i++)
 17     {
 18         for(int j=1;j<=n;j++)
 19         {
 20             scanf("%d",&maps[i][j]);
 21         }
 22     }
 23 }
 24 void flip(int x,int y)
 25 {
 26     vistmp[x][y] ^= 1;
 27     for(int i=0;i<5;i++)
 28     {
 29         int xx = x+way[i][0];
 30         int yy = y+way[i][1];
 31         if(0<xx && xx<=m && yy>0 && yy <=n)
 32         {
 33             maps[xx][yy] ^= 1;
 34         }
 35     }
 36 }
 37 void solve(int k,int cnt)
 38 {
 39     if(cnt >= ans)return;
 40     if(k == m)
 41     {
 42         int i;
 43         for(i=1;i<=n;i++)
 44             if(maps[m][i])break;
 45             if(i <= n)return;
 46             ans = cnt;
 47             memcpy(vis,vistmp,sizeof(vistmp));
 48             return;
 49     }
 50     for(int i=1;i<=n;i++)
 51     {
 52         if(maps[k][i])
 53         {
 54             flip(k+1,i);
 55             cnt++;
 56         }
 57     }
 58     solve(k+1,cnt);
 59     for(int i=1;i<=n;i++)
 60     {
 61         if(vistmp[k+1][i])flip(k+1,i);
 62     }
 63     return;
 64 }
 65 void dfs(int k,int cnt)
 66 {
 67     if(cnt >= ans)return;
 68     else if(k == n+1)
 69     {
 70         solve(1,cnt);
 71         return;
 72     }
 73     else
 74     {
 75     dfs(k+1,cnt);
 76     flip(1,k);
 77     dfs(k+1,cnt+1);
 78     flip(1,k);
 79     }
 80 }
 81 int main()
 82 {
 83     while(~scanf("%d%d",&m,&n))
 84     {
 85             ans = 0x3f3f3f3f;
 86             inif();
 87             dfs(1,0);
 88             if(ans == 0x3f3f3f3f)printf("IMPOSSIBLE\n");
 89             else
 90             {
 91                 for(int i=1;i<=m;i++)
 92                 {
 93                     for(int j=1;j<=n;j++)
 94                     {
 95                         if(j != 1)
 96                         printf(" %d",vis[i][j]);
 97                         else
 98                         printf("%d",vis[i][j]);
 99                     }
100                     puts("");
101                 }
102             }
103     }
104 }
View Code

 




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325062302&siteId=291194637