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 ...
随机推荐
- doj常用包
dojo.raise 抛出一个异常 dojo.errorToString将异常转换为字符串 dojo.render 系统环境对象 dojo.hostenv. ...
- Local IIS 7.0 - CS0016: Could not write to output file / Microsoft.Net > Framework > v4.0.30319 > Temporary ASP.NET Files
This week I went nuts over my local IIS. I have never swore to a machine that much in my whole life. ...
- maven依赖缺少oracle驱动包
maven添加oracle jdbc驱动的依赖问题 在maven工程里不能下载oracle的jdbc驱动jar包,需要Oracle官方授权才可以,找一个ojdbc14的10.2.0.1.0或10.2. ...
- iOS8 iPad Warning: Attempt to present <UIImagePickerController:xxxx > on xxxx which is already presenting (null)
解决方法: /* I think this is because in iOS 8, alert views and action sheets are actually presented view ...
- C#字符串颠倒输出
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- 【html】【3】html标签列表
必看参考: http://www.divcss5.com/html/h323.shtml http://www.w3school.com.cn/tags/tag_html.asp 常用: <ht ...
- jQuery 遍历同胞(siblings)
同胞拥有相同的父元素. 通过 jQuery,您能够在 DOM 树中遍历元素的同胞元素. 在 DOM 树中水平遍历 有许多有用的方法让我们在 DOM 树进行水平遍历: siblings() next() ...
- Linux的cat、more、less的区别
cat命令功能用于显示整个文件的内容单独使用没有翻页功能因此经常和more命令搭配使用,cat命令还有就是将数个文件合并成一个文件的功能. more命令功能:让画面在显示满一页时暂停,此时可按空格健继 ...
- Windows phone 之独立存储
独立存储命名空间的说明:
- hibernate中一对多 多对多 inverse cascade
----------------------------一对多------------------------------------------- inverse属性:是在维护关联关系的时候起作用的 ...