hdu 4127 Flood-it! (IDA+bfs)

Flood-it!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1187    Accepted Submission(s): 240


Problem Description
Flood-it is a fascinating puzzle game on Google+ platform. The game interface is like follows:
hdu 4127 Flood-it! (IDA+bfs)_第1张图片
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):
hdu 4127 Flood-it! (IDA+bfs)_第2张图片
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
 

Source
2011 Asia Fuzhou Regional Contest


思路:
IDA+bfs.
限制深度进行dfs,bfs找连通块,每次保证像连通块变多的方向搜索,具体剪枝详见代码。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define maxn 10
#define INF 0x3f3f3f3f
using namespace std;

int n,m,ans,flag,depth;
int mp[maxn][maxn];
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
bool vis[maxn][maxn];
struct Node
{
    int x,y;
}cur,now,q[100];
struct node
{
    int num;
    Node xx[100];
};

void bfs(node &tt,int color[])
{
    int i,j,nx,ny,tx,ty;
    int head=0,tail=-1;
    tt.num=0;
    memset(vis,0,sizeof(vis));
    cur.x=cur.y=1;
    vis[1][1]=1;
    q[++tail]=cur;
    while(head<=tail)
    {
        now=q[head];
        head++;
        tt.xx[++tt.num]=now;
        nx=now.x;
        ny=now.y;
        for(i=0;i<4;i++)
        {
            tx=nx+dx[i];
            ty=ny+dy[i];
            if(tx<1||tx>n||ty<1||ty>n||vis[tx][ty]||mp[tx][ty]!=mp[nx][ny]) continue ;
            cur.x=tx;
            cur.y=ty;
            vis[tx][ty]=1;
            q[++tail]=cur;
        }
    }
    for(i=1;i<=n;i++)    // 标记还剩几种颜色
    {
        for(j=1;j<=n;j++)
        {
            if(!vis[i][j]) color[mp[i][j]]=1;
        }
    }
}
void dfs(int c,int cnt,int step)   // 当前颜色 连通块大小 步数
{
    if(step>depth||flag) return ;  // 已经搜到结果就return
    int i,j,t=0,num,color[6]={0};
    node tt;
    bfs(tt,color);
    num=tt.num;
    if(num<=cnt) return ;      // 保证每次都要向变多的方向搜索
    for(i=0;i<6;i++)           // 计算还剩几种颜色
    {
        t+=color[i];
    }
    if(step+t>depth) return ;  // 当前步数+还剩颜色数>depth return
    if(num==n*n)
    {
        flag=1;
        ans=step;
        return ;
    }
    for(i=0;i<6;i++)
    {
        if(i==c) continue ;
        for(j=1;j<=num;j++)   // 改变颜色
        {
            mp[tt.xx[j].x][tt.xx[j].y]=i;
        }
        dfs(i,num,step+1);
        for(j=1;j<=num;j++)   // 还原颜色
        {
            mp[tt.xx[j].x][tt.xx[j].y]=c;
        }
    }
}
void IDA()
{
    int i,j;
    flag=0;
    depth=1;
    while(!flag)
    {
        dfs(mp[1][1],0,0);
        depth++;
    }
}
int main()
{
    int i,j;
    while(scanf("%d",&n),n)
    {
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                scanf("%d",&mp[i][j]);
            }
        }
        ans=INF;
        IDA();
        printf("%d\n",ans);
    }
    return 0;
}




 

你可能感兴趣的:(hdu 4127 Flood-it! (IDA+bfs))