題目: UVa - 10888 - Warehouse

題目說明

推箱子遊戲,問最少需要幾步才可以將所有箱子 (B) 推到 X。

解題思路

建圖後求 MCMF 即可。

參考解法

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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define CLR(c) memset(c, 0, sizeof(c))

static auto __ = []
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
return 0;
}();

template <typename T>
T QPOP(queue<T>& q)
{
T tmp = q.front();
q.pop();
return tmp;
}

const int INF = (int)1e9;
const int MXN = 2000;
const int D[] = { 1, 0, -1, 0, 1 };

vector<int> e[MXN];
map<pair<int, int>, int> pb, po; // position of B, position of #
char g[50][50];
int dist[50][50];

int c[MXN][MXN];
int f[MXN][MXN];
int cost[MXN][MXN];
int d[MXN];
int p[MXN];
bool inQ[MXN];

int S, T;
int X, Y;

void addEdge(int u, int v, int Cap = 0, int Cost = 0)
{
e[u].push_back(v);
c[u][v] = Cap;
cost[u][v] = Cost;
}

void init()
{
for (auto& v : e) v.clear();
pb.clear();
po.clear();
CLR(f);
}

void read()
{
cin >> Y >> X;
FOR(i, 0, Y) FOR(j, 0, X)
{
cin >> g[i][j];
if (g[i][j] == 'B') pb[{i, j}] = pb.size() + 1;
else if (g[i][j] == 'X') po[{i, j}] = po.size() + 1;
}
T = pb.size() + po.size() + 1;
}

void bfs(int y, int x)
{
fill(dist[0], dist[0] + 50 * 50, INF);
dist[y][x] = 0;

queue<pair<int, int>> q;
q.push({ y, x });

while (!q.empty())
{
auto [_y, _x] = QPOP(q);

FOR(i, 0, 4)
{
int dy = _y + D[i], dx = _x + D[i + 1];
if (dy < 0 || dy >= Y || dx < 0 || dx >= X || g[dy][dx] == '#') continue;
if (dist[dy][dx] == INF) q.push({ dy, dx });
dist[dy][dx] = min(dist[_y][_x] + 1, dist[dy][dx]);
}
}
}

void build()
{
for (auto& [boxPos, u] : pb)
{
auto& [y, x] = boxPos;

bfs(y, x);

addEdge(S, u, 1);
addEdge(u, S);

for (auto [pos, v] : po)
{
auto& [dy, dx] = pos;
v += pb.size();

addEdge(u, v, 1, dist[dy][dx]);
addEdge(v, u);
}
}

for (auto [_, v] : po)
{
v += pb.size();

addEdge(v, T, 1, 0);
addEdge(T, v);
}
}

bool SPFA()
{
fill(d, d + MXN, INF);
d[S] = 0;

CLR(inQ);
queue<int> q;

q.push(S);
inQ[S] = true;

while (!q.empty())
{
auto u = QPOP(q);
inQ[u] = false;

for (auto& v : e[u])
{
if (c[u][v] > f[u][v] && d[u] + cost[u][v] < d[v])
{
d[v] = d[u] + cost[u][v];
p[v] = u;
if (!inQ[v]) q.push(v), inQ[v] = true;
}
if (f[v][u] > 0 && d[u] + (-cost[v][u]) < d[v])
{
d[v] = d[u] + (-cost[v][u]);
p[v] = u;
if (!inQ[v]) q.push(v), inQ[v] = true;
}
}
}

return d[T] != INF;
}

int augment(int u, int v, int bottleNeck)
{
if (v == S) return bottleNeck;
bottleNeck = augment(p[u], u, min(c[u][v] - f[u][v], bottleNeck));
f[u][v] += bottleNeck;
f[v][u] -= bottleNeck;
return bottleNeck;
}

int MCMF()
{
int mnC = 0, mf = 0;

while (SPFA())
{
mnC += d[T];
mf += augment(p[T], T, INF);
}

return mnC;
}

void solve()
{
build();
cout << MCMF() << '\n';
}

int main()
{
int T;
cin >> T;
while (T--)
{
init();
read();
solve();
}
}