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

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
const int INF = 1e9+;
const int MAXN = ;
int dp[MAXN][], m, n, k;///状态,当前所在的位置
char maps[][];
struct Point
{
int x, y;
}P[], Star; int GetLen(Point A, Point B)
{
int len = min(abs(A.x-B.x),abs(A.y-B.y));
len += max(abs(A.x-B.x),abs(A.y-B.y)) - len;
return len;
} int DFS(int sta,int x)///状态, 现在所在的位置
{
if(dp[sta][x] != -) return dp[sta][x]; dp[sta][x] = INF;
for(int i=; i<k; i++)
{
if( (sta&(<<i)) && i != x)
{
dp[sta][x] = min(dp[sta][x], DFS(sta-(<<x), i)+GetLen(P[x],P[i]));
}
}
// printf("dp[%d][%d] = %d\n", sta, x, dp[sta][x]);
return dp[sta][x];
} int main()
{
int T, cas = ;
scanf("%d", &T);
while(T --)
{
scanf("%d %d",&n, &m);
k = ;
memset(dp, -, sizeof(dp));
for(int i=; i<n; i++)
{
scanf("%s", maps[i]);
for(int j=; j<m; j++)
{
if(maps[i][j] == 'g')
P[k].x = i, P[k++].y = j;
if(maps[i][j] == 'x')
Star.x = i, Star.y = j;
}
} for(int i=; i<k; i++)
{
int sta = <<i;
dp[sta][i] = GetLen(Star, P[i]);
} int ans = INF, Lim = (<<k)-;
for(int i=; i<k; i++)///最后停留的位置
ans = min(ans,DFS(Lim,i)+GetLen(Star,P[i]));
if(k == )
ans = ;
printf("Case %d: %d\n", cas ++, ans);
}
return ;
}

1057 - Collecting Gold (状态压缩DP)的更多相关文章

  1. 1057 - Collecting Gold

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

  2. hdu4336 Card Collector 状态压缩dp

    Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  3. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

  4. POJ 3254 Corn Fields(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 Descr ...

  5. [知识点]状态压缩DP

    // 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...

  6. HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP

    题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...

  7. DP大作战—状态压缩dp

    题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...

  8. 状态压缩dp问题

    问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...

  9. BZOJ-1226 学校食堂Dining 状态压缩DP

    1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...

  10. Marriage Ceremonies(状态压缩dp)

     Marriage Ceremonies Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

随机推荐

  1. 获取键盘输入或者USB扫描枪数据

    /// <summary> /// 获取键盘输入或者USB扫描枪数据 可以是没有焦点 应为使用的是全局钩子 /// USB扫描枪 是模拟键盘按下 /// 这里主要处理扫描枪的值,手动输入的 ...

  2. linux 学习一

    linux 命令 ls -al ls -l cal

  3. 网卡添加VLAN TAG

    #modprobe 8021q 用命令 lsmod | grep 8021q 来检查 以配置网卡eth0为例,添加vlan号:1002 ================================ ...

  4. Codevs 2449 骑士精神 2005年省队选拔赛四川

    2449 骑士精神 2005年省队选拔赛四川 时间限制: 1 s 空间限制: 128000 KB 题目等级 : **大师 Master** 题目描述 Description 在一个5×5的棋盘上有12 ...

  5. EF初始化mysql数据库codefirst

    EF使用Code First修改生成数据库表名的方法 1. 重写OnModelCreating,去掉表名复数 System.Data.Entity.ModelConfiguration.Convent ...

  6. sqlserver字符串转日期

    declare   @str   varchar(15)     declare   @dt   datetime     select   @str='2005-8-26'     set   @d ...

  7. php怎么保留相除后几位小数:sprintf

    $n=0.1265489; echo sprintf("%.2f", $n); // 0.13

  8. JavaBean的使用

    JavaBean主要用于实现一些业务逻辑或封装一些业务对象 通常将JavaBean中的属性设置为私有的(private),但需要为其提供公共的(public)访问方法,也就是所说的getXXX()方法 ...

  9. 转 Web APi之认证(Authentication)两种实现方式【二】(十三)

    前言 上一节我们详细讲解了认证及其基本信息,这一节我们通过两种不同方式来实现认证,并且分析如何合理的利用这两种方式,文中涉及到的基础知识,请参看上一篇文中,就不再废叙述废话. 序言 对于所谓的认证说到 ...

  10. Accessing Scoped Variables

    To permit the JSP page to access the data, the servlet needs to use setAttribute to store the data i ...