HDU 5067-Harry And Dig Machine(DFS)
Harry And Dig Machine
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 435 Accepted Submission(s): 153
and even algorithm.
Dumbledore, the headmaster of Hogwarts, is planning to construct a new teaching building in his school. The area he selects can be considered as an n*m grid, some (but no more than ten) cells of which might contain stones. We should remove the stones there
in order to save place for the teaching building. However, the stones might be useful, so we just move them to the top-left cell. Taking it into account that Harry learned how to operate dig machine in Lanxiang School several years ago, Dumbledore decides
to let him do this job and wants it done as quickly as possible. Harry needs one unit time to move his dig machine from one cell to the adjacent one. Yet skilled as he is, it takes no time for him to move stones into or out of the dig machine, which is big
enough to carry infinite stones. Given Harry and his dig machine at the top-left cell in the beginning, if he wants to optimize his work, what is the minimal time Harry needs to finish it?
For each test case, there are two integers n and m.(1≤n,m≤50).
The next n line, each line contains m integer. The j-th number of ith line
a[i][j] means there are a[i][j] stones on the jth cell
of the ith line.( 0≤a[i][j]≤100 ,
and no more than 10 of a[i][j] will be positive integer).
3 3
0 0 0
0 100 0
0 0 0
2 2
1 1
1 1
4
4BC的b题。一開始写的dfs后来挂掉了。后来才知道自己傻逼了居然妄图想搜全图。 。题解给的是状压DP,蒟蒻还不会。 。由于题意是要让清掉非0的数字且步数最小,我们能够把图中非0的数字的坐标取出来爆搜这些点就能够了,注意用最优性剪一下。#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <list>
#define ll long long
using namespace std;
const int INF=1<<27;
const int maxn=1010;
int ans,num,n,m,a[55][55];
bool vis[12];
int tx[12],ty[12];
void dfs(int x,int y,int goal,int step)
{
//最优性剪枝。这个比較好想
if(step>ans)return ;
if(goal==num)
{
ans=min(ans,step+x-1+y-1);
return ;
}
for(int i=0;i<num;i++)
{
if(!vis[i])
{
vis[i]=1;
int st=step+abs(x-tx[i])+abs(y-ty[i]);
dfs(tx[i],ty[i],goal+1,st);
vis[i]=0;
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
num=0;
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
if(i==1&&j==1)continue;
if(a[i][j])
{
tx[num]=i;
ty[num++]=j;
}
}
ans=INF;
dfs(1,1,0,0);
printf("%d\n",ans);
}
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
HDU 5067-Harry And Dig Machine(DFS)的更多相关文章
- HDU 5067 Harry And Dig Machine(状压dp)
HDU 5067 Harry And Dig Machine 思路:因为点才10个,在加上一个起点,处理出每一个点之间的曼哈顿距离,然后用状压dp搞,状态表示为: dp[i][s],表示在i位置.走过 ...
- HDU 5067 Harry And Dig Machine(状压DP)(TSP问题)
题目地址:pid=5067">HDU 5067 经典的TSP旅行商问题模型. 状压DP. 先分别预处理出来每两个石子堆的距离.然后将题目转化成10个城市每一个城市至少经过一次的最短时间 ...
- hdu 5067 Harry And Dig Machine (状态压缩dp)
题目链接 bc上的一道题,刚开始想用这个方法做的,因为刚刚做了一个类似的题,但是想到这只是bc的第二题, 以为用bfs水一下就过去了,结果MLE了,因为bfs的队列里的状态太多了,耗内存太厉害. 题意 ...
- HDU 5067 Harry And Dig Machine:TSP(旅行商)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5067 题意: 给你一个n*m的地图,地图上标着对应位置的石子数.你从左上角出发,每次可以向上下左右四个 ...
- hdu 5067 Harry And Dig Machine
http://acm.hdu.edu.cn/showproblem.php?pid=5067 思路:问题可以转化成:从某一点出发,遍历网格上的一些点,每个点至少访问一次需要的最小时间是多少.这就是经典 ...
- hdu 1010 Tempter of the Bone(dfs)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- HDU 5113:Black And White(DFS)
题目链接 题意 给出一个n*m的图,现在有k种颜色让你对这个图每个格子染色,每种颜色最多可以使用col[i]次,问是否存在一种染色方案使得相邻格子的颜色不同. 思路 以为是构造题,结果是爆搜.对于每一 ...
- HDU 6060:RXD and dividing(DFS)
题目链接 题意 给出n个点,要把除1以外的点分成k个集合,然后对于每个集合要和1这个点一起求一个最小生成树,然后问这k个最小生成树的最大总和是多少. 思路 因为每个集合都包含1这个点,因此对于每个点都 ...
- HDU 2181 哈密顿绕行世界问题(DFS)
Problem Description 一个规则的实心十二面体,它的 20个顶点标出世界著名的20个城市,你从一个城市出发经过每个城市刚好一次后回到出发的城市. Input 前20行的第i行有3个数, ...
随机推荐
- [JavaScript]plupload多图片上传图片
var uploader = new plupload.Uploader({ //创建实例的构造方法 runtimes: 'html5,flash,silverlight,html4', ...
- nginx添加缓存
nginx的具体逻辑是什么样的? 分布式session spring session redis过滤器 有4种方案: 一直访问一台 //如果这台机器垮掉了,怎么办? session同步 序列化传输 / ...
- Windbg简单介绍
1.1 使用帮助 Windbg中的命令分为三种:基本命令.元命令和扩展命令.基本命令和元命令都是调试器自带的,元命令以" ."开头. 扩展命令是外部加入的,以"!&quo ...
- 如何删除Windows服务
删除的办法有两个: 办法一: 用sc.exe这个Windows命令 开始——运行——cmd.exe,然后输入sc就可以看到了.使用办法很简单: sc delete &q ...
- Swift - 42 - 类的基本使用
import Foundation /* 1.class表示类的关键字 2.class后面表示类名 3.类名后面的大括号内表示类的内部 */ /* 1.属性封装了set和get方法 2.方法里面封装了 ...
- 1、CentOS6.5系统安装及学习
1. CentOS6.5系统安装及学习 1.需要的工具,Vmware workstation12虚拟机,CentOS6.5 ISO镜像,选择的是32位系统. 2.下载CentOS6.5地址:htt ...
- regular expression (php函数)
1. 正则表达式是一种字符串搜索和匹配的工具 2. php中常用正则表达式函数 preg_match($pattern, $subject) preg_match_all($pattern, $sub ...
- 转:为什么需要htons(), ntohl(), ntohs(),htons() 函数
为什么需要htons(), ntohl(), ntohs(),htons() 函数: 在C/C++写网络程序的时候,往往会遇到字节的网络顺序和主机顺序的问题.这是就可能用到htons(), ntohl ...
- Oracle_OCP课程实验学习
Linux启动oracl.查看lsnrctl状态,然后启动监听start.sqlplus / as sysdba 启动数据库.conn sys/jxsrpv as sysdba .startup Ad ...
- spark1.1.0源码阅读-executor
1. executor上执行launchTask def launchTask( context: ExecutorBackend, taskId: Long, taskName: String, s ...