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位置.走过 ...
随机推荐
- 线性表(List)
1.什么是线性表(List)? 零个或多个数据元素的有限序列. (1)元素之间是有序的. (2)线性表强调是有限的. 2.线性表有哪些操作? (1)线性表的创建和初始化,InitList (2)判空, ...
- 2.ifconfig
转载http://www.cnblogs.com/peida/archive/2013/02/27/2934525.html 许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口 ...
- Hyper-V中的Linux无法配置网络地址的解决办法
一周碰到2次在Hyper-V 2012中安装了Linux,也安装了IC 3.4.但是却无法配置IP地址的问题.因此造成很多不便,因此找机会把这个原因和解决办法进行了尝试. 这过程中感谢同事的提示,让我 ...
- 《Cracking the Coding Interview》——第3章:栈和队列——题目6
2014-03-19 03:01 题目:给定一个栈,设计一个算法,在只使用栈操作的情况下将其排序.你可以额外用一个栈.排序完成后,最大元素在栈顶. 解法:我在草稿纸上试了试{1,4,2,3}之类的小例 ...
- MySQL增强半同步几个重要参数搭配的测试
Preface Semi-synchronous replication is supported since MySQL 5.5 and then enhanced graduall ...
- Hastable和Dictionary以及ArrayList和(List,LinkedList,数组)的区别
Hastable和Dictionary的区别:(键值对) 1:单线程程序中推荐使用 Dictionary, 有泛型优势, 且读取速度较快, 容量利用更充分. 2:多线程程序中推荐使用 Hashtabl ...
- Python——初识Python
本篇主要内容: • Python的特点 • Python的种类 • Python的编码 • Python的安装环境推荐 • Python的基础用法:输入输出,算术运算符,逻辑运算符,基本程序结构语法 ...
- JavaScript里面的条件、循环语句以及异常处理
1.JavaScript里面条件语句主要有两种形式 if(条件){ ... }else if(条件){ ... }else{ ...} switch(变量名): case 值1://如果变量名为值1, ...
- 九 DIP 依赖倒置原则
首先看定义: 1.高层模块不依赖于低层模块,两者都应该依赖于抽象层 2.抽象不能依赖于细节,细节必须依赖于抽象 首先,模块是个抽象的概念,可以大到一个系统中的子系统作为一个模块,也可以是某个子系统中的 ...
- POJ3345 Bribing FIPA 【背包类树形dp】
题目链接 POJ 题解 背包树形dp板题 就是读入有点无聊,浪费了很多青春 #include<iostream> #include<cstdio> #include<cm ...