-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOJ_1726번.cpp
153 lines (110 loc) · 2.19 KB
/
BOJ_1726번.cpp
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
//로봇 1726번
#include<iostream>
#include<queue>
using namespace std;
#define MAX_SIZE 101
class Point {
public :
int x;
int y;
int direct;
public:
Point(int a, int b, int c) {
x = a;
y = b;
direct = c;
}
};
int map[MAX_SIZE][MAX_SIZE];
int visit[MAX_SIZE][MAX_SIZE][4];
int m, n, st_x, st_y, st_d, en_x, en_y, en_d, ret;//방향 : 동쪽 1, 서쪽 2, 남쪽 3, 북쪽 4
queue<Point> q;
int turn_left(int d) {
if (d == 1) return 4;
else if (d == 2) return 3;
else if(d == 3) return 1;
else return 2;
}
int turn_right(int d) {
if (d == 1) return 3;
else if (d == 2) return 4;
else if (d == 3) return 2;
else return 1;
}
void BFS() {
int size;
while (!q.empty()) {
size = q.size();
ret++;
for (int i = 0; i < size; i++) {
int xp = q.front().x;
int yp = q.front().y;
int d = q.front().direct;
q.pop();
if (xp == en_x && yp == en_y && d == en_d) {
cout << ret;
exit(0);
}
// 명령 1. Go k
int xp2, yp2, d2;
bool flag = true;
for (int j = 1; j <= 3; j++) { // k는 1, 2, 3
if (!flag) break;
if (d == 1) {//동쪽
xp2 = xp;
yp2 = yp + j;
}
else if (d == 2) { // 서쪽
xp2 = xp;
yp2 = yp - j;
}
else if (d == 3) { // 남쪽
xp2 = xp + j;
yp2 = yp;
}
else { //북쪽
xp2 = xp - j;
yp2 = yp;
}
if (xp2 < 1 || yp2 < 1 || m < xp2 || n < yp2) continue;
if (visit[xp2][yp2][d] == 1) continue;
if (map[xp2][yp2] == 1) {
flag = false;
continue;
}
visit[xp2][yp2][d] = 1;
q.push(Point(xp2, yp2, d));
}
//명령 2, Turn dir
// Left
d2 = turn_left(d);
if (visit[xp][yp][d2] == 0) {
visit[xp][yp][d2] = 1;
q.push(Point(xp, yp, d2));
}
// Right
d2 = turn_right(d);
if (visit[xp][yp][d2] == 0) {
visit[xp][yp][d2] = 1;
q.push(Point(xp, yp, d2));
}
}
}
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> m >> n;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
cin >> map[i][j];
}
}
cin >> st_x >> st_y >> st_d;
cin >> en_x >> en_y >> en_d;
ret = -1;
visit[st_x][st_y][st_d] = 1;
q.push(Point(st_x, st_y, st_d));
BFS();
return 0;
}