原题POJ1753
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 36003 | Accepted: 15706 |
Description
- Choose any one of the 16 pieces.
- Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).
Consider the following position as an example:
bwbw
wwww
bbwb
bwwb
Here “b” denotes pieces lying their black side up and “w” denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:
bwbw
bwww
wwwb
wwwb
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.
Input
Output
Sample Input
1 2 3 4 |
bwwb bbwb bwwb bwww |
Sample Output
1 |
4 |
Source
这一道题最关键的部分就是运用了DFS算法(Depth-First Search, 深度优先搜索算法)
首先来看其它几个比较简单的函数
初始化函数,输入棋盘
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
void build()//tag color of chess { char c; int i, j; for(i = 0; i < col; i++) for( j = 0; j < col; j++) { cin >> c; if(c == 'w') chess[i][j] = 0; else chess[i][j] = 1; } } |
赋反值函数:
1 2 3 4 5 |
void turn(int i, int j) { if(i >= 0 && i <= col-1 && j >= 0 && j <= col-1) chess[i][j] = !chess[i][j]; } |
翻转函数:
1 2 3 4 5 6 7 8 9 |
void flip(int s)//flip the box { int i = s/col, j = s%col; //rownum, colnum turn(i, j); turn(i+1, j); turn(i-1, j); turn(i, j+1); turn(i, j-1); } |
检测函数:
1 2 3 4 5 6 7 8 9 10 11 12 |
int complete()//check if the color is all same { int sum = 0; int i, j; for(i = 0; i < col; i++) for(j = 0; j < col; j++) sum += chess[i][j]; if(sum % (col*col)) return 0; else return 1; } |
上面的函数都比较简单,几乎不用什么脑子想,最关键的就是dfs函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
void dfs(int id, int num) //n is current box, num is times of flipping { if(complete()) { if(c > num) c = num; return ; } if(id >= col*col) return ; dfs(id+1, num); flip(id); dfs(id+1, num+1); flip(id); } |
虽然数据结构早就学了DFS,但是一直都不是特别理解,听了前辈的建议,把算法用笔在纸上模拟了一遍果然就明了了。 DFS算法其实就是先到达最深处的点,然后一步一步往回走,每一步再往下一步一步看,在这道题里面首先到最远的id为16的点,然后尝试所有的的翻转可能,每到一个点向下就有2^(17-id)种可能, 然后遍历一遍。 我后来又把程序改了一下,把每一次翻转都打印一遍,这样就更清晰更好理解了,只用把flip函数修改一下即可。 PS: 这里最好把col改小一点好模拟一些, col等于4的时候翻转了13+w次, 改成2就可以了, 只有28次。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
void flip(int s)//flip the box { /*添加的打印代码*/ flag++; //每一次翻转的编号 cout << flag << 't' << s << 't' << i << 't' << j << 't' << chess[i][j] << endl;//输出 翻转编号 元素id 行数 列数 标记值 /*添加的打印代码*/ int i = s/col, j = s%col; //rownum, colnum turn(i, j); turn(i+1, j); turn(i-1, j); turn(i, j+1); turn(i, j-1); } |
完整代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
#include <stdio.h> #include <iostream> #define col 4 using namespace std; int c = 2*col*col + 1; int chess[col][col]; void build()//tag color of chess { char c; int i, j; for(i = 0; i < col; i++) for( j = 0; j < col; j++) { cin >> c; if(c == 'w') chess[i][j] = 0; else chess[i][j] = 1; } } void turn(int i, int j) { if(i >= 0 && i <= col-1 && j >= 0 && j <= col-1) chess[i][j] = !chess[i][j]; } void flip(int s)//flip the box { int i = s/col, j = s%col; //rownum, colnum turn(i, j); turn(i+1, j); turn(i-1, j); turn(i, j+1); turn(i, j-1); } int complete()//check if the color is all same { int sum = 0; int i, j; for(i = 0; i < col; i++) for(j = 0; j < col; j++) sum += chess[i][j]; if(sum % (col*col)) return 0; else return 1; } void dfs(int id, int num) //n is current box, num is times of flipping { if(complete()) { if(c > num) c = num; return ; } if(id >= col*col) return ; dfs(id+1, num); flip(id); dfs(id+1, num+1); flip(id); } int main() { build(); dfs(0, 0); if( c == 2*col*col+1) printf("Impossible\n"); //32 most else printf("%d\n", c); return 0; } |