POJ 3279 Fliptile 枚举+搜索

题目链接http://poj.org/problem?id=3279

题目大意:有一个N*M的网格,有些格子是黑色的,其余的是白色的。反转一个黑格子或白格子会连同其相连(共享边)的格子一同反转。现要求全部反转成白色的,问每个格子的反转次数为多少。

解题思路:之前应该是看过题解但是没写。。。。假设我们现在已经知道第一行的反转行为,那么由于第一行已经确定,因此第一行为1的必须在第二行进行反转的时候将其反转为0,进而第二行在第三行反转,第三行在第四行反转...若将第n - 1行全部反转之后第n行仍有1,则第一行对应状态不可行;否则比较,记录最优解。按照这种方式计算:

搜索第一行的所有状态,共2m个;

对于第一行的每个状态,按照上述方式递推下面的每行的状态,然后检查最后一行是否全为0,并进行相应操作

思路清晰一点还是没太大难度的。

代码:

 1 const int maxn = 20;
 2 const int inf = 0x3f3f3f3f;
 3 int maze[maxn][maxn];
 4 int n, m;
 5 int mti, mvis[maxn][maxn];
 6 int tmti, tmvis[maxn][maxn];
 7 
 8 void fliptm(int ti, int tj, int ma[][maxn]){
 9     ma[ti][tj] = !ma[ti][tj];
10     ma[ti + 1][tj] = !ma[ti + 1][tj];
11     ma[ti][tj + 1] = !ma[ti][tj + 1];
12     if(ti > 0) ma[ti - 1][tj] = !ma[ti - 1][tj];
13     if(tj > 0) ma[ti][tj - 1] = !ma[ti][tj - 1];
14 }
15 void check(int ma[][maxn], int tvis[][maxn], int ti){
16     for(int i = 1; i < n; i++){
17         for(int j = 0; j < m; j++){
18             if(ma[i - 1][j] == 1) {
19                 tvis[i][j] = 1;
20                 fliptm(i, j, ma);
21                 ti++;
22             }
23         }
24     }
25     bool flag = true;
26     for(int i = 0; i < m; i++) if(ma[n - 1][i] == 1) flag = false;
27     if(flag && ti < mti){
28         mti = ti;
29         memcpy(mvis, tvis, sizeof(mvis));
30     }
31     return;
32 }
33 void dfs(int st){
34     if(st == m) return;
35     int tma[maxn][maxn], tvis[maxn][maxn];
36     fliptm(0, st, maze);
37     tmti++; tmvis[0][st] = 1;
38     memcpy(tma, maze, sizeof(tma));
39     memcpy(tvis, tmvis, sizeof(tvis));
40     check(tma, tvis, tmti);
41     dfs(st + 1);
42     
43     fliptm(0, st, maze);
44     tmti--; tmvis[0][st] = 0;
45     memcpy(tma, maze, sizeof(tma));
46     memcpy(tvis, tmvis, sizeof(tvis));
47     check(tma, tvis, tmti);
48     dfs(st + 1);
49         
50 }
51 int main(){
52     scanf("%d %d", &n, &m);
53     for(int i = 0; i < n; i++){
54         for(int j = 0; j < m; j++){
55             scanf("%d", &maze[i][j]);
56         }
57     }
58     mti = inf; tmti = 0;
59     memset(tmvis, 0, sizeof(tmvis));
60     memset(mvis, 0, sizeof(mvis));
61     dfs(0);
62     if(mti >= inf) puts("IMPOSSIBLE");
63     else{
64         for(int i = 0; i < n; i++){
65             for(int j = 0; j < m; j++){
66                 printf("%d ", mvis[i][j]);
67             }
68             puts("");
69         }
70     }
71 }

题目:

Fliptile
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 11402   Accepted: 4209

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

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

Source

posted @ 2017-08-10 20:25  EricJeffrey  阅读(128)  评论(0编辑  收藏  举报