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

 
DFS搜索每次染的颜色。无用状态很多,所以需要IDA星算法
估价:统计场上还剩下多少颜色,至少要染这么多次才能出解,如果预估次数加上当前次数超过了当前迭代加深的深度限制,剪枝。
染色:用vis数组标记当前左上角连通块周边的块,每次染色只从这些块儿开始处理。
 
经历了几次T以后开始标准代码比对,把DFS(当前深度,限制深度)改成DFS(当前深度),函数外存限制;把cpy数组从函数外面拖到里面,就A了。
玄学……?
 

SilverN at 2018.6.11
  神tm玄学,你把cpy设成全局变量,每层搜索共用一个cpy数组,能过样例都是奇迹
  真丢人(雾)

 
 /*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 ;
}

最新文章

  1. ubuntu 下安装mysql,以及配置远程登录
  2. CF 435B Pasha Maximizes(贪心)
  3. lightoj1085 线段树+dp
  4. Linux下通过shell脚本创建账户
  5. SVN资料库转移-----dump和load
  6. 再次回首 TCP Socket服务器编程
  7. UVA 796 Critical Links(无向图求桥)
  8. (转)xcode报Could not find a storyboard named...错误的解决办法
  9. adb安装和卸载apk的方式
  10. linux中萌翻了的cowsay命令
  11. Extjs 6 MVC开发模式(一)
  12. 深入浅出MS06-040
  13. jsp内置对象-session对象
  14. 线段树(区间树)之区间染色和4n推导过程
  15. spring-mvc(基础)
  16. Impl模式实现之注意内联
  17. Cublas矩阵加速运算
  18. JSON 字符串转换为JavaScript 对象.JSON.parse()和JSON.stringify()
  19. hdu2824 The Euler function(欧拉函数个数)
  20. 源码编译安装mysql-boost-5.7.16.tar.gz报错分析处理

热门文章

  1. java基础—接口概念
  2. 采用maven 对tomcat 进行自动部署
  3. TCP/IP协议头部结构体
  4. VueJS坎坷之路222--vue cli 3.0引入静态文件
  5. VueX源码分析(1)
  6. [vijos]P1514 天才的记忆
  7. NOIP模拟赛 密室逃脱
  8. shopnc路由功能分析
  9. 地理位置编码geohash学习笔记
  10. 【nginx】nginx.sh nginx 安装脚本