Flood-it!

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

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
 
 //2016.8.27
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; //vis[i][j]为1表示格子与左上角连通,为2表示与连通块相邻的格子,0为其他
int a[][], vis[][], n, deep;
int dx[] = {, , , -};
int dy[] = {, , -, };
bool ok; int Astar()//估价函数,计算除左上角连通块之外还有多少颜色,即最少要染的次数
{
int book[], h = ;
memset(book, , sizeof(book));
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
if(vis[i][j]!= && !book[a[i][j]])
book[a[i][j]]++, h++;
return h;
} void dfs(int x, int y, int color)//把颜色为color的格子并入左上角连通块
{
vis[x][y] = ;
for(int i = ; i < ; i++)
{
int nx = x+dx[i];
int ny = y+dy[i];
if(nx>=&&nx<=n&&ny>=&&ny<=n)
{
if(vis[nx][ny] == )continue;
vis[nx][ny] = ;
if(a[nx][ny] == color)
dfs(nx, ny, color);
}
}
} int fill(int color)//把vis[i][j]==2的格子染成color色
{
int cnt = ;
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
if(vis[i][j]==&&a[i][j]==color)
{
cnt++;
dfs(i, j, color);
}
return cnt;
} void IDAstar(int step)
{
if(ok)return ;
int h = Astar();
if(h == )
{
cout<<step<<endl;
ok = true;
return ;
}
if(step+h>deep)return ;
int tmp[][];
memcpy(tmp, vis, sizeof(tmp));
for(int i = ; i < ; i++)//进行染色
{
if(fill(i)==)continue;
IDAstar(step+);
memcpy(vis, tmp, sizeof(vis));
}
} int main()
{
while(scanf("%d", &n)!=EOF && n)
{
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
scanf("%d", &a[i][j]);
ok = false;
deep = ;
memset(vis, , sizeof(vis));
dfs(, , a[][]);//对左上角预处理
while(!ok)
{
IDAstar();
deep++;//一层一层加深搜索的深度
}
} return ;
}

HDU4127(IDA*)的更多相关文章

  1. 逆向工程 - Reveal、IDA、Hopper、HTTPS抓包 等

    目录: 一. iOS 如何做才安全 二.ipa文件 三.沙盒 中的数据 四.Reveal:查看 任何APP 的UI结构 五.反编译工具:IDA 六.反编译工具:Hopper Disassembler ...

  2. IDA的脚本IDC的一个简单使用

    目的:主要是想学习一下IDA的IDC的脚本的使用.这里做了一个小的测试. 这里使用的是VS2015Community来生成文件的. 一.编写测试程序: 这里先生成我们的目标数据. 然后编写测试程序.得 ...

  3. 安卓动态调试七种武器之孔雀翎 – Ida Pro

    安卓动态调试七种武器之孔雀翎 – Ida Pro 作者:蒸米@阿里聚安全 0x00 序 随着移动安全越来越火,各种调试工具也都层出不穷,但因为环境和需求的不同,并没有工具是万能的.另外工具是死的,人是 ...

  4. iOS程序逆向Mac下常用工具——Reveal、HopperDisassemble、IDA

    原文在此 一.Reveal 1 一般使用     Reveal是ITTY BITTY发布的UI分析工具,可以很直观的查看App的UI布局.如下图所示:     Reveal是需要付费的,需要89美元, ...

  5. IDA插件栈字符串识别插件

    该插件是一款可以自动识别栈上局部变量为字符串的插件,字符串形式如下,并自动的加上注释                                       如图:可以自动识别栈上的字符串 项目主 ...

  6. Android动态方式破解apk进阶篇(IDA调试so源码)

    一.前言 今天我们继续来看破解apk的相关知识,在前一篇:Eclipse动态调试smali源码破解apk 我们今天主要来看如何使用IDA来调试Android中的native源码,因为现在一些app,为 ...

  7. 计算机病毒实践汇总六:IDA Pro基础

    在尝试学习分析的过程中,判断结论不一定准确,只是一些我自己的思考和探索.敬请批评指正! 1. IDA使用 (1)搜索.下载并执行IDA Pro,对可执行程序lab05-01.dll进行装载,分别以图形 ...

  8. IDA在内存中dump出android的Dex文件

    转载自http://drops.wooyun.org/tips/6840 在现在的移动安全环境中,程序加壳已经成为家常便饭了,如果不会脱壳简直没法在破解界混的节奏.ZJDroid作为一种万能脱壳器是非 ...

  9. IDA来Patch android的so文件

    在上文中,我们通过分析定位到sub_130C()这个函数有很大可能性是用来做反调试检测的,并且作者开了一个新的线程,并且用了一个while来不断执行sub_130C()这个函数,所以说我们每次手动的修 ...

随机推荐

  1. 采用多线程方式,解决由于查询等待造成winfrom假死问题

    1.这里是触发一个比较耗时的操作,比如一次大数据量的查询: Thread thread = new Thread(new ThreadStart(DoWord)); thread.Start(); 2 ...

  2. 伸展二叉树树(C#)

    参考过好几篇关于将伸展树的代码,发现看不懂.看图能看懂原理.就尝试自己实现了下. 自顶向上的算法. using System; using System.Collections.Generic; us ...

  3. EasyUi之datagird解读

    1.其json格式需要为:  JSON Code  1234567891011121314151617181920212223   {     ,     "rows": [    ...

  4. servlet与CGI的区别

    与cgi的区别在于servlet处于服务器进程中,它通过多线程方式运行其service方法,一个实例可以服务于多个请求,并且其实例一般不会销毁,而CGI对每个请求都产生新的进程,服务完成后就销毁,所以 ...

  5. CSS-学习笔记六

    1. 自适应,响应式布局 2. pure 3. Animate

  6. UILabel的抗压缩、抗拉伸、以及控件的约束简述

    今天来说一说UILabel的约束设置问题 首先主要介绍:Priority(控件约束的优先级).Content Hugging Priority(控件抗拉伸优先级).Content Compressio ...

  7. UDP传输包大小(转)

    源:UDP传输包大小 在进行UDP编程的时候,我们最容易想到的问题就是,一次发送多少bytes好? 当然,这个没有唯一答案,相对于不同的系统,不同的要求,其得到的答案是不一样的,我这里仅对 像ICQ一 ...

  8. git如何正确回滚代码

    git如何正确回滚代码 方法一,删除远程分支再提交 ①首先两步保证当前工作区是干净的,并且和远程分支代码一致 $ git co currentBranch $ git pull origin curr ...

  9. time.setToNow() 取当前时间,月份有误

      [java] view plaincopy Time time = new Time("GMT+8"); time.setToNow(); int year = time.ye ...

  10. 搭建NDK环境

    2014.07.14 搭建OK,但是目前只能手动编译c代码,具体不清楚.