1057 - Collecting Gold
Time Limit: 2 second(s) Memory Limit: 32 MB

Finally you found the city of Gold. As you are fond of gold, you start collecting them. But there are so much gold that you are getting tired collecting them.

So, you want to find the minimum effort to collect all the gold.

You can describe the city as a 2D grid, where your initial position is marked by an 'x'. An empty place will be denoted by a '.'. And the cells which contain gold will be denoted by 'g'. In each move you can go to all 8 adjacent places inside the city.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case will start with a blank line and two integers, m and n (0 < m, n < 20) denoting the row and columns of the city respectively. Each of the next m lines will contain n characters describing the city. There will be exactly one 'x' in the city and at most 15 gold positions.

Output

For each case of input you have to print the case number and the minimum steps you have to take to collect all the gold and go back to 'x'.

Sample Input

Output for Sample Input

2

5 5

x....

g....

g....

.....

g....

5 5

x....

g....

g....

.....

.....

Case 1: 8

Case 2: 4

思路:状态压缩dp;

一开始用状压搜索来写的wa;

这题是个状态压缩dp,dp[i][j],表示在状态i下到j结束的最小的花费,将起始点放入一起状压,因为起点也是终点,那么最后答案就是dp[(1<<cn)-1][cn-1];cn为起始的标号,也是点的个数

还有可以8个方向,那么两个点的最小距离就是x,y两个方向距离中大的那个;

 1 #include<stdio.h>
2 #include<algorithm>
3 #include<stdlib.h>
4 #include<iostream>
5 #include<math.h>
6 #include<string.h>
7 #include<queue>
8 using namespace std;
9 typedef long long LL;
10 char ma[22][22];
11 int __ma[22][22];
12 int dp[1<<16][22];
13 typedef struct node
14 {
15 int x;
16 int y;
17 } ss;
18 ss ans[100];
19 int d(int n,int m,int nn,int mm)
20 {
21 int x = abs(n-nn);
22 int y = abs(m-mm);
23 return max(x,y);
24 }
25 int main(void)
26 {
27 int n,m;
28 int T,__ca=0;
29 scanf("%d",&T);
30 while(T--)
31 {
32 __ca++;
33 int i ,j;
34 //memset(flag,0,sizeof(flag));
35 scanf("%d %d",&n,&m);
36 for(i = 0; i < n; i++)
37 {
38 scanf("%s",ma[i]);
39 }
40 int __n,__m;
41 int cn=0;
42 for(i = 0; i < n; i++)
43 {
44 for(j = 0; j < m; j++)
45 {
46 if(ma[i][j] == 'x')
47 {
48 __n=i;
49 __m=j;
50 }
51 if(ma[i][j]=='g')
52 {
53 ans[cn].x = i;
54 ans[cn].y = j;
55 __ma[i][j] = cn++;
56 }
57 }
58 }
59 int ask=0;
60 for(i = 0; i < (1<<16); i++)
61 {
62 for(j = 0; j < 16; j++)
63 {
64 dp[i][j] = 1e9;
65 }
66 }
67 ans[cn].x = __n;
68 ans[cn].y = __m;
69 __ma[__n][__m] = cn++;
70 dp[0|(1<<(cn-1))][cn-1] = 0;
71 if( cn != 1)
72 {
73 for( i = 0; i <(1<<cn); i++)
74 {
75 for(int x = 0; x < cn; x++)
76 { if(i & (1<<x))
77 for(int y = 0; y < cn; y++)
78 {
79 int di=d(ans[x].x,ans[x].y,ans[y].x,ans[y].y);
80 dp[i|(1<<y)][y] = min(dp[i|(1<<y)][y],dp[i][x]+di);
81 }
82 }
83 }
84 ask=dp[(1<<cn)-1][cn-1];
85 }
86 printf("Case %d: ",__ca);
87 printf("%d\n",ask);
88 }
89 return 0;
90 }

最新文章

  1. 第二章作业-第3题(markdown格式)-万世想
  2. CMake
  3. ros语音交互(四)移植科大讯飞语音识别到ros
  4. Linux运维入门到高级全套常用要点
  5. string、math、random、datetime类
  6. ACM: meixiuxiu学图论-并查集-最小生成树-解题报告
  7. Django过滤器列表
  8. Sublime text 快捷键总结
  9. linux内核编程笔记【原创】
  10. poj1000 A+B Problem
  11. 【POJ 3487】 The Stable Marriage Problem (稳定婚姻问题)
  12. Java设计模式(七)策略模式 模板模式
  13. flume 以 kafka 为channel 的配置
  14. 阿里巴巴的datasource
  15. 深入理解令牌认证机制(token)
  16. Yii2 console执行定时脚本
  17. myeclipse破解软件(jar包分析)
  18. Android 模块化/热修复/插件化 框架选用
  19. 逆袭之旅.DAY07东软实训..封装~继承~抽象~final
  20. mysql 主从复制change master to

热门文章

  1. 总结HashSet以及分析部分底层源码
  2. 用友低代码开发平台YonBuilder首次亮相DevRun开发者沙龙
  3. 用前端表格技术构建医疗SaaS 解决方案
  4. 11-如何通过newman生成不同类型的测试报告
  5. 【Linux】【Web】【Nginx】配置nginx日志到远程syslog服务器
  6. springboot 设置项目路劲后不能访问首页
  7. vue 键盘事件keyup/keydoen
  8. XML名命空间
  9. Jenkins 关闭和重启的实现方式
  10. iOS-启动项目(一)设置 rootViewController