POJ4007 Flood-it!
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 337 | Accepted: 123 |
Description

At the beginning of the game, system will randomly generate an N×N square board and each grid of the board is painted by one of the six colors. The player starts from the top left corner. At each step, he/she selects a color and changes all the grids connected with the top left corner to that specific color. The statement “two grids are connected” means that there is a path between the certain two grids under condition that each pair of adjacent grids on this path is in the same color and shares an edge. In this way the player can flood areas of the board from the starting grid (top left corner) until all of the grids are in same color. The following figure shows the earliest steps of a 4×4 game (colors are labeled in 0 to 5):

Given a colored board at very beginning, please find the minimal number of steps to win the game (to change all the grids into a same color).
Input
The following N lines show an N×N matrix (ai,j)n×n representing the game board. ai,j is in the range of 0 to 5 representing the color of the corresponding grid.
The input ends with N = 0.
Output
Sample Input
2
0 0
0 0
3
0 1 2
1 1 2
2 2 1
0
Sample Output
0
3
Source
/*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int mxn=;
int mx[]={,,,-,};
int my[]={,,,,-};
int mp[mxn][mxn];
int vis[mxn][mxn];
int n;
//估价函数
bool cvis[];
int pre(){
int res=;
memset(cvis,,sizeof cvis);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
if(vis[i][j]!= && !cvis[mp[i][j]]){
cvis[mp[i][j]]=; res++;
}
}
return res;
}
//方块染色
void change(int x,int y,int c){
vis[x][y]=;//和左上角同色
int i,j;
for(i=;i<=;i++){
int nx=x+mx[i],ny=y+my[i];
if(nx< || nx>n || ny< || ny>n)continue;//边界判断
if(vis[nx][ny]==)continue;//访问判断
if(mp[nx][ny]==c)change(nx,ny,c);
else vis[nx][ny]=;
}
return;
}
bool solve(int color){//尝试染对应颜色
bool flag=;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
if(mp[i][j]==color && vis[i][j]== ){
change(i,j,color);
flag=;//自带剪枝:如果染这个颜色不能扩大联通范围,就不染
}
}
return flag;
}
int limit;
bool DFS(int now){
if(now==limit)return (pre()==);
if(now+pre()>limit)return ;
int i,j,k;
for(k=;k<=;k++){
int cpy[][];
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
cpy[i][j]=vis[i][j];
}//保存状态
if(!solve(k))continue;
if(DFS(now+))return ;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
vis[i][j]=cpy[i][j];
}//回溯
}
return ;
}
int main(){
int i,j;
while(scanf("%d",&n) && n){
memset(vis,,sizeof vis);
for(i=;i<=n;i++)
for(j=;j<=n;j++)
scanf("%d",&mp[i][j]);
change(,,mp[][]);
for(limit=pre();limit;limit++){//迭代加深
if(DFS())break;
}
printf("%d\n",limit);
}
return ;
}
POJ4007 Flood-it!的更多相关文章
- SYN Flood测试
由于工作需要对公司进行SYN Flood测试,在网上查了些资料,Youtube上找到最多的方法就是hping3工具来实现, 该工具已经预装在Kali下,具体操作用一条命令即可实现. hping3 -S ...
- 浅谈iptables防SYN Flood攻击和CC攻击
------------------------本文为自己实践所总结,概念性的东西不全,这里粗劣提下而已,网上很多,本文主要说下目前较流行的syn洪水攻击和cc攻击------------------ ...
- SYN Flood应如何应对
1 什么是SYN Flood攻击 在TCP三次握手时,服务器接收客户端的SYN请求,操作系统将为该请求分配一个TCP(Transmission Control Block),服务器返回一个SYN/AC ...
- Message Flood
Message Flood Time Limit: 1500MS Memory limit: 65536K 题目描述 Well, how do you feel about mobile phone? ...
- 图像处理之泛洪填充算法(Flood Fill Algorithm)
泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...
- Cisco 防止SYN Flood 攻击原理
DoS(Denial of Service拒绝服务)和DDoS(Distributed Denial of Service分布式拒绝服务)攻击是大型网站和网络服务器的安全威胁之一.2000年2月,Ya ...
- Nginx下防御HTTP GET FLOOD(CC)攻击
Nginx下防御HTTP GET FLOOD(CC)攻击 Nginx是一款轻量级的Web服务器,由俄罗斯的程序设计师Igor Sysoev所开发,最初供俄国大型的入口网站及搜寻引Rambler使用. ...
- Codeforces gym 100685 F. Flood bfs
F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...
- 扯谈网络编程之Tcp SYN flood洪水攻击
简单介绍 TCP协议要经过三次握手才干建立连接: (from wiki) 于是出现了对于握手过程进行的攻击.攻击者发送大量的SYN包,server回应(SYN+ACK)包,可是攻击者不回应ACK包,这 ...
- TCP洪水攻击(SYN Flood)的诊断和处理
TCP洪水攻击(SYN Flood)的诊断和处理 SYN Flood介绍 前段时间网站被攻击多次,其中最猛烈的就是TCP洪水攻击,即SYN Flood. SYN Flood是当前最流行的DoS(拒 ...
随机推荐
- db2的定时备份
定时任务: db2.bat db2cmd -i -w db2_backup.bat exit db2_backup.bat db2 connect to TEST db2 force applicat ...
- 廖老师JavaScript教程高阶函数-sort用法
先来学习一个新词:高阶函数 高阶函数英文叫Higher-order function.那么什么是高阶函数? JavaScript的函数其实都指向某个变量.既然变量可以指向函数,函数的参数能接收变量,那 ...
- console.log与console.dir的区别
今天学习promise的时候看到了console.dir这个方法,感到很好奇,查了以下感觉又长知识了 在Chrome中,控制台对象定义了两个似乎做同样事情的方法: console.log() cons ...
- JWT的使用流程
JWT的实现原理 一篇文章告诉你JWT的实现原理 发布于 3 个月前 作者 axetroy 3097 次浏览 来自 分享 在使用 JWT 的时候,有没有想过,为什么我们需要 JWT?以及它的工作原理是 ...
- 【windows】【md5】查看文件的md5值
certutil -hashfile filename MD5 certutil -hashfile filename SHA1 certutil -hashfile filename SHA256 ...
- 《linux设备驱动开发详解》笔记——12linux设备驱动的软件架构思想
本章重点讲解思想.思想.思想. 12.1 linux驱动的软件架构 下述三种思想,在linux的spi.iic.usb等复杂驱动里广泛使用.后面几节分别对这些思想进行详细说明. 思想1:驱动与设备分离 ...
- python爬虫基础11-selenium大全5/8-动作链
Selenium笔记(5)动作链 本文集链接:https://www.jianshu.com/nb/25338984 简介 一般来说我们与页面的交互可以使用Webelement的方法来进行点击等操作. ...
- 如何编写自己的C语言头文件
一些初学C语言的人,不知道头文件(*.h文件)原来还可以自己写的.只知道调用系统库函数时,要使用#include语句将某些头文件包含进去.其实,头文件跟.C文件一样,是可以自己写的.头文件是一种文本文 ...
- MySQL迁移至MariaDB
为什么要用MariaDB来代替MySQL MariaDB是MySQL社区开发的分支,也是一个增强型的替代品.它由MySQL前开发者们带头组织的基金会开发,使用起来和MySQL完全一样.自从Oracle ...
- zookeeper安装、配置、使用
[安装] wget http://www.apache.org/dist/zookeeper/zookeeper-3.3.6/zookeeper-3.3.6.tar.gz tar zxvf zooke ...