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/ 这题我之前写没想到迭代加深,看了题解,然后学习了这种搜索(之前我写的某题也用过,,但是不懂专业名词 囧.) 迭代加深搜索就是限制 ...
随机推荐
- 图解 5 种 Join 连接及实战案例!(inner/ left/ right/ full/ cross)
Join 连接在日常开发用得比较多,但大家都搞清楚了它们的使用区别吗??一文带你上车~~ 内连接 inner join 内连接是基于连接谓词将俩张表(如A和B)的列组合到一起产生新的结果表,在表中存在 ...
- 02-python 学习第二天
今天学习了以下几个方面的内容,虽然部分内容不能理解,跟着老师写出了代码. 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 程序练习1:购物车程序 请闭眼写出以下程序. 程序: ...
- php中heredoc使用方法
Heredoc技术,在正规的PHP文档中和技术书籍中一般没有详细讲述,只是提到了这是一种Perl风格的字符串输出技术.但是现在的一些论坛程序,和部分文章系统,都巧妙的使用heredoc技术,来部分的实 ...
- LVS/Nginx/HAProxy负载均衡器的对比分析
转自:http://www.blogjava.net/ivanwan/archive/2013/12/25/408014.html LVS的特点是: 抗负载能力强.是工作在网络4层之上仅作分发之用,没 ...
- 2019-11-12-浅谈-Windows-桌面端触摸架构演进
title author date CreateTime categories 浅谈 Windows 桌面端触摸架构演进 lindexi 2019-11-12 14:37:31 +0800 2019- ...
- python3 使用aria2下载的一个脚本
import requests import time ariaurl="http://localhost:6800/jsonrpc" dlurl="http://xxx ...
- Ansible-playbook简单应用的几个实例
①ansible-playbook的循环: 重复执行某任务:对迭代项的引用,固定变量名为“item”,而后要在task中使用with_items给定要迭代的元素列表,列表方法:字符串/字典(类似jso ...
- day2-元组、字典、文件操作
学习内容: 1. 元组操作 2. 字典操作 3. 文件操作 4. 深浅copy 1. 元组操作: 元组和列表非常相似,只不过元组不能在原处修改(它是不可变的),并且通常写成圆括号中的一系列项. # 元 ...
- 位运算 - 左移右移运算符 >>, <<, >>>
1-左移运算符m<<n,表示把m左移n位.左移n位的时候,最左边的n位数将被丢弃,同时在最右边补上n个0.例如: 00001010<<2 = 00101000 10001010 ...
- MySQL其他和备份
目录 事务 存储引擎 InnoDB存储引擎 数据存储形式 锁的粒度 事务 数据的存储特点 MyISAM存储引擎 数据存储形式 锁的粒度 事务 数据的存储特点 其他 对比与选择 视图 触发器 存储过程 ...