POJ 4007 Flood-it!
题目:http://poj.org/problem?id=4007
思路:
(lyd学长的思路)
IDA*算法,首先迭代加深限制搜索深度。
可以发现如果当前矩阵中除了左上角的连通块之外,共有M种颜色,那么还需要的步数不小于M。如果当前搜索深度+估价函数的值>深度限制,可以剪枝。
如果改变颜色后,左上角格子所在的联通块大小没有改变,可以剪枝,避免来回往复地搜索。
每次寻找左上角的格子所在的连通块耗费的时间常数巨大,可以在这里寻求突破。我们引入一个N*N的v数组。左上角的格子所在的连通块里的格子标记为1。左上角连通块周围一圈格子标记为2,其它格子标记为0。如果某次选择了颜色c,我们只需要找出标记为2并且颜色为c的格子,向四周扩展,并相应地修改v标记,就可以不断扩大标记为1的区域,最终如果所有格子标记都是1,就找到了答案。
//By SiriusRen
#include <cstdio>
#include <cstring>
using namespace std;
#define ff for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)
int n,a[10][10],vis[10][10],xx[]={1,-1,0,0},yy[]={0,0,1,-1},T;
bool check(int x,int y){return x>=1&&y>=1&&x<=n&&y<=n;}
void dye(int x,int y,int color){
vis[x][y]=1;
for(int i=0;i<=3;i++){
int dx=x+xx[i],dy=y+yy[i];
if(vis[dx][dy]==1||!check(dx,dy))continue;
if(a[dx][dy]==color)dye(dx,dy,color);
else vis[dx][dy]=2;
}
}
int h(){
int cnt=0,col[6];
memset(col,0,sizeof(col));
ff if(vis[i][j]!=1&&!col[a[i][j]])
cnt++,col[a[i][j]]=1;
return cnt;
}
bool count(int color){
bool flag=0;
ff if(a[i][j]==color&&vis[i][j]==2)
flag=1,dye(i,j,color);
return flag;
}
bool A_star(int deep){
if(deep==T)return !h();
if(deep+h()>T)return 0;
for(int ii=0;ii<=5;ii++){
int temp[9][9];
ff temp[i][j]=vis[i][j];
if(!count(ii))continue;
if(A_star(deep+1))return 1;
ff vis[i][j]=temp[i][j];
}
return 0;
}
int main(){
while(~scanf("%d",&n)&&n){
memset(vis,0,sizeof(vis));
ff scanf("%d",&a[i][j]);
dye(1,1,a[1][1]);
for(T=h();;T++){
if(A_star(0)){printf("%d\n",T);break;}
}
}
}
哈哈哈哈哈
POJ 4007 Flood-it!的更多相关文章
- 【POJ 4007】 Flood-it!
[题目链接] http://poj.org/problem?id=4007 [算法] IDA* [代码] #include <algorithm> #include <bitset& ...
- [NOIP 2014复习]第二章:搜索
一.深度优先搜索(DFS) 1.Wikioi 1066引水入城 题目描写叙述 Description 在一个遥远的国度,一側是风景秀美的湖泊,还有一側则是漫无边际的沙漠.该国的行政 区划十分特殊,刚好 ...
- 【POJ】4007.Flood-it!
原题戳这里 题解 搜索是个好东西,不是人人都会搜 迭代加深,然后用一个函数估值,值是除了和左上连通的部分还有几个颜色不同的块,如果走的步数加上估值大于当前枚举的深度就跳出 代码 #include &l ...
- SYN Flood测试
由于工作需要对公司进行SYN Flood测试,在网上查了些资料,Youtube上找到最多的方法就是hping3工具来实现, 该工具已经预装在Kali下,具体操作用一条命令即可实现. hping3 -S ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
随机推荐
- [CSS3] CSS Background Images
Body with background image and gradient html { background: linear-gradient(#000, white) no-repeat; h ...
- [Hyperapp] Interact with the State Object through Hyperapp Action functions
Hyperapp is an ultra lightweight (1kb), minimal, functional, JavaScript library for building UIs. It ...
- MongoDB之Java測试代码(DAO层)
MongoInit.java是数据库初始化及连接类 MongoUtils.java是对mongodb的各种操作方法 MongoInit.java package com.wlwcloud.datate ...
- linux高级技巧:heartbeat+lvs(三)
之前我们把LVS和heartbeat都单独进行了測试,是时候进行合并了 1.LVS+heartbeat: 首先显示我们的控制台: 让这两个 ...
- [windows+cocos2dx]CCSprite精灵类
序言 回想cocos2dx,之前在mac+Xcode平台学习了一遍cocos2dx,一年时间不接触cocos了.一直在搞Unity3d.如今还是就之前所学温故温故,但不再用Xcode来写.用经常使用的 ...
- java中StringBuilder、StringBuffer、String类之间的关系
今天在CSDN的高校俱乐部里看到了"Java基础水平測试(英文)".感觉自己学了java这么久,想看下自己的java水平究竟是个什么样.測试结果就不说了,反正是慘不忍睹. 看了一下 ...
- UVALive 4192/HDU 2959 Close Enough Computations 数学
Close Enough Computations Problem Description The nutritional food label has become ubiquitous. A sa ...
- composer是什么
composer是什么 Composer 是 PHP5.3以上 的一个依赖管理工具.它允许你声明项目所依赖的代码库,它会在你的项目中为你安装他们.Composer 不是一个包管理器.是的,它涉及 &q ...
- zzulioj--1786--求最大值(技巧题)
1786: 求最大值 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 222 Solved: 46 SubmitStatusWeb Board Des ...
- maven关于pom文件配置详解(转载)
转载:http://www.cnblogs.com/hafiz <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...