题目大意:
给你一个矩阵,'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 ;
}

最新文章

  1. FineReport制作可动态展开的组织递归树报表
  2. linux命令语法格式
  3. Scala 深入浅出实战经典 第53讲:Scala中结构类型实战详解
  4. python学习笔记三 文件操作(基础篇)
  5. 20150221&mdash;LINQ to SQL 查询数据
  6. C++中多重继承构造函数执行顺序
  7. MySQL Administrator的简单操作
  8. 6.0、Android Studio性能优化工具
  9. [转]php hash_pbkdf2 和 node.js crypto.pbkdf2
  10. 培训班课程课时及费用管理系统V3.0,适合钢琴培训班、艺术培训班等
  11. SLAM数据集整理
  12. Oracle 临时表创建及删除
  13. redis三种启动方式
  14. [LeetCode] 331. Verify Preorder Serialization of a Binary Tree_Medium tag: stack
  15. WebLogic 任意文件上传 远程代码执行漏洞 (CVE-2018-2894)-------&gt;&gt;&gt;任意文件上传检测POC
  16. linux添加计划任务
  17. day6作业--选课系统
  18. CentOS 6编译安装yum和配置常用的yum源
  19. tomcat解析
  20. PS 放大眼睛

热门文章

  1. html-----010
  2. Dao模型设计(基于Dao与Hebernate框架)
  3. linux命令之端口占用
  4. Demo_张仕传_结构体考试-modify
  5. 自定义注解与MYSQL
  6. java中的JSON对象的使用
  7. c# 实现文件批量压缩
  8. 【转】JSONP简介
  9. textarea出现多余的空格
  10. notepad++ :正则表达式系统教程