【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 ...
随机推荐
- 大白话理解promise对象
Promise 代表了未来某个将要发生的事件(通常是一个异步操作) Promise 是异步编程的解决方案,能够简化多层回调嵌套,代表了未来某个将要发生的事件.Promise是一个构造函数,本身有a ...
- Android 判断app是否安装
1. private boolean isAppInstalled(Context context, String uri) { PackageManager pm = context.getPack ...
- redis-linux
redis3.0.4 server版本 jedis-2.7.2.jar spring-data-redis-1.6.0.RELEASE.jar commons-pool2-2.3.jar spring ...
- 【sqli-labs】 less22 Cookie Injection- Error Based- Double Quotes - string (基于错误的双引号字符型Cookie注入)
注入的过程和less 20 21一样,这次闭合cookie的使用的双引号
- js document 触发按键事件
// 键盘控制 var keyEvent = (function () { document.onkeydown = function (e) { if (e.keyCode === 38) { // ...
- js开发性能(一)
随着js技术的发展,性能问题开始被越来越多的人关注,最近了解了一些关于前端性能的问题,这里主要讨论一下在js脚本加载和执行的过程中,我们应该怎么样来提高js的性能. js脚本的处理 初学前端的时候,我 ...
- Codeforces Round #548 (Div. 2) A. Even Substrings
You are given a string
- nyoj169-素数
ms | 内存限制:65535 KB 难度:1 描述 走进世博园某信息通信馆,参观者将获得前所未有的尖端互动体验,一场充满创想和喜悦的信息通信互动体验秀将以全新形式呈现,从观众踏入展馆的第一步起, ...
- cent OS官网上下载老版本系统镜像的正确打开方式
当时的情况是这样的: 客户需要给服务器安装cent OS 7.3操作系统,我打开官网https://www.centos.org/,点击“GET CENTOS”——>“Minimal ISO”, ...
- GOF23设计模式之代理模式
GOF23设计模式之代理模式 核心作用:通过代理,控制对对象的访问.可以详细控制访问某个(某类)对象的方法,在调用这个方法前做前置处理,调用这个方法后做后置处理(即:AOP的微观实现) AOP(Asp ...