Collecting Gold LightOJ - 1057

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

2

5 5

x....

g....

g....

.....

g....

5 5

x....

g....

g....

.....

.....

Sample Output

Case 1: 8

Case 2: 4

题解:因为金矿最多有15个,所以强势转化成TSP问题。想到这点就行了。。。

 1 #include<cmath>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 #define INF 0x3f3f3f3f
 7 using namespace std;
 8
 9 int n,p,q;
10 int dp[1<<17][17],x[17],y[17];
11 char s[25][25];
12
13 int get(int i,int j){
14     return max(abs(x[i]-x[j]),abs(y[i]-y[j]));
15 }
16
17 int DFS(int S,int v){
18     if(dp[S][v]>=0) return dp[S][v];
19     if(S==(1<<n)-1 && v==0) return dp[S][v]=0;
20     int res=INF;
21     for(int u=0;u<n;u++){
22         if(S>>u & 1) continue;
23         res=min(res,DFS(S | 1<<u,u)+get(v,u));
24     }
25     return dp[S][v]=res;
26 }
27
28 int main()
29 {   int kase;
30     cin>>kase;
31     for(int t=1;t<=kase;t++){
32         cin>>p>>q;
33         n=1;
34         for(int i=1;i<=p;i++){
35             scanf("%s",s[i]+1);
36             for(int j=1;j<=q;j++){
37                 if(s[i][j]==‘x‘) x[0]=i,y[0]=j;
38                 if(s[i][j]==‘g‘) x[n]=i,y[n++]=j;
39             }
40         }
41         memset(dp,-1,sizeof(dp));
42         printf("Case %d: %d\n",t,DFS(0,0));
43     }
44     return 0;
45 }
时间: 2024-08-04 18:23:21

Collecting Gold LightOJ - 1057的相关文章

lightoj 1057 - Collecting Gold(状压dp)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1057 题解:看似有点下记忆话搜索但是由于他是能走8个方向的也就是说两点的距离其实就是最大的x轴或y轴的差.然后只有15个藏金点状压一下加dfs就行了. #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #define inf 0X3f3f3f

1057 - Collecting Gold

  PDF (English) Statistics Forum 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 t

1057 - Collecting Gold (状态压缩DP)

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

Discovering Gold LightOJ - 1030

You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell of the cave can contain any amount of gold. Initially you are in position 1. Now each turn you throw a perfect 6 sided dice. If you get X in the dice after throwin

LeetCode 1219. Path with Maximum Gold

原题链接在这里:https://leetcode.com/problems/path-with-maximum-gold/ 题目: In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can colle

LOL 术语

参考:http://guides.gamepressure.com/lol/guide.asp?ID=22190 ad:Attack Damage,物理攻擊.物理傷害 AA: (Auto Attack) ,右键点击目标,将会使你的角色进入自动攻击状态. afk:Away From Keyboard,人不在電腦前.暫時離開,掛網 aoe:Area of Effect,範圍傷害.範圍攻擊的意思 ap:Ability Power,魔法攻擊.法術傷害 ARM/Armor:物理防禦.護甲,減輕所受到的物理

LightOJ 1030 Discovering Gold(期望)

Description You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell of the cave can contain any amount of gold. Initially you are in position 1. Now each turn you throw a perfect 6 sided dice. If you get X in the dice a

LightOJ 1030 Discovering Gold (概率/期望DP)

题目链接:LightOJ - 1030 Description You are in a cave, a long cave! The cave can be represented by a \(1 \times N\) grid. Each cell of the cave can contain any amount of gold. Initially you are in position \(1\). Now each turn you throw a perfect \(6\) s

LightOJ 1030 Discovering Gold【概率】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题意:基础概率题. 代码: #include <stdio.h> #include <string.h> #include <vector> #include <string> #include <algorithm> #include <iostream> #include <iterator>