您的位置:首页 > 其它

HDU 4127Flood-it!

2015-11-08 20:41 267 查看


Flood-it!


Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)


Total Submission(s) : 2   Accepted Submission(s) : 1


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[sub]i,j[/sub])[sub]n×n[/sub] representing the game board.
a[sub]i,j[/sub] 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*

地图上还有多少种颜色,为至少需要染色次数

第一次写IDA*  然后之后练习中就习惯了  慢慢理解了

每次都将左上角连通块整体变为一种颜色,求最少使用步数将整个地图染色为同一种

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

int vis[10][10];
int Map[10][10];
int n;
int dir[4][2]={0,1,0,-1,1,0,-1,0};

bool cheak(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<n)
return true ;
return false ;
}

void set_vis(int x,int y,int c) //设置颜色块 1为左上角连通 2为连通块相连块
{
vis[x][y]=1;
for(int i=0;i<4;i++)
{
int xx,yy;
xx=x+dir[i][0];
yy=y+dir[i][1];
if(cheak(xx,yy))
{
if(vis[xx][yy]==1) continue;
if(Map[xx][yy]==c) set_vis(xx,yy,c);
else vis[xx][yy]=2;
}
}
return ;
}

int get_h() //估价函数
{
bool flag[10];
int h=0;
memset(flag,false ,sizeof(flag));
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
if(vis[i][j]!=1&&!flag[Map[i][j]])
{
flag[Map[i][j]]=true ;
h++;
}
}
return h;
}

int get_cnt(int c) //如果选择颜色C 能改变多少块
{
int ans=0;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(vis[i][j]==2&&Map[i][j]==c)
{
set_vis(i,j,c);
ans++;
}
return ans;
}

int sum_h;

bool IDAstar(int sum)
{
if(sum==sum_h) return get_h()==0; //已经用了预定的步数
if(sum+get_h()>sum_h) return false ; //剪枝,预定步数小于预估步数
for(int i=0;i<6;i++)
{
int tmp[10][10];
memcpy(tmp,vis,sizeof(vis));
if(get_cnt(i)==0) continue ;
if(IDAstar(sum+1)) return true ;
memcpy(vis,tmp,sizeof(tmp));
}
return false ;
}

int main()
{
while(cin>>n)
{
if(n==0) break;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>Map[i][j];
memset(vis,0,sizeof(vis));
set_vis(0,0,Map[0][0]);
sum_h=get_h();
while(!IDAstar(0))
{
sum_h++;
}
cout<<sum_h<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: