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()这个函数,所以说我们每次手动的修 ...
随机推荐
- /system改成可写
读写: mount -o remount,rw /dev/block/mtdblock0 /system 只读mount -o remount,ro /dev/block/mtdblock0 /sys ...
- Apache 隐藏入口文件 index.php
新建 .htaccess文件至站点目录下,并写入如下代码: RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQ ...
- 电子工程师名片——FAT16文件系统(转)
源:电子工程师名片——FAT16文件系统 从8月8号开始,连续一个月利用每天下班时间和周末的时间终于初步完成了一个电子工程师的电路板名片,就像U盘一样,不过这个FLASH只有64KB的大小,用的单片机 ...
- c#之时间戳与DateTime的相互转换
1. 时间戳转 DateTime static DateTime GetServerNow(ulong serverTime) { DateTime dateTimeStart = TimeZone. ...
- /bin/sh 与 /bin/bash 的区别
/bin/sh 与 /bin/bash 的区别,用 : 截取字符串不是POSIX 标准的. 区别 sh 一般设成 bash 的软链 (symlink) ls -l /bin/sh lrwxrwxrwx ...
- 对position的理解
作者:zccst 先看看手册 值 描述 absolute 生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位. 元素的位置通过 "left", "t ...
- windows下查找指定端口被哪个程序占用
在Windows环境下,用netstat命令查看某个端口号是否占用,为哪个进程所占用. eg.查看端口号为61078被哪个程序占用 1.查看端口号为61079被哪个PID所占用:Netstat –an ...
- abs函数
absolute 绝对值函数 abs函数是一个取绝对值函数,你得确保ABS()括号里的表达式所计算出的结果是数字,String是字符串的意思,你括号你的数据肯定是字符串了,如果A.B两变量你是这样定义 ...
- 【解决】System.Web.Http.Description 缺失
一.问题描述 使用visual studio 2013创建mvc4 api模板,然后build,run,broken,出错如下: Error 1 The type or namespace name ...
- Statemen接口对象
利用Statement接口实现数据表的更新和查询操作 -取得Statement接口对象:Statement createStatement(int resultSetType, int resultS ...