-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1938.cpp
More file actions
163 lines (156 loc) · 2.83 KB
/
1938.cpp
File metadata and controls
163 lines (156 loc) · 2.83 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
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
154
155
156
157
158
159
160
161
162
163
// 1938. 통나무 옮기기
// 2019.05.19
// BFS
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
// 나무 구조체.
struct Tree
{
int x, y, z;
Tree(int x, int y, int z) :x(x), y(y), z(z) {}
Tree() {}
};
int dx[] = { 1,-1,0,0,0 };
int dy[] = { 0,0,1,-1,0 };
int dz[] = { 0,0,0,0,1 };
// 회전시 9방향 검사
int fx[] = { -1,-1,-1,0,0,0,1,1,1 };
int fy[] = { 1,-1,0,1,-1,0,1,-1,0 };
char a[55][55];
int n, disc[55][55][2];
int chk(int x, int y)
{
return 0 <= x && x < n && 0 <= y && y < n;
}
bool possible(Tree e, int move)
{
int x = e.x;
int y = e.y;
int z = e.z;
if (move == 4)
{
for (int i = 0; i < 9; i++)
{
// 범위 벗어나는지 체크
if (!chk(x + fx[i], y + fy[i]))
{
return false;
}
// 1이면 움직일수 없음
if (a[x + fx[i]][y + fy[i]] == '1')
{
return false;
}
}
return true;
}
else
{
int lx, ly, rx, ry;
if (z == 1)
{
lx = x + 1 + dx[move];
ly = y + dy[move];
rx = x - 1 + dx[move];
ry = y + dy[move];
x += dx[move], y += dy[move];
if (chk(lx, ly) && chk(x, y) && chk(rx, ry) && a[lx][ly] != '1'&&a[rx][ry] != '1'&&a[x][y] != '1')
{
return true;
}
}
else
{
lx = x + dx[move];
ly = y + 1 + dy[move];
rx = x + dx[move];
ry = y - 1 + dy[move];
x += dx[move];
y += dy[move];
if (chk(lx, ly) && chk(x, y) && chk(rx, ry) && a[lx][ly] != '1'&&a[rx][ry] != '1'&&a[x][y] != '1')
{
return true;
}
}
}
return false;
}
Tree s, e;
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
// 시작점(B)찾기
if (a[i][j] == 'B')
{
if (chk(i - 1, j) && chk(i + 1, j) && a[i - 1][j] == 'B'&&a[i + 1][j] == 'B')
{
s = Tree(i, j, 1);
}
if (chk(i, j - 1) && chk(i, j + 1) && a[i][j - 1] == 'B'&&a[i][j + 1] == 'B')
{
s = Tree(i, j, 0);
}
}
// 도착점(E) 찾기
if (a[i][j] == 'E')
{
if (chk(i - 1, j) && chk(i + 1, j) && a[i - 1][j] == 'E'&&a[i + 1][j] == 'E')
{
e = Tree(i, j, 1);
}
if (chk(i, j - 1) && chk(i, j + 1) && a[i][j - 1] == 'E'&&a[i][j + 1] == 'E')
{
e = Tree(i, j, 0);
}
}
}
}
queue<Tree> qu;
disc[s.x][s.y][s.z] = 1;
qu.push(s);
int ans = 0;
while (qu.size())
{
int qs = qu.size();
while (qs--)
{
Tree here = qu.front();
qu.pop();
// BBB->EEE 도착 완료
if (here.x == e.x&&here.y == e.y&&here.z == e.z)
{
cout << ans << endl;
return 0;
}
for (int i = 0; i < 5; i++)
{
if (!possible(here, i))
{
continue;
}
int nx = here.x + dx[i];
int ny = here.y + dy[i];
int nz = here.z^dz[i]; // 뒤집기
if (disc[nx][ny][nz])
{
continue;
}
disc[nx][ny][nz] = 1;
qu.push(Tree(nx, ny, nz));
}
}
ans++;
}
cout << 0 << endl;
return 0;
}