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(拒 ...
随机推荐
- Silverlight日记:动态操作Grid
一,动态生成Grid public static Grid CreateGrid(List<T_METER> List) { var g = new Grid(); if (null == ...
- java基础——快速排序
今天又把以前学的快速排序拿出来回忆一下 高快省的排序算法 有没有既不浪费空间又可以快一点的排序算法呢?那就是“快速排序”啦!光听这个名字是不是就觉得很高端呢. 假设我们现在对“6 1 2 7 9 3 ...
- SQL学习总结笔记
SQL语句的效率不仅是sql语句的设计还有一些其他的原因比如网络 .是否有视图.是否有索引等等.这里主要描述的是我个人对于sql设计方面优化的一些见解: 首先要说明一下的是数据库SQL解析顺序: (1 ...
- c内置数据类型
参考 C与指针 第三章 类型 类型标识符 字节 表示数值范围 备注 整型 [signed] int 2* -32768~32767 -2^15 ~ (2^15 -1) 无符号整型 unsigned [ ...
- loj6485 LJJ 学二项式定理
题目描述: loj 题解: 单位根反演. $[n|x]=\frac{1}{n} \sum _{i=0}^{n-1} (ω_n^x)^i$ 证明?显然啊,要么停在$(1,0)$要么转一圈. 所以说题目要 ...
- python之路-双下方法
双下方法 定义: 双下方法是特殊方法,他是解释器提供的,由双下线加方法名加双下划线 __方法名__具有特殊意义的方法 双下方法主要是Python源码程序员使用的,元编程 我们在开发中尽量不要使用双下方 ...
- python-安装及配置环境变量
1.python安装十分简单,直接下载与自己电脑位数匹配的python安装包进行安装即可. 这里提供python27的安装包供大家参考. win-32位: 链接: https://pan.baidu. ...
- Vue样式绑定、事件绑定
1.样式绑定 1.1class类标签绑定 <p :class="对象"> <p :class="数组"> <p :class=&q ...
- [python 测试框架学习篇] 分享 uiautomator测试框架
uiautomator测试框架 :https://testerhome.com/topics/4194
- Codeforces Round #459 (Div. 2)
A. Eleven time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...