TZOJ 4267 An Easy Puz(深搜)
描述
Wddpdh find an interesting mini-game in the BBS of WHU, called “An easy PUZ”. It’s a 6 * 6 chess board and each cell has a number in the range of 0 and 3(it can be 0, 1, 2 or 3). Each time you can choose a number A(i, j) in i-th row and j-th column, then the number A(i, j) and the numbers around it (A(i-1, j), A(i+1, j),A(i, j-1),A(i, j+1), sometimes there may just be 2 or 3 numbers.) will minus 1 (3 to 2, 2 to 1, 1 to 0, 0 to 3). You can do it finite times. The goal is to make all numbers become 0. Wddpdh now come up with an extended problem about it. He will give you a number N (3 <= N <= 6) indicate the size of the board. You should tell him the minimum steps to reach the goal.
输入
The input consists of multiple test cases. For each test case, it contains a positive integer N(3 <= n <= 6). N lines follow, each line contains N columns indicating the each number in the chess board.
输出
For each test case, output minimum steps to reach the goal. If you can’t reach the goal, output -1 instead.
样例输入
3
1 1 0
1 0 1
0 1 1
3
2 3 1
2 2 1
0 1 0
样例输出
2
3
题意
给你个N*N的矩阵,每次操作选择(i,j),然后(i,j)和周围4格都-1,3->2->1->0->3。问最少操作次数。
题解
爆搜4^36肯定不行,考虑优化。
可以发现,如果确定第一行每个位置的变化次数,那么下一行的变化次数也就确定了,最后判断是否每个数都变成0。
总时间复杂度O(4^6*36)。
代码
#include<bits/stdc++.h>
using namespace std;
int n,a[][],c[][],b[],minn;
int dx[]={,,,,-};
int dy[]={,-,,,};
void dfs(int p,int ans)
{
if(p==n+)
{
for(int i=;i<=n;i++)for(int j=;j<=n;j++)c[i][j]=a[i][j];
for(int j=;j<=n;j++)
for(int k=;k<;k++)
{
int xx=+dx[k];
int yy=j+dy[k];
if(xx>=&&xx<=n&&yy>=&&yy<=n)c[xx][yy]=(c[xx][yy]-b[j]+)%;
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(c[i-][j])
{
ans+=c[i-][j];
for(int k=;k<;k++)
{
int xx=i+dx[k];
int yy=j+dy[k];
if(xx>=&&xx<=n&&yy>=&&yy<=n)c[xx][yy]=(c[xx][yy]-c[i-][j]+)%;
}
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(c[i][j])
goto e;
minn=min(minn,ans);
e:return;
}
for(int i=;i<;i++)
{
b[p]=i;
dfs(p+,ans+i);
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)for(int j=;j<=n;j++)scanf("%d",&a[i][j]);
minn=1e9;
dfs(,);
printf("%d\n",minn==1e9?-:minn);
}
return ;
}
TZOJ 4267 An Easy Puz(深搜)的更多相关文章
- TZOJ 3305 Hero In Maze II(深搜)
描述 500年前,Jesse是我国最卓越的剑客.他英俊潇洒,而且机智过人^_^.突然有一天,Jesse心爱的公主被魔王困在了一个巨大的迷宫中.Jesse听说这个消息已经是两天以后了,他急忙赶到迷宫,开 ...
- HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?
这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others) ...
- 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。
利用TreeView控件加载文件,必须遍历处所有的文件和文件夹. 深搜算法用到了递归. using System; using System.Collections.Generic; using Sy ...
- 2016弱校联盟十一专场10.3---Similarity of Subtrees(深搜+hash、映射)
题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52310 problem description Define the depth of a ...
- 2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)
题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52305 problem description In ICPCCamp, there ar ...
- 2015暑假多校联合---Cake(深搜)
题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...
- 深搜+回溯 POJ 2676 Sudoku
POJ 2676 Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17627 Accepted: 8538 ...
- 深搜+DP剪枝 codevs 1047 邮票面值设计
codevs 1047 邮票面值设计 1999年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description ...
- 【wikioi】1049 棋盘染色(迭代深搜)
http://www.wikioi.com/problem/1049/ 这题我之前写没想到迭代加深,看了题解,然后学习了这种搜索(之前我写的某题也用过,,但是不懂专业名词 囧.) 迭代加深搜索就是限制 ...
随机推荐
- <scrapy爬虫>scrapy命令行操作
1.mysql数据库 2.mongoDB数据库 3.redis数据库 1.创建项目 scrapy startproject myproject cd myproject 2.创建爬虫 scrapy g ...
- OpenGL键盘交互响应事件
GLUT允许我们编写程序,在里面加入键盘输入控制,包括了普通键,和其他特殊键(如F1,UP).在这一章里我们将学习如何去检测哪个键被按下,可以从GLUT里得到些什么信息,和如何处理键盘输入. 处理 ...
- JSP四个域对象的应用场景
request 如果客户向服务器发请求,产生的数据,用户看完就没用了,像这样的数据就存在request域 比如新闻数据,属于用户看完就没用的 session 如果客户向服务器发请求,产生的数据,用户用 ...
- Python 的一些方法
有关 Python 内编写类的各种技巧和方法(构建和初始化.重载操作符.类描述.属性访问控制.自定义序列.反射机制.可调用对象.上下文管理.构建描述符对象.Pickling). 你可以把它当作一个教程 ...
- Ubuntu安装Windows软件
https://www.cnblogs.com/chendeqiang/p/10177530.html Windows系列软件 安装Deepin封装好的框架 git clone https://git ...
- LUOGU P2580 于是他错误的点名开始了(trie树)
传送门 解题思路 trie树模板
- EF(Entity Framwork)结构
初次接触EF,看了一些资料,将自己对EF结构的理解记录如下: EF的核心是EDM----实体数据模型(.edmx).它由三部分组成:概念模型(.csdl文件).存储模型(.ssdl文件).映射规范(. ...
- 设定计算属性setter
<!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...
- Windows下运行Tomcat闪退问题
直接双击startup.bat后闪退,可能的原因是tomcat的配置没有完全正确 完整的tomcat的配置应该配置: 1.JAVA_HOME 2.CATALINA_HOME 3.Path:%CATAL ...
- Python学习day09 - Python进阶(3)
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...