题目链接: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问题)的更多相关文章

  1. LightOJ1057 Collecting Gold(状压DP)

    这道题可以想到几点: 整个行程可以看作一次次的行走,每次行走都是用最短的路程从某一非空点到达另外一非空点: 两点间最少的步数是二者x和y坐标差的最大值: 返回原点这个过程,肯定是取完最后一个黄金后直接 ...

  2. 1057 - Collecting Gold

    1057 - Collecting Gold   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB ...

  3. 1057 - Collecting Gold (状态压缩DP)

    题目大意: 给你一个矩阵,'x'是你的起始位置, 'g'是宝藏的位置,问最少多少步可以把所有的宝藏取完,并且最后返回起始位置. 注意:没有宝藏的时候输出 0   =================== ...

  4. lightoj 1057 - Collecting Gold(状压dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1057 题解:看似有点下记忆话搜索但是由于他是能走8个方向的也就是说两点的距离其 ...

  5. LeetCode 1219. Path with Maximum Gold

    原题链接在这里:https://leetcode.com/problems/path-with-maximum-gold/ 题目: In a gold mine grid of size m * n, ...

  6. 【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 ...

  7. POJ2096 Collecting Bugs

    Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 5090   Accepted: 2529 Case Time Limit: ...

  8. poj3311 TSP经典状压dp(Traveling Saleman Problem)

    题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...

  9. 【poj2096】Collecting Bugs

    题目描述 Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other materia ...

随机推荐

  1. Java SpringMvc+hibernate架构中,调用Oracle中的sp,传递数组参数

    一.问题 我们调用数据,大都是可以直接获取表中的数据,或者用复杂点的sql语句组成的.但是,有时候,当这样达不到我们要的全部数据的时候,这时,我们就用到了存储过程[sp],如果sp需要参数是数组的话, ...

  2. HDU3591找零,背包

    题目大概的意思就是:小强用硬币买东西,硬币有N种,面值为Vi,店家有各种硬币都有无限个,而小强只有Ci个(分别对应Vi) 问最小交易硬币数,就是一个有找零的背包问题啦. 我的上一篇博客跟这hdu359 ...

  3. codeblocks快捷键(转载)

    • 按住Ctrl滚滚轮,代码的字体会随你心意变大变小. • 在编辑区按住右键可拖动代码,省去拉(尤其是横向)滚动条之麻烦:相关设置:Mouse Drag Scrolling. • Ctrl+D可复制当 ...

  4. WPF 进度条

    //Create a Delegate that matches the Signature of the ProgressBar's SetValue method private delegate ...

  5. jQuery监听键盘事件及相关操作使用

    一.首先需要知道的是: 1.keydown() keydown事件会在键盘按下时触发. 2.keyup() keyup事件会在按键释放时触发,也就是你按下键盘起来后的事件 3.keypress() k ...

  6. phpmyadmin数据导入最大限制的解决方法

    mysql导入文件最大限制更改解决方法:phpmyadmin库导入出错:You probably tried to upload too large file. Please refer to doc ...

  7. Warning File `.depend' has modification time 1.6 s in the future

    一.前提知识 主机时间与虚拟机时间不同步所致.我们在某一操作系统所属磁盘空间下创建一个文件,那么这个文件的创建时间是以磁盘所属的操作系统的时钟为基准的. 我们假设主机windows的系统时间是10:0 ...

  8. 实现windows和linux的NFS交互

    说明:本文是Omni-NFS-X Windows与Linux间通讯的另一种方式和在windows中配置使用NFS客户端的杂交篇 概述 windows/winnt4.0/win2000与Linux/Fr ...

  9. winfrom获得鼠标的坐标

    Point mouse = this.PointToScreen(Control.MousePosition);label1.Text = mouse.X.ToString() + ":&q ...

  10. [译]36 Days of Web Testing(二)

    Day 7: Http 和 Https Why? 当在网络上传输一些私人,敏感信息时,应该采用加密的手段来保证这些信息在传输的过程中不被侦测到.Https协议正是这种实现机制. Https是一种广泛使 ...