-
Notifications
You must be signed in to change notification settings - Fork 2
/
1057 - Collecting Gold.cpp
135 lines (135 loc) · 3.02 KB
/
1057 - Collecting Gold.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define inf 10000007
char str[22][22];
int dist[22][22];
int mat[16][16];
int dp[16][(1<<16)+2];
int dirx[] = {+0,+0,+1,-1,-1,+1,-1,+1};
int diry[] = {-1,+1,+0,+0,+1,+1,-1,-1};
vector<pii> gold;
pii init;
map<pii, int> mp;
int m,n, val;
struct Node
{
int x;
int y;
int cost;
Node(int a, int b, int c)
{
x = a;
y = b;
cost = c;
}
bool operator<(const Node& ob)const
{
return cost>ob.cost;
}
};
void dijkstra(int x, int y)
{
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
dist[i][j] = inf;
}
}
dist[x][y] = 0;
priority_queue<Node> pq;
pq.push(Node(x,y,0));
while(!pq.empty()){
x = pq.top().x;
y = pq.top().y;
int cost = pq.top().cost;
pq.pop();
for(int i=0; i<8; i++){
int xx = x + dirx[i];
int yy = y + diry[i];
if(xx>=0 && xx<m && yy>=0 && yy<n){
if(dist[xx][yy] > cost+1){
dist[xx][yy] = cost+1;
pq.push(Node(xx,yy,cost+1));
}
}
}
}
}
void process()
{
map<pii, int> :: iterator it = mp.begin();
while(it!=mp.end()){
int id = it->second;
pii now = it->first;
dijkstra(now.first, now.second);
map<pii, int> :: iterator it2 = mp.begin();
while(it2!=mp.end()){
int id2 = it2->second;
now = it2->first;
mat[id][id2] = dist[now.first][now.second];
it2++;
}
it++;
}
}
int setBit(int N, int pos)
{
return (N|(1<<pos));
}
bool checkBit(int N, int pos)
{
return (N&(1<<pos));
}
int fun(int pos, int mask)
{
if(mask==((1<<val)-1)) return mat[pos][0];
int &ret = dp[pos][mask];
if(ret!=-1) return ret;
ret = inf;
for(int i=1; i<val; i++){
if(!checkBit(mask, i)){
ret = min(ret, mat[pos][i] + fun(i, setBit(mask,i)));
}
}
return ret;
}
int solve()
{
// for(int i=0; i<val; i++){
// for(int j=0; j<val; j++){
// cout<<mat[i][j]<< " ";
// }
// cout<<endl;
// }
memset(dp, -1, sizeof dp);
return fun(0,1);
}
int main()
{
int tc,cs=1;
scanf("%d",&tc);
while(tc--){
scanf("%d%d",&m,&n);
gold.clear();
pii init;
mp.clear();
val = 1;
for(int i=0; i<m; i++){
scanf("%s",str[i]);
for(int j=0; j<n; j++){
if(str[i][j]=='x'){
init = {i,j};
mp[ {i,j} ] = 0;
}
else if(str[i][j]=='g'){
gold.push_back({i,j});
mp[ {i,j} ] = val++;
}
}
}
process();
int ans = solve();
printf("Case %d: %d\n", cs++, ans);
}
return 0;
}