HDU 4127 Flood-it!
2016-07-12 17:44
351 查看
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
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
,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;
}
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
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
,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;
}
相关文章推荐
- 【HDU 5366】The mook jong 详解
- 【HDU 2136】Largest prime factor 详细图解
- 【HDU 1568】Fibonacci 数学公式 详解
- HDU 1568
- HDU1290
- HDU1568(Fobonacci公式)
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)
- HDU 1405
- HDU 1297
- hdu 1205
- hdu 2087
- hdu 1016
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- HDU 5592 ZYB's Premutation 线段树(查找动态区间第K大)
- HDU 5240 Exam (好水的题)
- HDU5237 Base64 大模拟
- HDU 1000
- HDU 1001
- HDU 1016 Prime Ring Problem
- HDU 1017 A Mathematical Curiosity