码迷,mamicode.com
首页 > 其他好文 > 详细

(POJ-3279)Fliptile (dfs经典---也可以枚举)

时间:2018-04-30 01:03:46      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:close   task   ring   分享图片   lan   esc   scan   row   最小   

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以外,也可以用枚举(开关问题),
这里我只使用了dfs(枚举还不会orz)。
dfs搜索的话,肯定不是按一条一条路径来搜,我们可以dfs第一行的所有翻转情况,然后从第一行的情况可以递推出下面行的状态,
下面行中只要上行对应列有黑色就把这个位置翻转,因为会影响周围,所有上面行也会翻转。
至于字典序,因为一开始dfs第一行的时候是直接搜到底的,所以后面的dfs情况都是从最右边开始向左翻转的,如果有相同的最小值,那么记录下的一定是最右边的情况。
注意路径的回溯
技术分享图片
  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

 




(POJ-3279)Fliptile (dfs经典---也可以枚举)

标签:close   task   ring   分享图片   lan   esc   scan   row   最小   

原文地址:https://www.cnblogs.com/iwannabe/p/8972471.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!