Submission #00020


ソースコード

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
#include <bits/stdc++.h>
using namespace std;
typedef pair< int, int > Pi;
const int INF = 1 << 28;
int h, w;
bool dontcan[30][30][30][30];
Pi Arev[30][30];
int min_cost[30][30];
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int bfs()
{
queue< Pi > que;
fill_n(*min_cost, 30 * 30, INF);
que.push(Pi(0, 0));
min_cost[0][0] = 0;
Arev[0][0] = Pi(-1, -1);
while(!que.empty()) {
Pi p = que.front();
que.pop();
if(p == Pi(h - 1, w - 1)) return(min_cost[h - 1][w - 1]);
for(int i = 0; i < 4; i++) {
int nx = p.second + dx[i], ny = p.first + dy[i];
if(nx < 0 || ny < 0 || nx >= w || ny >= h) continue;
if(dontcan[p.first][p.second][ny][nx]) continue;
if(min_cost[ny][nx] == INF) {
min_cost[ny][nx] = min_cost[p.first][p.second] + 1;
que.push(Pi(ny, nx));
Arev[ny][nx] = Pi(p.first, p.second);
}
}
}
return(-1);
}
int getCost(Pi a, Pi b) // a -> b は行っちゃダメ
{
queue< Pi > que;
fill_n(*min_cost, 30 * 30, INF);
que.push(Pi(a.first, a.second));
min_cost[a.first][a.second] = 0;
while(!que.empty()) {
Pi p = que.front();
que.pop();
if(p == Pi(h - 1, w - 1)) return(min_cost[h - 1][w - 1]);
for(int i = 0; i < 4; i++) {
int nx = p.second + dx[i], ny = p.first + dy[i];
if(nx < 0 || ny < 0 || nx >= w || ny >= h) continue;
if(dontcan[p.first][p.second][ny][nx]) continue;
if(p == a && Pi(ny, nx) == b) continue;
if(min_cost[ny][nx] == INF) {
min_cost[ny][nx] = min_cost[p.first][p.second] + 1;
que.push(Pi(ny, nx));
}
}
}
return(-1);
}
int main()
{
while(cin >> h >> w, h) {
memset(dontcan, false, sizeof(dontcan));
for(int i = 0; i < 2 * h - 1; i++) {
if(i % 2) {
for(int j = 0; j < w; j++) {
int x;
cin >> x;
if(x) dontcan[i / 2][j][i / 2 + 1][j] = dontcan[i / 2 + 1][j][i / 2][j] = true;
}
} else {
for(int j = 0; j < w - 1; j++) {
int x;
cin >> x;
if(x) dontcan[i / 2][j][i / 2][j + 1] = dontcan[i / 2][j + 1][i / 2][j] = true;
}
}
}
int Cost = bfs(), PrevCost = Cost - 1;
if(Cost == -1) {
hoge:
cout << -1 << endl;
continue;
}
for(Pi now = Pi(h - 1, w - 1), pre = Arev[h - 1][w - 1]; pre != Pi(-1, -1); now = Arev[now.first][now.second], pre = Arev[pre.first][pre.second], PrevCost--) {
int c = getCost(pre, now);
if(c == -1) goto hoge;
Cost = max(Cost, PrevCost + getCost(pre, now));
}
cout << Cost << endl;
}
}

ステータス

項目 データ
問題 0007 - 壊れたドア
ユーザー名 I dont any
投稿日時 2015-10-28 18:54:13
言語 C++11
状態 Wrong Answer
得点 0
ソースコード長 2507 Byte
最大実行時間 72 ms
最大メモリ使用量 1328 KB

セット

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

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
g1 WA 70 ms 1244 KB
1
g2 WA 68 ms 1184 KB
1
g3 WA 72 ms 1256 KB
1
g4 WA 70 ms 1328 KB
1