Submission #00204


ソースコード

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
#include <bits/stdc++.h>
using namespace std;
int main() {
int h, w;
cin >> h >> w;
string a[h];
pair<int, int> s, g;
for (int i = 0; i < h; i++) {
cin >> a[i];
int pos = a[i].find("S");
if (pos != -1) {
s = {i, pos};
a[i][pos] = '.';
}
pos = a[i].find("G");
if (pos != -1) {
g = {i, pos};
a[i][pos] = '.';
}
}
queue<pair<int, int>> q;
q.push(s);
vector<vector<vector<int>>> dist(2, vector<vector<int>>(h, vector<int>(w, -1)));
dist[0][s.first][s.second] = 0;
while (!q.empty()) {
int y = q.front().first;
int x = q.front().second;
q.pop();
if (y - 1 >= 0 && a[y - 1][x] != '#') {
if (a[y][x] == 'D' || a[y][x] == 'L' || a[y][x] == 'R') {
dist[1][y - 1][x] = dist[0][y][x] + 1;
} else if (dist[0][y - 1][x] == -1) {
dist[0][y - 1][x] = dist[0][y][x] + 1;
if (dist[1][y][x] != -1) {
dist[1][y - 1][x] = dist[1][y][x] + 1;
}
q.push({y - 1, x});
}
}
if (y + 1 < h && a[y + 1][x] != '#') {
if (a[y][x] == 'U' || a[y][x] == 'L' || a[y][x] == 'R') {
dist[1][y + 1][x] = dist[0][y][x] + 1;
} else if (dist[0][y + 1][x] == -1) {
dist[0][y + 1][x] = dist[0][y][x] + 1;
if (dist[1][y][x] != -1) {
dist[1][y + 1][x] = dist[1][y][x] + 1;
}
q.push({y + 1, x});
}
}
if (x - 1 >= 0 && a[y][x - 1] != '#') {
if (a[y][x] == 'U' || a[y][x] == 'D' || a[y][x] == 'R') {
dist[1][y][x - 1] = dist[0][y][x] + 1;
} else if (dist[0][y][x - 1] == -1) {
dist[0][y][x - 1] = dist[0][y][x] + 1;
if (dist[1][y][x] != -1) {
dist[1][y][x - 1] = dist[1][y][x] + 1;
}
q.push({y, x - 1});
}
}
if (x + 1 < w && a[y][x + 1] != '#') {
if (a[y][x] == 'U' || a[y][x] == 'D' || a[y][x] == 'L') {
dist[1][y][x + 1] = dist[0][y][x] + 1;
} else if (dist[0][y][x + 1] == -1) {
dist[0][y][x + 1] = dist[0][y][x] + 1;
if (dist[1][y][x] != -1) {
dist[1][y][x + 1] = dist[1][y][x] + 1;
}
q.push({y, x + 1});
}
}
}
if (dist[0][g.first][g.second] == -1 && dist[1][g.first][g.second] == -1) {
cout << -1 << "\n";
} else if (dist[0][g.first][g.second] == -1) {
cout << dist[1][g.first][g.second] << "\n";
} else if (dist[1][g.first][g.second] == -1) {
cout << dist[0][g.first][g.second] << "\n";
} else {
cout << min(dist[0][g.first][g.second], dist[1][g.first][g.second]) << "\n";
}
return(0);
}

ステータス

項目 データ
問題 0008 - 一方通行迷路ゲーム
ユーザー名 woody_1227
投稿日時 2023-08-28 11:36:01
言語 C++17
状態 Wrong Answer
得点 0
ソースコード長 3074 Byte
最大実行時間 51 ms
最大メモリ使用量 3352 KB

セット

セット 得点 Cases
1 ALL 0 / 30 *

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
in1 AC 33 ms 472 KB
1
in2 AC 23 ms 1144 KB
1
in3 AC 29 ms 1956 KB
1
in4 AC 20 ms 2048 KB
1
in5 WA 22 ms 2140 KB
1
in6 AC 22 ms 2112 KB
1
in7 AC 51 ms 2080 KB
1
in8 AC 20 ms 2304 KB
1
in9 WA 23 ms 2552 KB
1
in10 WA 26 ms 700 KB
1
in11 WA 21 ms 664 KB
1
in12 WA 18 ms 828 KB
1
in13 WA 20 ms 848 KB
1
in14 AC 40 ms 1996 KB
1
in15 WA 33 ms 2008 KB
1
in16 AC 27 ms 2016 KB
1
in17 WA 30 ms 2148 KB
1
in18 WA 21 ms 2276 KB
1
in19 WA 32 ms 2288 KB
1
in20 WA 23 ms 2516 KB
1
in21 WA 32 ms 2524 KB
1
in22 AC 27 ms 2664 KB
1
in23 WA 24 ms 2676 KB
1
in24 AC 33 ms 2812 KB
1
in25 AC 21 ms 2812 KB
1
in26 WA 27 ms 3080 KB
1
in27 WA 30 ms 3216 KB
1
in28 AC 19 ms 3352 KB
1