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
#include
#include
#include
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=0&&ysum_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>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<