标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 35877 | Accepted: 15658 |
Description
Input
Output
Sample Input
bwwb bbwb bwwb bwww
Sample Output
4
Source
#include <stdio.h> const int inf=9999999; char s[10]; int map[10][10],i,j; int ans=9999999; int panduan() { int x=map[0][0]; for (i=0; i<4; i++) { for (j=0; j<4; j++) { if (map[i][j]!=x) return 0; } } return 1; } void fan (int x,int y) { map[x][y]=!map[x][y]; if (x - 1 >= 0) map[x-1][y]=!map[x-1][y]; if (x + 1 < 4) map[x+1][y]=!map[x+1][y]; if (y - 1 >= 0) map[x][y-1]=!map[x][y-1]; if (y + 1 < 4) map[x][y+1]=!map[x][y+1]; } int dfs (int x,int y,int t) { int nx,ny; if ( panduan()==1) { if (ans > t) ans = t ; return 0; } if (x >= 4 || y >= 4) return 0; nx = (x + 1)%4; ny = y + ( x + 1 ) / 4; dfs (nx,ny,t);//两次dfs不知道啥意思。。。 fan (x,y); dfs (nx,ny,t+1); fan (x,y); return 0; } int main () { for (i=0; i<4; i++) { scanf ("%s",s); for (j=0; j<4; j++) { if (s[j]==‘b‘) map[i][j]=0; else map[i][j]=1; } } dfs (0,0,0); if (ans == inf ) printf ("Impossible\n"); else printf ("%d\n",ans); return 0; }
POJ1753【简单DFS算法】--翻棋盘(一步看不懂!!)
标签:
原文地址:http://www.cnblogs.com/zhangfengnick/p/4907662.html