在洛谷评测RE了,runtime error,但是找不出哪里出问题了,在本地编译器全都没问题。
题目是UVA1601“万圣节后的早晨 The Morning after Halloween”。
```c++
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
int n, m, k;//n列数,m行数
int map[18][18];
int vis1[256][256][256] = { 0 }, vis2[256][256][256] = { 0 }, walk[5][2] = { {0,0},{1,0},{-1,0},{0,1},{0,-1} }, le = 0;
//int path[256][256][256][3] = {0}, path2[256][256][256][3] = { 0 } ;
typedef struct Node {
int dis = 0;
pair<int, int> p[3];
Node(int x1 = 0, int y1 = 0, int x2 = 0, int y2 = 0, int x3 = 0, int y3 = 0) {
p[0].first = x1;
p[0].second = y1;
p[1].first = x2;
p[1].second = y2;
p[2].first = x3;
p[2].second = y3;
}
}nd;
bool is_change(int sx1, int sy1, int ex1, int ey1, int sx2, int sy2, int ex2, int ey2) {
if (sx1 == ex2 && ex1 == sx2 && sy1 == ey2 && sy2 == ey1)return true;
return false;
}
void init() {
le = 0;
memset(map, 0, sizeof(map));
memset(vis1, -1, sizeof(vis1));
memset(vis2, -1, sizeof(vis2));
//memset(path, 0, sizeof(path));
//memset(path2, 0, sizeof(path2));
}
int bfs_double(nd s, nd e) {
queue<nd> q;
queue<nd> p;
q.push(s);
p.push(e);
vis1[s.p[0].first * n + s.p[0].second][s.p[1].first * n + s.p[1].second][s.p[2].first * n + s.p[2].second] = 0;
vis2[e.p[0].first * n + e.p[0].second][e.p[1].first * n + e.p[1].second][e.p[2].first * n + e.p[2].second] = 0;
while (!q.empty() || !p.empty()) {
int xx = q.size(), yy = p.size();
if (!q.empty() && xx <= yy) {
nd st = q.front(); q.pop();
for (int i = 0; i < 5; i++) {
int x1 = 0, y1 = 0;
if (st.p[0].first) {
x1 = st.p[0].first + walk[i][0];
y1 = st.p[0].second + walk[i][1];
}
if (map[x1][y1])continue;
for (int j = 0; j < 5; j++) {
int x2 = 0, y2 = 0;
if (st.p[1].first) {
x2 = st.p[1].first + walk[j][0];
y2 = st.p[1].second + walk[j][1];
}
if (map[x2][y2])continue;
if (is_change(st.p[0].first, st.p[0].second, x1, y1, st.p[1].first, st.p[1].second, x2, y2))continue;
if (x1 == x2 && y1 == y2)continue;
for (int d = 0; d < 5; d++) {
int x3 = 0, y3 = 0;
if (st.p[2].first) {
x3 = st.p[2].first + walk[d][0];
y3 = st.p[2].second + walk[d][1];
}
if (map[x3][y3])continue;
if (vis1[x1 * n + y1][x2 * n + y2][x3 * n + y3] != -1)continue;
if (is_change(st.p[0].first, st.p[0].second, x1, y1, st.p[2].first, st.p[2].second, x3, y3))continue;
if (is_change(st.p[1].first, st.p[1].second, x2, y2, st.p[2].first, st.p[2].second, x3, y3))continue;
if (x1 == x3 && y1 == y3)continue;
if (x2 == x3 && y2 == y3)continue;
nd nex = nd(x1, y1, x2, y2, x3, y3);
nex.dis = st.dis + 1;
//cout << 1 << "(" << x1 << "," << y1 << ")" << "(" << x2 << "," << y2 << ")" << "(" << x3 << "," << y3 << ")" << nex.dis << endl;
if (vis2[x1 * n + y1][x2 * n + y2][x3 * n + y3] != -1) {
return vis2[x1 * n + y1][x2 * n + y2][x3 * n + y3] + nex.dis;
}
q.push(nex);
/*path[x1 * n + y1][x2 * n + y2][x3 * n + y3][0] = st.p[0].first * n + st.p[0].second;
path[x1 * n + y1][x2 * n + y2][x3 * n + y3][1] = st.p[1].first * n + st.p[1].second;
path[x1 * n + y1][x2 * n + y2][x3 * n + y3][2] = st.p[2].first * n + st.p[2].second;*/
vis1[x1 * n + y1][x2 * n + y2][x3 * n + y3] = nex.dis;
}
}
}
}
else{
nd ed = p.front(); p.pop();
for (int i = 0; i < 5; i++) {
int x1 = 0, y1 = 0;
if (ed.p[0].first) {
x1 = ed.p[0].first + walk[i][0];
y1 = ed.p[0].second + walk[i][1];
}
if (map[x1][y1])continue;
for (int j = 0; j < 5; j++) {
int x2 = 0, y2 = 0;
if (ed.p[1].first) {
x2 = ed.p[1].first + walk[j][0];
y2 = ed.p[1].second + walk[j][1];
}
if (map[x2][y2])continue;
if (is_change(ed.p[0].first, ed.p[0].second, x1, y1, ed.p[1].first, ed.p[1].second, x2, y2))continue;
if (x1 == x2 && y1 == y2)continue;
for (int d = 0; d < 5; d++) {
int x3 = 0, y3 = 0;
if (ed.p[2].first) {
x3 = ed.p[2].first + walk[d][0];
y3 = ed.p[2].second + walk[d][1];
}
if (map[x3][y3])continue;
if (vis2[x1 * n + y1][x2 * n + y2][x3 * n + y3] != -1)continue;
if (is_change(ed.p[0].first, ed.p[0].second, x1, y1, ed.p[2].first, ed.p[2].second, x3, y3))continue;
if (is_change(ed.p[1].first, ed.p[1].second, x2, y2, ed.p[2].first, ed.p[2].second, x3, y3))continue;
if (x1 == x3 && y1 == y3)continue;
if (x2 == x3 && y2 == y3)continue;
nd nex = nd(x1, y1, x2, y2, x3, y3);
nex.dis = ed.dis + 1;
//cout << 2 << "(" << x1 << "," << y1 << ")" << "(" << x2 << "," << y2 << ")" << "(" << x3 << "," << y3 << ")" << nex.dis << endl;
if (vis1[x1 * n + y1][x2 * n + y2][x3 * n + y3] != -1) {
return vis1[x1 * n + y1][x2 * n + y2][x3 * n + y3] + nex.dis;
}
p.push(nex);
/*path2[x1 * n + y1][x2 * n + y2][x3 * n + y3][0] = ed.p[0].first * n + ed.p[0].second;
path2[x1 * n + y1][x2 * n + y2][x3 * n + y3][1] = ed.p[1].first * n + ed.p[1].second;
path2[x1 * n + y1][x2 * n + y2][x3 * n + y3][2] = ed.p[2].first * n + ed.p[2].second;*/
vis2[x1 * n + y1][x2 * n + y2][x3 * n + y3] = nex.dis;
}
}
}
}
}
}
int main() {
while (cin >> n >> m >> k) {
string line;
getline(cin,line);
if (!n && !m && !k)return 0;
init();
nd st;
nd ed;
for (int i = 0; i < m; i++) {
getline(cin, line);
for (int j = 0; j < n; j++) {
if (line[j] == '#')map[i][j] = 1;
else if (line[j] == 'a') { st.p[0].first = i; st.p[0].second = j; }
else if (line[j] == 'b') { st.p[1].first = i; st.p[1].second = j; }
else if (line[j] == 'c') { st.p[2].first = i; st.p[2].second = j; }
else if (line[j] == 'A') { ed.p[0].first = i; ed.p[0].second = j; }
else if (line[j] == 'B') { ed.p[1].first = i; ed.p[1].second = j; }
else if (line[j] == 'C') { ed.p[2].first = i; ed.p[2].second = j; }
}
}
map[0][0] = 0;
int step = bfs_double(st, ed);
cout << step << endl;
}
return 0;
}
```