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(拒 ...
随机推荐
- 最大长度回文子串(Manacher's algorithm)
输出最大长度的回文子串. string longestPalindrome(string s) { int id, mx, i, j, len, maxlen; vector<char> ...
- javaweb基础(23)_jsp自定义标签
一.自定义标签的作用 自定义标签主要用于移除Jsp页面中的java代码. 二.自定义标签开发和使用 2.1.自定义标签开发步骤 1.编写一个实现Tag接口的Java类(标签处理器类) 1 packag ...
- iOS或Mac开发者应该记住的前缀
- 扫雷游戏 NOIP(入门)
题目描述: 扫雷游戏是一款十分经典的单机小游戏.它的精髓在于,通过已翻开格子所提示的周围格地雷数,来判断未翻开格子里是否是地雷. 现在给出n行m列的雷区中的地雷分布,要求计算出每个非地雷格的周围格地雷 ...
- Linux - 链接概念详解
1> Linux链接概念Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link).默认情况下,ln命令产生硬链接. [硬连接]硬连接指通过 ...
- Windows 10+Ubuntu双系统修复Ubuntu启动引导
U盘启动,联网 $ sudo su sudo add-apt add-apt-repository ppa:yannubuntu/boot-repair apt-get update apt-get ...
- python学习之判断和循环的使用
作为一个小白运维,工作中常常发现很多东西还是自动化的好一点,所以就想到的用python来编写脚本.当然,我肯定是不会的啦,哈哈哈~~~~所以啦,身为一个懒癌晚期的上班族不得不在闲余时间来好好学学pyt ...
- python入门:1-99所有数的和的等式
#!/usr/bin/env python # -*- coding:utf-8 -*- #1-99所有数的和的等式 #start(开始,译音:思达二测)sum(合计,译音:桑木)temp(临时雇员, ...
- linux 安装elasticsearch
一.检测是否已经安装的elasticsearch ps aux|grep elasticsearch. 二.下载elasticsearch.tar.gz并上传至服务器usr/local/文件夹下 三. ...
- js实现获取当前时间是本月第几周的方法
这篇文章主要介绍了js实现获取当前时间是本月第几周的方法,涉及javascript针对日期及时间的相关操作技巧,非常简单实用,需要的朋友可以参考下. 本文实例讲述了js实现获取当前时间是本月第几周的方 ...