题目链接: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. [Introduction to programming in Java 笔记] 1.3.8 Gambler's ruin simulation 赌徒破产模拟

    赌徒赢得机会有多大? public class Gambler { public static void main(String[] args) { // Run T experiments that ...

  2. 【elasticsearch】(3)centos7 安装中文分词插件elasticsearch-analyzer-ik

    前言 elasticsearch(下面简称ES,安装ES点击这里)的自带standard分词只能把汉语分割成一个个字,而不能分词.分段,这就是我们需要分析器ik的地方了. 一.下载ik的相应版本 查看 ...

  3. CentOS 6.4 64位 安装 apache-tomcat-6.0.43

    下载 tomcat: 地址:http://mirrors.hust.edu.cn/apache/tomcat/tomcat-6/v6.0.43/bin/apache-tomcat-6.0.43.tar ...

  4. 【原创】Android 从一个Activity跳转到另外一个Activity

    Android四大组件activity使用,实现两个activity之间的跳转 基本流程:创建两个activity-将其中一个activity中组件作为事件源-通过组件事件的处理借助intent对象实 ...

  5. 怎样让老浏览器兼容html5新标签

    CSS样式设置默认样式: <style> article, aside, canvas, details, figcaption, figure, footer, header, hgro ...

  6. Python 基础-python-列表-元组-字典-集合

    列表格式:name = []name = [name1, name2, name3, name4, name5] #针对列表的操作 name.index("name1")#查询指定 ...

  7. finalspeed服务器端和客户端安装

    https://www.91yun.org/archives/2775 https://www.91yun.org/archives/615 1.首先安装服务器端:一键安装代码 wget -N --n ...

  8. net Core 通过 Ef Core 访问、管理Mysql

    net Core 通过 Ef Core 访问.管理Mysql 本文地址:http://www.cnblogs.com/likeli/p/5910524.html 环境 dotnet Core版本:1. ...

  9. 如何通过友盟分析发布后App崩溃日志-b

    要分析崩溃日志,首先需要保留发布时的编译出来的.xcarchive文件.这个文件包含了.DSYM文件. 我一般的做法是,发布成功后,把这个文件.xcarchive直接提交到代码版本库对应的版本分支里, ...

  10. 基于STM32F10x的串口(USART)输入输出编程

    1 前言 STM32有强大的固件库,绝大部分函数都可以有库里面的函数组合编写.固件库可以到ST官网(www.st.com)上下载,也可以搜索“STM32 固件库 v3.5”下载到固件库.本文章就是基于 ...