【POJ 4007】 Flood-it!
【题目链接】
http://poj.org/problem?id=4007
【算法】
IDA*
【代码】
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
const int dx[] = {,,-,};
const int dy[] = {-,,,}; int i,j,n,step;
int v[][],a[][]; inline bool valid(int x,int y)
{
return x > && x <= n && y > && y <= n;
}
inline void dfs(int x,int y,int c)
{
int i,tx,ty;
v[x][y] = ;
for (i = ; i < ; i++)
{
tx = x + dx[i];
ty = y + dy[i];
if (valid(tx,ty) && v[tx][ty] != )
{
if (a[tx][ty] == c) dfs(tx,ty,c);
else v[tx][ty] = ;
}
}
}
inline int f()
{
int i,j,s = ;
bool h[];
memset(h,,sizeof(h));
for (i = ; i <= n; i++)
{
for (j = ; j <= n; j++)
{
if (!h[a[i][j]] && v[i][j] != )
{
h[a[i][j]] = true;
s++;
}
}
}
return s;
}
inline bool fill(int c)
{
int i,j,s = ;
for (i = ; i <= n; i++)
{
for (j = ; j <= n; j++)
{
if (a[i][j] == c && v[i][j] == )
{
s++;
dfs(i,j,c);
}
}
}
return s != ;
}
inline bool IDDFS(int dep)
{
int i,t;
int tmp[][];
t = f();
if (dep + t > step) return false;
if (!t) return true;
for (i = ; i <= ; i++)
{
memcpy(tmp,v,sizeof(v));
if (fill(i) && IDDFS(dep+))
return true;
memcpy(v,tmp,sizeof(v));
}
return false;
}
int main()
{ while (scanf("%d",&n) != EOF && n)
{
memset(v,,sizeof(v));
for (i = ; i <= n; i++)
{
for (j = ; j <= n; j++)
{
scanf("%d",&a[i][j]);
}
}
dfs(,,a[][]);
for (i = ; i <= n * n; i++)
{
step = i;
if (IDDFS())
break;
}
printf("%d\n",step);
} return ; }
【POJ 4007】 Flood-it!的更多相关文章
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- 【BZOJ 2288】 2288: 【POJ Challenge】生日礼物 (贪心+优先队列+双向链表)
2288: [POJ Challenge]生日礼物 Description ftiasch 18岁生日的时候,lqp18_31给她看了一个神奇的序列 A1, A2, ..., AN. 她被允许选择不超 ...
- 【POJ 1850】 Code
[POJ 1850] Code 还是非常想说 数位dp真的非常方便! !. 数位dp真的非常方便!.! 数位dp真的非常方便! !! 重要的事说三遍 该题转换规则跟进制差点儿相同 到z时进一位 如az ...
- 【POJ 2750】 Potted Flower(线段树套dp)
[POJ 2750] Potted Flower(线段树套dp) Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4566 ...
- 【poj 1984】&【bzoj 3362】Navigation Nightmare(图论--带权并查集)
题意:平面上给出N个点,知道M个关于点X在点Y的正东/西/南/北方向的距离.问在刚给出一定关系之后其中2点的曼哈顿距离((x1,y1)与(x2,y2):l x1-x2 l+l y1-y2 l),未知则 ...
- 【poj 1988】Cube Stacking(图论--带权并查集)
题意:有N个方块,M个操作{"C x":查询方块x上的方块数:"M x y":移动方块x所在的整个方块堆到方块y所在的整个方块堆之上}.输出相应的答案. 解法: ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
随机推荐
- Bootstrap3.0的栅格布局系统实现原理
这个标题取的有点奇怪,怪我翻译的有问题吧.英文学平有限,有道词典和google翻译齐上阵是必须的.还好翻译的不是小说,对于技术文章,还是能勉强翻过来的. 本文主要讲解了Bootstrap3.0的栅格布 ...
- Java中last_insert_id的使用
last_insert_id的作用是:在当前表中的主键是自增时,插入一条新记录的同时使用last_insert_id可以获取当前的新记录的主键id. 下面是一个例子: import java.sql. ...
- Haar、pico、npd、dlib等多种人脸检测特征及算法结果比较
原文:opencv.pico.npd.dlib.face++等多种人脸检测算法结果比较 NDP检测结果: 结果分析: Pico(Pixel Intensity Comparison-based Obj ...
- ubuntu16.04安装KDE
由于对KDE界面情有独钟,升级到ubuntu之后,第一件事就是安装kde桌面 命令: add-apt-repository ppa:kubuntu-ppa/backports apt-get upda ...
- python 处理中文 读取数据库输出全是问号
ref:http://www.cnblogs.com/zhoujie/archive/2013/06/07/problem1.html 1.python连接mssql数据库编码问题 python一直对 ...
- 在vue中,让表格td下的textraea自适应高度
1.效果图 2.数据是动态获取的,因此存在一个异步的问题,解决的思路是数据获取到渲染在textarea中以后,获取文字的真实高度,然后把这个高度给textarea 3.具体代码以及步骤 (1)再cre ...
- dup、文件锁、库函数、函数调用(day07)
一.lseek()重新定位文件的读写位置. #include <sys/types.h> #include <unistd.h> off_t lseek(int fd, off ...
- 09.正则表达式re-3.常用的匹配规则
模式 描述 \w 匹配字母.数字及下划线 \W 匹配不是字母.数字及下划线的字符 \s 匹配任意空白字符,等价于[\t\n\r\f] \S 匹配任意非空字符 \d 匹配任意数字,等价于[0-9] \D ...
- apache下部署showdoc
1. 安装apache2服务器sudo apt-get install apache2 2. 安装php环境 sudo apt-get install php7.1 libapache2-mod-ph ...
- CentOS7安装Kubernetes
CentOS7安装Kubernetes 安装Kubernetes时候需要一台机器作为管理机器,1台或者多台机器作为集群中的节点. 系统信息: Hosts: 请将IP地址换成自己环境的地址. cento ...