-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaoj1166.cpp
More file actions
96 lines (86 loc) · 1.45 KB
/
aoj1166.cpp
File metadata and controls
96 lines (86 loc) · 1.45 KB
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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <cstring>
#include <cctype>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <complex>
using namespace std;
int field[30][30];
int hor[30][30];
int ver[30][30];
int result;
int w, h;
int main() {
while(1) {
cin >> w >> h;
if(w==0 && h==0) break;
for(int i=0; i<h; i++)
for(int j=0; j<w; j++)
field[i][j] = 0;
for(int i=0; i<h*2-1; i++) {
if(i%2==0) {
for(int j=0; j<w-1; j++) {
cin >> ver[i/2][j];
}
}
else {
for(int j=0; j<w; j++) {
cin >> hor[i/2][j];
}
}
}
queue<int> q;
q.push(0);
q.push(0);
q.push(1);
result = 0;
while(q.size() != 0) {
int x = q.front();
q.pop();
int y = q.front();
q.pop();
int cnt = q.front();
q.pop();
if(field[y][x] == 1) continue;
if(x == w-1 && y == h-1) {
result = cnt;
break;
}
if(y<h-1 && hor[y][x] == 0) {
q.push(x);
q.push(y+1);
q.push(cnt+1);
}
if(x<w-1 && ver[y][x] == 0) {
q.push(x+1);
q.push(y);
q.push(cnt+1);
}
if(y>0 && hor[y-1][x] == 0) {
q.push(x);
q.push(y-1);
q.push(cnt+1);
}
if(x>0 && ver[y][x-1] == 0) {
q.push(x-1);
q.push(y);
q.push(cnt+1);
}
field[y][x] = 1;
}
cout << result << endl;
}
return 0;
}