【题目链接】

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!的更多相关文章

  1. BZOJ2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 284  Solved: 82[Submit][St ...

  2. BZOJ2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 126  Solved: 90[Submit][Sta ...

  3. 【BZOJ 2288】 2288: 【POJ Challenge】生日礼物 (贪心+优先队列+双向链表)

    2288: [POJ Challenge]生日礼物 Description ftiasch 18岁生日的时候,lqp18_31给她看了一个神奇的序列 A1, A2, ..., AN. 她被允许选择不超 ...

  4. 【POJ 1850】 Code

    [POJ 1850] Code 还是非常想说 数位dp真的非常方便! !. 数位dp真的非常方便!.! 数位dp真的非常方便! !! 重要的事说三遍 该题转换规则跟进制差点儿相同 到z时进一位 如az ...

  5. 【POJ 2750】 Potted Flower(线段树套dp)

    [POJ 2750] Potted Flower(线段树套dp) Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4566   ...

  6. 【poj 1984】&【bzoj 3362】Navigation Nightmare(图论--带权并查集)

    题意:平面上给出N个点,知道M个关于点X在点Y的正东/西/南/北方向的距离.问在刚给出一定关系之后其中2点的曼哈顿距离((x1,y1)与(x2,y2):l x1-x2 l+l y1-y2 l),未知则 ...

  7. 【poj 1988】Cube Stacking(图论--带权并查集)

    题意:有N个方块,M个操作{"C x":查询方块x上的方块数:"M x y":移动方块x所在的整个方块堆到方块y所在的整个方块堆之上}.输出相应的答案. 解法: ...

  8. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  9. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

随机推荐

  1. scrollWidth clientWidth offsetWidth

    scrollWidth:对象的实际内容的宽度,不包边线宽度,会随对象中内容超过可视区后而变大.         实际内容+padding                 不包括滚动条 边框client ...

  2. Android几种常见的多渠道(批量)打包方式介绍

    多渠道打包,主要是为了统计不同的渠道上包的下载数量,渠道越多,我们需要打的包数量越多,这个时候,我们没法去使用单纯的手动打包去一个一个的生成不同的渠道包,我们需要更高效的打包方式. 声明渠道方式一: ...

  3. 金立 M6 (GN8003) 解锁 BootLoader 进入第三方 recovery 刷机 ROOT

    首先下载好工具:http://url.cn/5EILbQn 备用连接 :http://pan.baidu.com/s/1c28j7k0 本篇教程教你如何傻瓜式解锁BootLoader并刷入recove ...

  4. Excel常用的小技巧

    1.Excel如何实现单元格内轻松换行:按住ALT+enter就可以了. 2.Excel固定表头:在“视图”>冻结窗口>冻结首行. 3.防止电脑突然断电,导致正在编辑的Excel数据丢失, ...

  5. UID卡、CUID卡、FUID卡的区别

    UID卡(国外称GEN1) 所有区块可被重复读写 卡片ID可改且使用后门指令更改ID ID可被重复修改 响应后门指令(意味着可被使用后门指令检测是否为克隆卡的机器发现) CUID卡(国外称GEN2) ...

  6. nim游戏解法(转)

    转自:http://acm.hdu.edu.cn/forum/read.php?fid=9&tid=10617 取火柴的游戏 题目1:今有若干堆火柴,两人依次从中拿取,规定每次只能从一堆中取若 ...

  7. java 循环document 通用替换某个字符串或特殊字符

    document 生成xml时 报错 XML-20100: (Fatal Error) Expected ';'.  查了半天发现是 特殊字符 & 不能直接转出,需要进行转换,因为是通用方法很 ...

  8. MySQL--增删改查分页存储过程以及事务

    添加和修改写在一起了 可以用id判断添加和修改 和事务在一起编码 可以让代码更严谨 在这里简单的说一下事务的四大特性 事务四大特性之原子性:原子性是指事务是一个不可再分割的工作单位,事务中的操作要么都 ...

  9. 从输入url到页面展示出来经历了哪些过程

    本文只是一个整理向的随笔,以个人思路来简化的同时进行适当的拓展,如有错误,欢迎指正. 1.输入网址.  此时得到一个url 2.域名解析 整个过程都是dns系统在发挥作用,它的目的是将域名和ip对应起 ...

  10. BZOJ 1705: [Usaco2007 Nov]Telephone Wire 架设电话线 DP + 优化 + 推导

    Description 最近,Farmer John的奶牛们越来越不满于牛棚里一塌糊涂的电话服务 于是,她们要求FJ把那些老旧的电话线换成性能更好的新电话线. 新的电话线架设在已有的N(2 <= ...