hdu 5067(暴力搜索)
Harry And Dig Machine
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 852 Accepted Submission(s): 348
we all know, Harry Porter learns magic at Hogwarts School. However,
learning magical knowledge alone is insufficient to become a great
magician. Sometimes, Harry also has to gain knowledge from other certain
subjects, such as language, mathematics, English, 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).
0 0 0
0 100 0
0 0 0
2 2
1 1
1 1
4
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int INF = ;
struct Point
{
int x,y;
} p[];
int n,m,res,cnt;
int graph[][];
bool vis[];
void dfs(int x,int dept,int ans)
{
vis[x] = true;
if(ans>res) return ; ///剪枝
if(dept==cnt-)
{
ans += graph[x][]; ///走回去
res = min(res,ans);
return ;
}
for(int i=; i<cnt; i++)
{
if(!vis[i]&&graph[x][i]!=INF&&x!=i)
{
dfs(i,dept+,ans+graph[x][i]);
vis[i] = false;
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
cnt = ;
int v;
for(int i=; i<n; i++)
{
for(int j=; j<m; j++)
{
scanf("%d",&v);
if(i==&&j==)
{
p[cnt].x = i,p[cnt++].y=j;
}
else if(v)
{
p[cnt].x = i,p[cnt++].y=j;
}
}
}
for(int i=; i<cnt; i++)
{
for(int j=; j<cnt; j++)
{
if(i==j)graph[i][j] = ;
else graph[i][j] =INF;
}
}
for(int i=; i<cnt; i++)
{
for(int j=i+; j<cnt; j++)
{
graph[i][j] = graph[j][i] = abs(p[i].x-p[j].x)+abs(p[i].y-p[j].y);
}
}
/*for(int i=0; i<cnt; i++)
{
for(int j=0; j<cnt; j++)
{
printf("%d ",graph[i][j]);
}
printf("\n");
}*/
memset(vis,false,sizeof(vis));
res = INF;
vis[] = true;
dfs(,,);
printf("%d\n",res);
}
return ;
}
hdu 5067(暴力搜索)的更多相关文章
- hdu 4740 The Donkey of Gui Zhou(暴力搜索)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4740 [题意]: 森林里有一只驴和一只老虎,驴和老虎互相从来都没有见过,各自自己走过的地方不能走第二次 ...
- HDU 3131 One…Two…Five! (暴力搜索)
题目链接:pid=3131">HDU 3131 One-Two-Five! (暴力搜索) 题意:给出一串数字,要求用加,减,乘,除(5/2=2)连接(计算无优先级:5+3*6=8*6= ...
- hdu 1427 速算24点 dfs暴力搜索
速算24点 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem De ...
- ACM 暴力搜索题 题目整理
UVa 129 Krypton Factor 注意输出格式,比较坑爹. 每次要进行处理去掉容易的串,统计困难串的个数. #include<iostream> #include<vec ...
- HDU 5067 Harry And Dig Machine(状压DP)(TSP问题)
题目地址:pid=5067">HDU 5067 经典的TSP旅行商问题模型. 状压DP. 先分别预处理出来每两个石子堆的距离.然后将题目转化成10个城市每一个城市至少经过一次的最短时间 ...
- 随手练——洛谷-P1151(枚举与暴力搜索)
枚举 #include <iostream> using namespace std; int main() { ; cin >> k; ; i < ; i++) { ) ...
- HDU - 5067 / HDU - 5418 TSP
集合表示多用[0,n)表示方法 HDU - 5067 经典TSP,每个顶点恰经过一次最优 #include<bits/stdc++.h> #define rep(i,j,k) for(in ...
- 枚举进程——暴力搜索内存(Ring0)
上面说过了隐藏进程,这篇博客我们就简单描述一下暴力搜索进程. 一个进程要运行,必然会加载到内存中,断链隐藏进程只是把EPROCESS从链表上摘除了,但它还是驻留在内存中的.这样我们就有了找到它的方法. ...
- HDU 5067 Harry And Dig Machine(状压dp)
HDU 5067 Harry And Dig Machine 思路:因为点才10个,在加上一个起点,处理出每一个点之间的曼哈顿距离,然后用状压dp搞,状态表示为: dp[i][s],表示在i位置.走过 ...
随机推荐
- c语言的左移、右移
先说左移,左移就是把一个数的所有位都向左移动若干位,在C中用<<运算符.例如: int i = 1; i = i << 2; //把i里的值左移2位 也就是说,1的2进制是0 ...
- 《Cracking the Coding Interview》——第18章:难题——题目13
2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目10
2014-03-20 04:15 题目:你有n个盒子,用这n个盒子堆成一个塔,要求下面的盒子必须在长宽高上都严格大于上面的.如果你不能旋转盒子变换长宽高,这座塔最高能堆多高? 解法:首先将n个盒子按照 ...
- 【Linear Regression】林轩田机器学习基石
这一节开始讲基础的Linear Regression算法. (1)Linear Regression的假设空间变成了实数域 (2)Linear Regression的目标是找到使得残差更小的分割线(超 ...
- gulp批量添加版本号
要实现的理想效果: "/css/style.css" => "/dist/css/style.css?v=1d87bebe""/js/scrip ...
- chrome flash插件改为自动运行
1.情景展示 国内网页视频播放大部分用的都是flash插件,每次都要将默认改为允许,才能正常播放 能不能让flash插件在所有的网站上都能自动运行呢? 2.解决方案 第一步:打开fla ...
- drf解决跨域问题 使用 django-corse-headers扩展
跨域CORS 使用django-corse-headers扩展 安装 pip install django-cors-headers 添加应用 INSTALLED_APPS = ( ... 'cors ...
- Java 以及JEE环境快速搭建
吐槽一下 博主最近找了一个Java Development的实习,加上上个月末的考试周,所以很久没有更新博客. 上了一周的班,还没有在熟悉项目的阶段. 感想:哇,读别人的代码是一件很费力的事情啊!!! ...
- 《大道至简》第一章 编程的精义 java伪代码形式
愚公.这位名家身上,浓缩了项目组织者.团队经理.编程人员.技术分析师等众多角色的优秀素质. 愚公移山事件分析: 原始需求:惩山北之塞,出入之迂 项目沟通方式:聚室而某曰 项目目标:毕力平险,指通豫南, ...
- JavaScript中的parseInt和Number函数
函数作用: parseInt将字符串(String)类型转为整数类型. Number() 函数把对象(Object)的值转换为数字. 语法不同: parseInt(string, [radix]) s ...