POJ4007 Flood-it!

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 337   Accepted: 123

Description

Flood-it is a fascinating puzzle game on Google+ platform. The game interface is like follows: 

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 input contains no more than 20 test cases. For each test case, the first line contains a single integer N (2<=N<=8) indicating the size of game board.

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

For each test case, output a single integer representing the minimal number of steps to win the game.

Sample Input

2
0 0
0 0
3
0 1 2
1 1 2
2 2 1
0

Sample Output

0
3

Source

Fuzhou 2011

DFS搜索每次染的颜色。无用状态很多,所以需要IDA星算法

估价:统计场上还剩下多少颜色,至少要染这么多次才能出解,如果预估次数加上当前次数超过了当前迭代加深的深度限制,剪枝。

染色:用vis数组标记当前左上角连通块周边的块,每次染色只从这些块儿开始处理。

经历了几次T以后开始标准代码比对,把DFS(当前深度,限制深度)改成DFS(当前深度),函数外存限制;把cpy数组从函数外面拖到里面,就A了。

玄学……?

 1 /*by SilverN*/
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 using namespace std;
 8 const int mxn=10;
 9 int mx[5]={0,1,0,-1,0};
10 int my[5]={0,0,1,0,-1};
11 int mp[mxn][mxn];
12 int vis[mxn][mxn];
13 int n;
14 //估价函数
15 bool cvis[6];
16 int pre(){
17     int res=0;
18     memset(cvis,0,sizeof cvis);
19     for(int i=1;i<=n;i++)
20      for(int j=1;j<=n;j++){
21          if(vis[i][j]!=1 && !cvis[mp[i][j]]){
22              cvis[mp[i][j]]=1;  res++;
23          }
24      }
25     return res;
26 }
27 //方块染色
28 void change(int x,int y,int c){
29     vis[x][y]=1;//和左上角同色
30     int i,j;
31     for(i=1;i<=4;i++){
32         int nx=x+mx[i],ny=y+my[i];
33         if(nx<1 || nx>n || ny<1 || ny>n)continue;//边界判断
34         if(vis[nx][ny]==1)continue;//访问判断
35         if(mp[nx][ny]==c)change(nx,ny,c);
36         else vis[nx][ny]=2;
37     }
38     return;
39 }
40 bool solve(int color){//尝试染对应颜色
41     bool flag=0;
42     for(int i=1;i<=n;i++)
43      for(int j=1;j<=n;j++){
44          if(mp[i][j]==color && vis[i][j]==2 ){
45              change(i,j,color);
46              flag=1;//自带剪枝:如果染这个颜色不能扩大联通范围,就不染
47          }
48      }
49     return flag;
50 }
51 int limit;
52 bool DFS(int now){
53     if(now==limit)return (pre()==0);
54     if(now+pre()>limit)return 0;
55     int i,j,k;
56     for(k=0;k<=5;k++){
57         int cpy[9][9];
58         for(int i=1;i<=n;i++)
59          for(int j=1;j<=n;j++){
60              cpy[i][j]=vis[i][j];
61          }//保存状态
62         if(!solve(k))continue;
63         if(DFS(now+1))return 1;
64         for(int i=1;i<=n;i++)
65          for(int j=1;j<=n;j++){
66              vis[i][j]=cpy[i][j];
67          }//回溯
68     }
69     return 0;
70 }
71 int main(){
72     int i,j;
73     while(scanf("%d",&n) && n){
74         memset(vis,0,sizeof vis);
75         for(i=1;i<=n;i++)
76          for(j=1;j<=n;j++)
77           scanf("%d",&mp[i][j]);
78         change(1,1,mp[1][1]);
79         for(limit=pre();limit;limit++){//迭代加深
80             if(DFS(0))break;
81         }
82         printf("%d\n",limit);
83     }
84     return 0;
85 }
时间: 2024-10-07 04:44:21

POJ4007 Flood-it!的相关文章

局域网安全-MAC Flood/Spoof

原文发表于:2010-09-22 转载至cu于:2012-07-21 很早之前就看过秦柯讲的局域网安全的视频.但是看了之后在实际工作当中很少用到(指我个人的工作环境中,惭愧啊-),时间长了,好多技术细节的东西就忘记了.这段时间再看看,看的同时会做一下笔记,既能加深印象也方便以后查找. 局域网安全的mac flood/spoof 攻击 Unknown unicast flooding: 交换机收到单播包,但在cam表没有目的mac时会对广播域除入口外的所有端口泛洪,这样会导致非目的mac的终端截获

