【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 ...
随机推荐
- javascript 核心概念(1)-数据类型
语法 (1)到现在为止,大多数浏览器也还是支持到ECMAScript 第三版的标准. 核心概念就是一个语言的基本工作原理,涉及语法,操作符,数据类型. (2)javascript的一切--变量,函数名 ...
- 前端-Vue结构思维导图笔记
看不清的朋友右键保存或者新窗口打开哦!喜欢我可以关注我,还有更多前端思维导图笔记有vue结构分析,JS基础,JQ,JS高级,Angular,git等等
- Spark on Yarn集群搭建
软件环境: linux系统: CentOS6.7 Hadoop版本: 2.6.5 zookeeper版本: 3.4.8 主机配置: 一共m1, m2, m3这五部机, 每部主机的用户名都为centos ...
- 【PostgreSQL-9.6.3】表操作语句
1.创建数据表 create table table_name ( 字段1 数据类型[列级别约束条件][默认值], 字段2 数据类型[列级别约束条件][默认值], 字段3 数据类型[列级别约束条件][ ...
- sphinx在windows下的简单安装与使用
1.下载地址 http://sphinxsearch.com/downloads/release/,我这里下的是“Win64 binaries w/MySQL+PgSQL+libstemmer+id6 ...
- C#的WebBrowser操作frame
刚学c#不久,也不太懂什么IHTMLDocument.IHTMLDocument2.IWebBrowser2等等.自己琢磨了好久,终于知道了怎么用WebBrowser操作frame和iframe. 1 ...
- php知识点(基本上文档都有,只为方便记忆)
类型转换 (unset)转换为NULL (binary) 转换和 b 前缀转换支持为 PHP 5.2.1 新增 转换二进制 隐藏php后缀名 AddType application/x-httpd ...
- python从TXT创建PDF文件——reportlab
使用reportlab创建PDF文件电子书一般都是txt格式的,某些电子阅读器不能读取txt的文档,如DPT-RP1.因此本文从使用python实现txt到pdf的转换,并且支持生成目录,目录能够生成 ...
- elasticsearch重建索引
1.重建索引 一个field的设置是不能被修改的,如果要修改一个Field,那么应该重新按照新的mapping,建立一个index,然后将数据批量查询出来,重新用bulk api写入index中 批量 ...
- Python数据分析----scipy稀疏矩阵
一.sparse模块: python中scipy模块中,有一个模块叫sparse模块,就是专门为了解决稀疏矩阵而生.本文的大部分内容,其实就是基于sparse模块而来的 导入模块:from scipy ...