HDU 4127 Flood-it!

来源:互联网 发布:windows讲述人快捷键 编辑:程序博客网 时间:2024/06/05 03:38
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). 

 

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
20 0 0 030 1 21 1 22 2 10
 

Sample Output
03

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;}


0 0
原创粉丝点击