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()这个函数,所以说我们每次手动的修 ...
随机推荐
- js - object的属性操作
视频学习地址: http://www.imooc.com/video/6002 原文PPT下载地址: http://img.mukewang.com/down/54c5ec1a000141f10000 ...
- ZenCoding 个人理解和总结
我的理解:ZenCoding是一个html简写的语法,可以最快速的生成html. 不少IDE应该都支持,我用的intellij idea是支持的. ZenCoding表示和CSS/JS有相通之处,比如 ...
- php 中的全局变量的理解
$GLOBALS 是php中的一个全局变量的数组. $GLOBALS['var1'] 代表的是 外部的全局变量 $var1 本身.global $var是外部$var的同名引用或者指针 例1: &l ...
- python_eval的用法
1. eval用法: 将字符串str当成有效的表达式来求值并返回计算结果. 2. eval的功能: math当成一个计算器很好用. 将字符串转换为list,tuple,dict. 3. 举例 # -* ...
- Go Runtime hashmap实现
努力学习go中,看到skoo博客内容很不错, 所以转载学习下 前两天有小伙伴问道是否看过 Go 语言 map 的实现,当时还真没看过,于是就花了一点时间看了一遍 runtime 源码中的 hashma ...
- ios中操作技巧
1.配置字段快捷键: @property(nonatimic,copy) NSString *<#param#>; 2.NSNumber 转NSString 最快简单方式: NSNumbe ...
- PHP 多线程、多进程
多线程:PHP其实并不支持多线程,只是通过一些扩展或者socket方式伪装成多线程,实质不是的.在PHP 5.3 以上版本,使用 pthreads PHP扩展,可以使PHP真正地支持多线程:或者使用 ...
- PHP运行方式
原文链接:http://www.cnblogs.com/xia520pi/p/3914964.html 1.运行模式 关于PHP目前比较常见的五大运行模式: 1)CGI(通用网关接口 / Common ...
- 15、手把手教你Extjs5(十五)各种Grid列的自定义渲染
Grid各列已经能够展示出来了.列的类型包括字符型,整型,浮点型,货币型,百分比型,日期型和布尔型,我自定义了各种类型的渲染样式: 1.整型:标题栏居中,数值靠右显示,正数颜色为蓝色,负数颜色为红色, ...
- 开源免费的C/C++网络库(c/c++ sockets library)补充
(1)ACE 庞大.复杂,适合大型项目.开源.免费,不依赖第三方库,支持跨平台. http://www.cs.wustl.edu/~schmidt/ACE.html (2)Asio Asio基于Boo ...