Flood-it!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1965 Accepted Submission(s): 482
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).
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).
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 (ai,j)n×n representing the game board. ai,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 (ai,j)n×n representing the game board. ai,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
1 //2016.8.27 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 6 using namespace std; 7 8 //vis[i][j]为1表示格子与左上角连通,为2表示与连通块相邻的格子,0为其他 9 int a[10][10], vis[10][10], n, deep; 10 int dx[4] = {0, 1, 0, -1}; 11 int dy[4] = {1, 0, -1, 0}; 12 bool ok; 13 14 int Astar()//估价函数,计算除左上角连通块之外还有多少颜色,即最少要染的次数 15 { 16 int book[10], h = 0; 17 memset(book, 0, sizeof(book)); 18 for(int i = 1; i <= n; i++) 19 for(int j = 1; j <= n; j++) 20 if(vis[i][j]!=1 && !book[a[i][j]]) 21 book[a[i][j]]++, h++; 22 return h; 23 } 24 25 void dfs(int x, int y, int color)//把颜色为color的格子并入左上角连通块 26 { 27 vis[x][y] = 1; 28 for(int i = 0; i < 4; i++) 29 { 30 int nx = x+dx[i]; 31 int ny = y+dy[i]; 32 if(nx>=1&&nx<=n&&ny>=1&&ny<=n) 33 { 34 if(vis[nx][ny] == 1)continue; 35 vis[nx][ny] = 2; 36 if(a[nx][ny] == color) 37 dfs(nx, ny, color); 38 } 39 } 40 } 41 42 int fill(int color)//把vis[i][j]==2的格子染成color色 43 { 44 int cnt = 0; 45 for(int i = 1; i <= n; i++) 46 for(int j = 1; j <= n; j++) 47 if(vis[i][j]==2&&a[i][j]==color) 48 { 49 cnt++; 50 dfs(i, j, color); 51 } 52 return cnt; 53 } 54 55 void IDAstar(int step) 56 { 57 if(ok)return ; 58 int h = Astar(); 59 if(h == 0) 60 { 61 cout<<step<<endl; 62 ok = true; 63 return ; 64 } 65 if(step+h>deep)return ; 66 int tmp[10][10]; 67 memcpy(tmp, vis, sizeof(tmp)); 68 for(int i = 0; i < 6; i++)//进行染色 69 { 70 if(fill(i)==0)continue; 71 IDAstar(step+1); 72 memcpy(vis, tmp, sizeof(vis)); 73 } 74 } 75 76 int main() 77 {
79 while(scanf("%d", &n)!=EOF && n) 80 { 81 for(int i = 1; i <= n; i++) 82 for(int j = 1; j <= n; j++) 83 scanf("%d", &a[i][j]); 84 ok = false; 85 deep = 0; 86 memset(vis, 0, sizeof(vis)); 87 dfs(1, 1, a[1][1]);//对左上角预处理 88 while(!ok) 89 { 90 IDAstar(0); 91 deep++;//一层一层加深搜索的深度 92 } 93 } 94 95 return 0; 96 }