ddos 攻击 值 DNS Query Flood

作为互联网最基础.最核心的服务,DNS自然也是DDoS攻击的重要目标之一.打垮DNS服务能够间接打垮一家公司的全部业务,或者打垮一个地区的网络服务.前些时候风头正盛的黑客组织anonymous也曾经宣布要攻击全球互联网的13台根DNS服务器,不过最终没有得手. UDP攻击是最容易发起海量流量的攻击手段,而且源IP随机伪造难以追查.但过滤比较容易,因为大多数IP并不提供UDP服务,直接丢弃UDP流量即可.所以现在纯粹的UDP流量攻击比较少见了,取而代之的是UDP协议承载的DNS Query Flo

如何防御syn flood的一些思路!

厦门-志君同学21期群里疑问? syn flood是否无法防御 刚看到群里同学问问题,我还在讲课,利用间隙简单给大家点思路吧. 老男孩简单答疑如下: 1.先了解什么是SYN Flood? SYN Flood是一种DoS(拒绝服务攻击)与DDoS(分布式拒绝服务攻击)的方式之一,是一种利用TCP协议缺陷,发送大量伪造的TCP连接请求,从而使得被攻击方资源耗尽的攻击方式. 2.再了解tcp/ip的三次握手和四次断开过程原理 04-老男孩linux技术分享-OSI七层模型及协议-包封装解封装详解htt

扯谈网络编程之Tcp SYN flood洪水攻击

简介 TCP协议要经过三次握手才能建立连接: (from wiki) 于是出现了对于握手过程进行的攻击.攻击者发送大量的FIN包,服务器回应(SYN+ACK)包,但是攻击者不回应ACK包,这样的话,服务器不知道(SYN+ACK)是否发送成功,默认情况下会重试5次(tcp_syn_retries).这样的话,对于服务器的内存,带宽都有很大的消耗.攻击者如果处于公网,可以伪造IP的话,对于服务器就很难根据IP来判断攻击者,给防护带来很大的困难. 攻与防 攻击者角度 从攻击者的角度来看,有两个地方可以

SYN Flood攻击及防御方法 (转)

原文连接:http://blog.csdn.net/bill_lee_sh_cn/article/details/6065704 一.为什么Syn Flood会造成危害      这要从操作系统的TCP/IP协议栈的实现说起.当开放了一个TCP端口后,该端口就处于Listening状态,不停地监视发到该端口的Syn报文,一 旦接收到Client发来的Syn报文,就需要为该请求分配一个TCB(Transmission Control Block),通常一个TCB至少需要280个字节,在某些操作系统

NOJ 1121 Message Flood (Trie树 或者 map)

Message Flood 时间限制(普通/Java):2000MS/6000MS         运行内存限制:65536KByte 总提交:399          测试通过:105 题目描述 Well, how do you feel about mobile phone? Your answer would probably be something like that "It's so convenient and benefits people a lot". Howeve

SYN Flood应如何应对

1 什么是SYN Flood攻击 在TCP三次握手时,服务器接收客户端的SYN请求,操作系统将为该请求分配一个TCP(Transmission Control Block),服务器返回一个SYN/ACK请求,并将处于SYN_RCVD状态(半开连接状态). 从以上过程可以看到,如果恶意的向某个服务器端口发送大量的SYN包,则可以使服务器打开大量的半开连接,分配TCB,从而消耗大量的服务器资源,同时也使得正常的连接请求无法被相应.而攻击发起方的资源消耗相比较可忽略不计. SYN Flood是当前最流

Message Flood(set)

Message Flood Time Limit: 1500MS Memory limit: 65536K 题目描述 Well, how do you feel about mobile phone? Your answer would probably be something like that "It's so convenient and benefits people a lot". However, If you ask Merlin this question on th

STL 之map解决 Message Flood(原字典树问题)

Message Flood Time Limit:1500MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description Well, how do you feel about mobile phone? Your answer would probably be something like that "It's so convenient and benefits people a lot".