HDU4127(IDA*)

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:
HDU4127(IDA*)
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):
HDU4127(IDA*)
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.

 

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
 

Sample Output

3
 
 //2016.8.27
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; //vis[i][j]为1表示格子与左上角连通,为2表示与连通块相邻的格子,0为其他
int a[][], vis[][], n, deep;
int dx[] = {, , , -};
int dy[] = {, , -, };
bool ok; int Astar()//估价函数,计算除左上角连通块之外还有多少颜色,即最少要染的次数
{
int book[], h = ;
memset(book, , sizeof(book));
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
if(vis[i][j]!= && !book[a[i][j]])
book[a[i][j]]++, h++;
return h;
} void dfs(int x, int y, int color)//把颜色为color的格子并入左上角连通块
{
vis[x][y] = ;
for(int i = ; i < ; i++)
{
int nx = x+dx[i];
int ny = y+dy[i];
if(nx>=&&nx<=n&&ny>=&&ny<=n)
{
if(vis[nx][ny] == )continue;
vis[nx][ny] = ;
if(a[nx][ny] == color)
dfs(nx, ny, color);
}
}
} int fill(int color)//把vis[i][j]==2的格子染成color色
{
int cnt = ;
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
if(vis[i][j]==&&a[i][j]==color)
{
cnt++;
dfs(i, j, color);
}
return cnt;
} void IDAstar(int step)
{
if(ok)return ;
int h = Astar();
if(h == )
{
cout<<step<<endl;
ok = true;
return ;
}
if(step+h>deep)return ;
int tmp[][];
memcpy(tmp, vis, sizeof(tmp));
for(int i = ; i < ; i++)//进行染色
{
if(fill(i)==)continue;
IDAstar(step+);
memcpy(vis, tmp, sizeof(vis));
}
} int main()
{
while(scanf("%d", &n)!=EOF && n)
{
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
scanf("%d", &a[i][j]);
ok = false;
deep = ;
memset(vis, , sizeof(vis));
dfs(, , a[][]);//对左上角预处理
while(!ok)
{
IDAstar();
deep++;//一层一层加深搜索的深度
}
} return ;
}
上一篇:Java for LeetCode 076 Minimum Window Substring


下一篇:201521123080《Java程序设计》第3周学习总结