标签:
题目链接:http://poj.org/problem?id=1753
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 36419 | Accepted: 15866 |
Description
Input
Output
Sample Input
bwwb bbwb bwwb bwww
Sample Output
4
Source
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using namespace std; int a[6][6]; char str[10][10]; int b[5][2]= {1,0,0,1,-1,0,0,-1,0,0}; int Min; int pai(int ans) { int t=a[1][1]; int flag = 0; for(int i=1; i<=4; i++) { for(int j=1; j<=4; j++) { if(a[i][j]!=t) { flag = 1; i = 5; break; } } } if(flag == 0) { if(ans < Min) Min = ans; return 1; } return 0; } void DFS(int x, int y, int ans) { if(y>4) { x+=1; y-=4; } if(x>4||y>4) { int f = pai(ans); return ; } DFS(x, y+1, ans); for(int k=0; k<5; k++) { a[x+b[k][0]][y+b[k][1]]^=1; } DFS(x, y+1, ans+1); for(int k=0; k<5; k++) { a[x+b[k][0]][y+b[k][1]]^=1; } } int main() { char str[10][10]; memset(a,-1,sizeof(a)); for(int i=1; i<=4; i++) { scanf("%s", str[i]+1); } for(int i=1; i<=4; i++) { for(int j=1; j<=4; j++) { if(str[i][j] == ‘b‘) a[i][j] = 1; else a[i][j] = 0; } } Min = 20; if(pai(0)) Min = 0; else DFS(1,1,0); if(Min==20) printf("Impossible\n"); else printf("%d\n",Min); return 0; }
标签:
原文地址:http://www.cnblogs.com/mengzhong/p/5036390.html