HDU-2871 Flood-it!(IDA*)

Flood-it!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


Problem Description
Flood-it is a fascinating puzzle game on Google+ platform. The game interface is like follows:
HDU-2871 Flood-it!(IDA*)_第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-2871 Flood-it!(IDA*)_第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

很容易就能想到IDA*

不过最开始只想到目前至少要染多少次的剪枝 TLE,看到别人代码注意到当前染色后连通块必须增大,以此剪枝就还是TLE,最后又把vector删了,自己写了个结构体才AC

看来vector还是不敢随意用


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int a[9][9],n,num[6],nn,ans,depth,tmp,pre,rr,cc;
int dr[4]={-1,0,1,0},dc[4]={0,1,0,-1};
bool vis[9][9];

struct Node {
    int r,c;
    Node(int rr=0,int cc=0) {
        r=rr,c=cc;
    }
};

struct node {
    int num;
    Node a[65];
};

void judge() {
    int i,j;
    for(i=0;i<=5;++i)
        num[i]=0;
    for(i=0;i<n;++i)
        for(j=0;j<n;++j)
            ++num[a[i][j]];
    sort(num,num+6);
}

void bfs(node& change) {
    int i=0,j,co=a[0][0];
    memset(vis,false,sizeof(vis));
    vis[0][0]=true;
    change.a[0].r=change.a[0].c=0;
    change.num=1;
    while(i<change.num) {
        for(j=0;j<4;++j) {
            if(0<=(rr=change.a[i].r+dr[j])&&rr<n&&0<=(cc=change.a[i].c+dc[j])&&cc<n&&a[rr][cc]==co&&!vis[rr][cc]) {
                change.a[change.num].r=rr,change.a[change.num].c=cc;
                ++change.num;
                vis[rr][cc]=true;
            }
        }
        ++i;
    }
}

bool dfs(int step) {
    judge();
    if(num[5]==nn) {
        ans=step;
        return true;
    }
    int i;
    tmp=0;
    for(i=4;i>=0;--i,++tmp)
        if(num[i]==0)
            break;
    if(step+tmp>depth)
        return false;
    node change;
    int col=a[0][0],j;
    bfs(change);
    if(change.num<=pre)
        return false;
    for(i=0;i<6;++i) {
        if(col==i)
            continue;
        for(j=0;j<change.num;++j)
            a[change.a[j].r][change.a[j].c]=i;
        pre=change.num;
        if(dfs(step+1))
            return true;
    }
    for(j=0;j<change.num;++j)
        a[change.a[j].r][change.a[j].c]=col;
    return false;
}

int main() {
    int i,j;
    while(scanf("%d",&n),n) {
        nn=n*n;
        for(i=0;i<n;++i)
            for(j=0;j<n;++j)
                scanf("%d",&a[i][j]);
        depth=0;
        pre=0;
        while(!dfs(0)) {
            ++depth;
            pre=0;
        }
        printf("%d\n",ans);
    }
    return 0;
}


你可能感兴趣的:(HDU,IDA)