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(拒 ...
随机推荐
- 第一单元OO总结
- javaweb基础(11)_cookie的会话管理
一.会话的概念 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学曾 ...
- Silverlight日记:动态操作Grid
一,动态生成Grid public static Grid CreateGrid(List<T_METER> List) { var g = new Grid(); if (null == ...
- Bootstrap历练实例:语境色彩的面板
带语境色彩的面板 使用语境状态类 panel-primary.panel-success.panel-info.panel-warning.panel-danger,来设置带语境色彩的面板,实例如下: ...
- cocos2dx 通过jni调用安卓底层方法
cocos2dx通过封装JniHelper类来调用安卓api底层函数,该文件在cocos/platform/android/jni/JniHelper.h,使用方法如下: 打开eclipse,导入co ...
- Caesars Cipher-freecodecamp算法题目
Caesars Cipher(凯撒密码.移位密码) 要求 字母会按照指定的数量来做移位. 一个常见的案例就是ROT13密码,字母会移位13个位置.由'A' ↔ 'N', 'B' ↔ 'O',以此类推. ...
- pm2日志记录和日志分割
pm2日志记录和日志分割 pm2介绍 pm2是nodejs进程管理工具,现在基本是node生产服务器的标准选择,可以帮助我们实现node多进程服务,开启的多个实例自动实现负载均衡. 最重要的是保证no ...
- Vue源码探究-事件系统
Vue源码探究-事件系统 本篇代码位于vue/src/core/instance/events.js 紧跟着生命周期之后的就是继续初始化事件相关的属性和方法.整个事件系统的代码相对其他模块来说非常简短 ...
- 浅谈抓取网页数据(奉上Demo)
Demo源码 背景 曾经在公司做过一个比价系统,就是抓取其它网站上商品的价格并和自己公司的商品进行对应,然后展示出来,给pm提供一个定价的参考.后来,有同事的朋友在找工作的时候,猎头让其做一个抓取去哪 ...
- 《百词斩·象形9000》第一册(下) 符号Symbol 1
001-version n.版本:译文 This is the first version of the software #这是软件开发的第一个版本: 002-element n.成分:要素:元素: ...