题目链接: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. Google Code项目代码托管网站上Git版本控制系统使用简明教程

    作为一个著名的在线项目代码托管网站,Google Code目前主要支持三种版本控制系统,分别为Git, Mercurial和 Subversion.Subversion即SVN相信大家都已经熟知了,这 ...

  2. 前端开发bower包管理器

    Bower 是 twitter 推出的一款包管理工具,基于nodejs的模块化思想,他可以很好的帮助你帮你解决js的依赖管理,比如jquery angular bootstrap 等等. 可以很方便的 ...

  3. MySQL常用函数 转载

    一.数学函数ABS(x)                    返回x的绝对值BIN(x) 返回x的二进制(OCT返回八进制,HEX返回十六进制)CEILING(x)                返 ...

  4. android关于installLocation

    以下内容主要参考自官网的描述. 从Android API-8开始,android允许你将应用程序安装到外部存储空间中去(比方:SD卡),你可以在AndroidManifest.xml中添加androi ...

  5. javascript 学习笔记之面向对象编程(一):类的实现

    ~~想是一回事,做是一回事,写出来又是一回事~~一直以来,从事C++更多的是VC++多一些,从面向过程到面向对象的转变,让我对OO的编程思想有些偏爱,将一个客观存在的规律抽象出来总是让人比较兴奋,通过 ...

  6. struts.xml文件示范

    <?xml version="1.0" encoding="GBK"?> <!--下面指定Struts2配置文件的DTD信息--> &l ...

  7. GPS定位学习笔记

    ********************************* GPS定位简介 ********************************** 1. iOS SDK提供两个框架来实现位置服务 ...

  8. PR曲线平滑

    两天写论文中,本来设计的是要画这个Precision-Recall Curve的,因为PRC是从信息检索中来的,而且我又做的类似一个检索,所以要画这个图,但是我靠,竟然发现不好画,找了很多资料等.最后 ...

  9. 卷积神经网络 cnnff.m程序 中的前向传播算法 数据 分步解析

    最近在学习卷积神经网络,哎,真的是一头雾水!最后决定从阅读CNN程序下手! 程序来源于GitHub的DeepLearnToolbox 由于确实缺乏理论基础,所以,先从程序的数据流入手,虽然对高手来讲, ...

  10. Linux内核监控模块-0-开篇简介

    最近做了一个关于Linux内核编程的项目,主要实现的功能是对文件系统的监控. 我采用的方式是将监控的功能做成一个可加载模块(LKM)的形式,动态加载到Linux内核中.在这个内核层的“监控模块”中,我 ...