标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 40632 | Accepted: 17647 |
Description
Input
Output
Sample Input
bwwb bbwb bwwb bwww
Sample Output
4
Source
题目地址:http://poj.org/problem?id=1753
#include<stdio.h> #include<string.h> #include<iostream> #include<stdlib.h> using namespace std; #define N 7 bool mat[N][N], flag; int deep; int dx[] = {1, -1, 0, 0, 0}; int dy[] = {0, 0, 1, -1, 0}; void Init() { char c; for(int i = 1; i <= 4; i++) { for(int j = 1; j <= 4; j++) { scanf(" %c",&c); if(c == ‘b‘) { mat[i][j] = 1; } else { mat[i][j] = 0; } } } } void Print() { for(int i = 1; i <= 4; i++) { for(int j = 1; j <= 4; j++) printf("%d", mat[i][j]); printf("\n"); } } void Change(int x, int y) { int next_x, next_y; for(int k = 0; k < 5; k++) { next_x = x + dx[k]; next_y = y + dy[k]; //if(next_x >= 1 && next_x <=4 && next_y >= 1 && next_y <=4) //{ mat[next_x][next_y] = !mat[next_x][next_y]; //} } } void Flip(int x) { switch(x) //1~16种case代表4*4的16个格子 { case 1: Change(1,1); break; case 2: Change(1,2); break; case 3: Change(1,3); break; case 4: Change(1,4); break; case 5: Change(2,1); break; case 6: Change(2,2); break; case 7: Change(2,3); break; case 8: Change(2,4); break; case 9: Change(3,1); break; case 10: Change(3,2); break; case 11: Change(3,3); break; case 12: Change(3,4); break; case 13: Change(4,1); break; case 14: Change(4,2); break; case 15: Change(4,3); break; case 16: Change(4,4); break; } } bool Result() { for(int i = 1; i <= 4; i++) { for(int j = 1; j <= 4; j++) if(mat[i][j] != mat[1][1]) return false; } return true; } void Dfs(int x, int dp) { if(flag || dp > deep || x>16) return; //printf("DFS(%d, %d)\n", x, dp); Flip(x); //Print();printf("---------\n"); if(dp == deep) { flag = (flag || Result()); if(flag) { //printf("OK!!!!!!!!!!!!!!"); //exit(0); goto A; } } Dfs(x+1, dp+1); Flip(x); //Print();printf("---------\n"); Dfs(x+1, dp); A: return; } int main() { //每个棋子最多翻一次(其实是奇数次,但是没意义),翻偶数次和没翻一样,所以每个棋子就两种状态,翻或者不翻 //所以一共就有2^16次方种可能,枚举这些可能就行了,DFS //从0到16,如果16还找不到那就是Impossible int a, b; Init(); //Print(); flag = false; if(Result()) { cout<<0<<endl; return 0; } for(deep = 1; deep <= 16; deep++) { //cout<<"deep = "<<deep<<endl; Dfs(1, 1); if(flag) break; } if(flag) cout<<deep<<endl; else cout<<"Impossible"<<endl; // while(cin>>a) // { // Flip(a); // Print(); // } return 0; } /* bwwb bbwb bwwb bwww wwww wwww wwww wwww bbbw wbww wwww wwww wwww wwww wwwb wwbb bbww bwww wwww wwww wwbw bbww wwww wwww */
标签:
原文地址:http://www.cnblogs.com/wmxl/p/5860936.html