HDU 4127 Flood-it!

63 篇文章 0 订阅
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 (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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值