lightoj1057 - Collecting Gold (tsp问题)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1057
题目大意:在二维矩阵中,给你一个起点和至多15个的目标点。要你求出从起点出发经过完所有的点后回到起点的最短路径值。每个点一步可以向 八个方向走。
算法思路:一看就觉得是tsp,用状态压缩。而任意两点的距离就是相应横纵坐标差的较大值。具体看代码。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; const int maxn = <<;
const int INF = 0x3f3f3f3f; int dp[][maxn]; //dp[i][S] 表示从起点走完S中的点到达i的最小距离。
int m,n;
struct Point{
int x,y;
Point(int x=,int y=): x(x), y(y) {}
}P[],startP;
int goldnum;
int dist[][]; inline int myfabs(int a){
if(a < ) a = -a;
return a;
} int main()
{
//freopen("E:\\acm\\input.txt","r",stdin);
int T;
cin>>T; for(int cas=; cas<=T; cas++)
{
cin>>m>>n;
char s[];
goldnum = ; for(int i=; i<m; i++)
{
scanf("%s",s);
for(int j=; j<n; j++)
{
if(s[j] == 'x')
{
startP = Point(i,j);
}
else if(s[j] == 'g')
{
P[goldnum++] = Point(i,j);
}
}
} //存点。
memset(dist,0x3f,sizeof(dist));
for(int i=; i<goldnum; i++)
for(int j=i+; j<goldnum; j++)
{
dist[i][j] = dist[j][i] = max(myfabs(P[j].x-P[i].x),myfabs(P[j].y-P[i].y)); //任意两个gold的距离
}
for(int i=; i<goldnum; i++) //把第goldnum看成起点。求起点与任意一个gold的距离。
{
dist[goldnum][i] = dist[i][goldnum] = max(myfabs(startP.x-P[i].x),myfabs(startP.y-P[i].y));
} int All = (<<goldnum) - ;
memset(dp,0x3f,sizeof(dp));
dp[][];
for(int i=; i<goldnum; i++)
{
dp[i][<<i] = dist[goldnum][i];
}
for(int S=; S<=All; S++)
{
for(int i=; i<goldnum; i++)
{
if(S & (<<i)) //S中包含了i这点。
{
for(int j=; j<goldnum; j++)
{
if(S & (<<j)) continue; //要取不在S中的点j
dp[j][S|(<<j)] = min(dp[j][S|(<<j)],dp[i][S] + dist[i][j]);
}
}
}
}
int ans = INF;
for(int i=; i<goldnum; i++)
ans =min(ans,dp[i][All] + dist[i][goldnum]); if(ans == INF) ans = ; //这个坑,WA了几次
printf("Case %d: %d\n",cas,ans);
}
}
/**
Keep in mind that, when you receive a WA and want to find "critical" cases,
it's often useful to start by thinking of cases that go to either extreme
of the specifications (either the largest or lowest numbers possible).
It doesn't take a lot of practice to get in a frame of mind in which,
when you read something like "There will be exactly one 'x' in the city
and at most 15 gold positions", you almost immediately make the connection to
think of a test case with either 0 or 15 gold and try that.
**/
lightoj1057 - Collecting Gold (tsp问题)的更多相关文章
- LightOJ1057 Collecting Gold(状压DP)
这道题可以想到几点: 整个行程可以看作一次次的行走,每次行走都是用最短的路程从某一非空点到达另外一非空点: 两点间最少的步数是二者x和y坐标差的最大值: 返回原点这个过程,肯定是取完最后一个黄金后直接 ...
- 1057 - Collecting Gold
1057 - Collecting Gold PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB ...
- 1057 - Collecting Gold (状态压缩DP)
题目大意: 给你一个矩阵,'x'是你的起始位置, 'g'是宝藏的位置,问最少多少步可以把所有的宝藏取完,并且最后返回起始位置. 注意:没有宝藏的时候输出 0 =================== ...
- lightoj 1057 - Collecting Gold(状压dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1057 题解:看似有点下记忆话搜索但是由于他是能走8个方向的也就是说两点的距离其 ...
- LeetCode 1219. Path with Maximum Gold
原题链接在这里:https://leetcode.com/problems/path-with-maximum-gold/ 题目: In a gold mine grid of size m * n, ...
- 【leetcode】1219. Path with Maximum Gold
题目如下: In a gold mine grid of size m * n, each cell in this mine has an integer representing the amou ...
- POJ2096 Collecting Bugs
Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 5090 Accepted: 2529 Case Time Limit: ...
- poj3311 TSP经典状压dp(Traveling Saleman Problem)
题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...
- 【poj2096】Collecting Bugs
题目描述 Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other materia ...
随机推荐
- xml处理相关文章收藏
XPath语法 在C#中使用XPath示例:http://blog.csdn.net/yukaizhao/article/details/6630613 .Net那点事儿系列:C#操作Xml:通过Xm ...
- C语言开发环境配置
链接:http://pan.baidu.com/s/1qWkpD72 密码:zhig 将解压包直接解压放在C盘下. 右击我的电脑,点属性—>高级—>环境变量然后在PATH里加入C:\Min ...
- CSS 链接
不同的链接可以有不同的样式. 链接样式 链接的样式,可以用任何CSS属性(如颜色,字体,背景等). 特别的链接,可以有不同的样式,这取决于他们是什么状态. 这四个链接状态是: a:link - 正常, ...
- max取得数组的最大值
var arr = [1,2,3,4,5,2,2,4,52,5,6,5,4,4]; var maxNum = Math.max.apply(Math,arr); var maxIndex = arr. ...
- Open judge 06月度开销
06:月度开销 总时间限制: 1000ms 内存限制: 65536kB 传送门 描述 农夫约翰是一个精明的会计师.他意识到自己可能没有足够的钱来维持农场的运转了.他计算出并记录下了接下来 N (1 ≤ ...
- C++ xmmp IM开发笔记(一)
XMMP C++库采用 gloox (client) 下载地址:http://camaya.net/download/gloox-1.0.11.tar.bz2 glooxd (server) 下载地址 ...
- 配置处理结果result
Action处理完用户请求后返回一个字符串,整个字符串就是一个逻辑视图名. 除此之外,struts2还支持多种结果映射,struts2将结果转为实际资源时,不仅可以是JSP视图资源,也可以是FreeM ...
- html引入外部的jswenjian
首先看一下引入的方式是否正确 <script type='text/javascript' src='xx.js'></script> 其次看引入的路径是否正确 如果引入了第三 ...
- js如何获取一个月的天数 data javascript
js如何获取一个月的天数 function days(year,month){ var dayCount; now = new Date(year,month, 0); dayCount = now. ...
- php 执行linux 命令函数
php的内置函数exec,system都可以调用系统命令(shell命令),当然还有passthru,escapeshellcmd等函数. 在很多时候利用php的exec,system等函数调用系统命 ...