Problem Description
Flood-it is a fascinating puzzle game on Google+ platform. The game interface is like follows:
At the beginning of the game, system will randomly generate an N×N square board and each grid of the board is painted by one of the six colors. The player starts from the top left corner. At each step, he/she selects a color and changes all the grids connected with the top left corner to that specific color. The statement “two grids are connected” means that there is a path between the certain two grids under condition that each pair of adjacent grids on this path is in the same color and shares an edge. In this way the player can flood areas of the board from the starting grid (top left corner) until all of the grids are in same color. The following figure shows the earliest steps of a 4×4 game (colors are labeled in 0 to 5):
Given a colored board at very beginning, please find the minimal number of steps to win the game (to change all the grids into a same color).
![](https://i-blog.csdnimg.cn/blog_migrate/db0b1c126ff90b129d6745ab34cb5b14.jpeg)
At the beginning of the game, system will randomly generate an N×N square board and each grid of the board is painted by one of the six colors. The player starts from the top left corner. At each step, he/she selects a color and changes all the grids connected with the top left corner to that specific color. The statement “two grids are connected” means that there is a path between the certain two grids under condition that each pair of adjacent grids on this path is in the same color and shares an edge. In this way the player can flood areas of the board from the starting grid (top left corner) until all of the grids are in same color. The following figure shows the earliest steps of a 4×4 game (colors are labeled in 0 to 5):
![](https://i-blog.csdnimg.cn/blog_migrate/dd2ca6ceb6107bfe8f72945ee28c2c7c.jpeg)
Given a colored board at very beginning, please find the minimal number of steps to win the game (to change all the grids into a same color).
Input
The input contains no more than 20 test cases. For each test case, the first line contains a single integer N (2<=N<=8) indicating the size of game board.
The following N lines show an N×N matrix (a i,j) n×n representing the game board. a i,j is in the range of 0 to 5 representing the color of the corresponding grid.
The input ends with N = 0.
The following N lines show an N×N matrix (a i,j) n×n representing the game board. a i,j is in the range of 0 to 5 representing the color of the corresponding grid.
The input ends with N = 0.
Output
For each test case, output a single integer representing the minimal number of steps to win the game.
Sample Input
2 0 0 0 0 3 0 1 2 1 1 2 2 2 1 0
Sample Output
0 3
IDA*搜索
#include<queue> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define rep(i,j,k) for (int i = j; i <= k; i++) const int N = 8; int n,step; int a[4]={0,0,1,-1}; int b[4]={1,-1,0,0}; struct maze { int d[N][N],c[6]; int t[N*N],cnt; void read() { rep(i,0,n-1) rep(j,0,n-1) scanf("%d",&d[i][j]); } int pre() { int res=0; rep(i,0,5) c[i]=0; rep(i,0,n-1) rep(j,0,n-1) c[d[i][j]]=1; rep(i,0,5) res+=c[i]; return res; } void get() { rep(i,0,5) c[i]=0; int org=d[0][0]; d[0][0]=-1; for (int q=cnt=t[0]=0;q<=cnt;q++) { rep(i,0,3) { int x=t[q]/n+a[i],y=t[q]%n+b[i]; if (x<0||x==n||y<0||y==n||d[x][y]==-1) continue; if (d[x][y]!=org) {c[d[x][y]]=1; continue;} t[++cnt]=x*n+y; d[x][y]=-1; } } } }x; bool dfs(maze x,int y) { int pre=x.pre()-1; if (!pre) return true; if (y+pre>step) return false; x.get(); rep(i,0,5) { if (!x.c[i]) continue; rep(j,0,x.cnt) { int X=x.t[j]/n,Y=x.t[j]%n; x.d[X][Y]=i; } if (dfs(x,y+1)) return true; } return false; } int main() { while (~scanf("%d", &n),n) { x.read(); for (step=0;!dfs(x,0);step++); printf("%d\n",step); } return 0; }