HDU4127(IDA*)
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

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 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
Sample Input
Sample Output
//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*)的更多相关文章
- 逆向工程 - Reveal、IDA、Hopper、HTTPS抓包 等
目录: 一. iOS 如何做才安全 二.ipa文件 三.沙盒 中的数据 四.Reveal:查看 任何APP 的UI结构 五.反编译工具:IDA 六.反编译工具:Hopper Disassembler ...
- IDA的脚本IDC的一个简单使用
目的:主要是想学习一下IDA的IDC的脚本的使用.这里做了一个小的测试. 这里使用的是VS2015Community来生成文件的. 一.编写测试程序: 这里先生成我们的目标数据. 然后编写测试程序.得 ...
- 安卓动态调试七种武器之孔雀翎 – Ida Pro
安卓动态调试七种武器之孔雀翎 – Ida Pro 作者:蒸米@阿里聚安全 0x00 序 随着移动安全越来越火,各种调试工具也都层出不穷,但因为环境和需求的不同,并没有工具是万能的.另外工具是死的,人是 ...
- iOS程序逆向Mac下常用工具——Reveal、HopperDisassemble、IDA
原文在此 一.Reveal 1 一般使用 Reveal是ITTY BITTY发布的UI分析工具,可以很直观的查看App的UI布局.如下图所示: Reveal是需要付费的,需要89美元, ...
- IDA插件栈字符串识别插件
该插件是一款可以自动识别栈上局部变量为字符串的插件,字符串形式如下,并自动的加上注释 如图:可以自动识别栈上的字符串 项目主 ...
- Android动态方式破解apk进阶篇(IDA调试so源码)
一.前言 今天我们继续来看破解apk的相关知识,在前一篇:Eclipse动态调试smali源码破解apk 我们今天主要来看如何使用IDA来调试Android中的native源码,因为现在一些app,为 ...
- 计算机病毒实践汇总六:IDA Pro基础
在尝试学习分析的过程中,判断结论不一定准确,只是一些我自己的思考和探索.敬请批评指正! 1. IDA使用 (1)搜索.下载并执行IDA Pro,对可执行程序lab05-01.dll进行装载,分别以图形 ...
- IDA在内存中dump出android的Dex文件
转载自http://drops.wooyun.org/tips/6840 在现在的移动安全环境中,程序加壳已经成为家常便饭了,如果不会脱壳简直没法在破解界混的节奏.ZJDroid作为一种万能脱壳器是非 ...
- IDA来Patch android的so文件
在上文中,我们通过分析定位到sub_130C()这个函数有很大可能性是用来做反调试检测的,并且作者开了一个新的线程,并且用了一个while来不断执行sub_130C()这个函数,所以说我们每次手动的修 ...
随机推荐
- PAT (Advanced Level) 1009. Product of Polynomials (25)
简单模拟. #include<iostream> #include<cstring> #include<cmath> #include<algorithm&g ...
- EasyUi之datagird解读
1.其json格式需要为: JSON Code 1234567891011121314151617181920212223 { , "rows": [ ...
- Python核心编程第二版(中文).pdf 目录整理
python核心编程目录 Chapter1:欢迎来到python世界!-页码:7 1.1什么是python 1.2起源 :罗萨姆1989底创建python 1.3特点 1.3.1高级 1.3.2面向 ...
- clip实现圆环进度条
效果主要通过clip和transform:rotate实现 把圆环分为左右两个部分,通过角度旋转对图片剪切旋转角度<=180度的时候之旋转右边,当大于180度时右边固定旋转180度的同时旋转左边 ...
- Android系统属性SystemProperties分析
下面这几个博客总结的不错,有空看下: http://www.cnblogs.com/bastard/archive/2012/10/11/2720314.html http://blog.csdn.n ...
- mysql忘记密码及无法使用程序连接
select(一)忘记root密码 很久之前装了mysql,后来做项目的时候,一直木有用到,就把密码给忘记了. 网上各种找,最终解决了这个问题了,记录下. 1)打开services,找到mysql服务 ...
- 【转】国外程序员收集整理的PHP资源大全
ziadoz在 Github发起维护的一个PHP资源列表,内容包括:库.框架.模板.安全.代码分析.日志.第三方库.配置工具.Web 工具.书籍.电子书.经典博文等等.伯乐在线对该资源列表进行了翻译, ...
- linux学习小记:如何查看linux服务器的cpu数量,内核数,和cpu线程数
查看物理cpu个数 grep 'physical id' /proc/cpuinfo | sort -u | wc -l 查看每个cpu核心数量 grep 'core id' /proc/cpuinf ...
- iOS调试-LLDB学习总结
from:http://www.jianshu.com/p/d6a0a5e39b0e LLDB阐述 LLDB 是一个有着 REPL 的特性和 C++ ,Python 插件的开源调试器.LLDB 绑定在 ...
- web前端面试第三波~
快来测试测试自己掌握能力吧! 1. class.forname的作用?为什么要用? 1).获取Class对象的方式:类名.class.对象.getClass().Class.forName(" ...