https://www.acmicpc.net/problem/13460
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
|
#include <iostream>
#include <queue>
struct coord
{
int y;
int x;
}R, B;
int N, M;
int dy[4] = { 1, 0, -1, 0 };
int dx[4] = { 0, 1, 0, -1 };
char board[11][11];
void input()
{
std::cin >> N >> M;
for (int y = 1; y <= N; ++y)
{
for (int x = 1; x <= M; ++x)
{
std::cin >> board[y][x];
if (board[y][x] == 'R')
{
board[y][x] = '.';
R = { y, x };
}
if (board[y][x] == 'B')
{
board[y][x] = '.';
B = { y, x };
}
}
}
}
void print(int ry, int rx, int by, int bx)
{
std::cout << '\n';
for (int y = 1; y <= N; ++y)
{
for (int x = 1; x <= M; ++x)
{
if (y == ry && x == rx)
{
std::cout << 'R' << ' ';
}
else if (y == by && x == bx)
{
std::cout << 'B' << ' ';
}
else
{
std::cout << board[y][x] << ' ';
}
}
std::cout << '\n';
}
}
int solve()
{
std::queue<std::pair<std::pair<coord, coord>, std::pair<int, int>>> bfs;
bfs.push({ { R, B }, { 1, 4 } });
int ans = 0;
while (bfs.empty() == false)
{
coord rPos = bfs.front().first.first;
coord bPos = bfs.front().first.second;
int cnt = bfs.front().second.first;
int flag = bfs.front().second.second;
bfs.pop();
if (cnt > 10)
{
continue;
}
//print(0, 0, 0, 0);
//std::cout << "----------------------------------------------------------\n";
for (int i = 0; i < 4; ++i)
{
if (flag != 4 && flag % 2 == 0 && i % 2 == 0)
{
continue;
}
if (flag != 4 && flag % 2 == 1 && i % 2 == 1)
{
continue;
}
bool chk = false;
coord rNext = rPos;
coord bNext = bPos;
int rCnt = 0;
int bCnt = 0;
while (board[rNext.y][rNext.x] == '.')
{
rNext.y += dy[i];
rNext.x += dx[i];
++rCnt;
}
rNext.y -= dy[i];
rNext.x -= dx[i];
while (board[bNext.y][bNext.x] == '.')
{
bNext.y += dy[i];
bNext.x += dx[i];
++bCnt;
}
// if (board[bNext.y][bNext.x] == 'O')
// {
// continue;
// }
bNext.y -= dy[i];
bNext.x -= dx[i];
if (bNext.y == rNext.y && bNext.x == rNext.x && board[rNext.y + dy[i]][rNext.x + dx[i]] == 'O')
{
continue;
}
if (rNext.y == bNext.y && rNext.x == bNext.x)
{
coord* ptr = nullptr;
if (rCnt < bCnt)
{
ptr = &bNext;
}
else
{
ptr = &rNext;
}
ptr->y -= dy[i];
ptr->x -= dx[i];
}
if (board[rNext.y + dy[i]][rNext.x + dx[i]] == 'O')
{
return cnt;
}
if (board[bNext.y + dy[i]][bNext.x + dx[i]] == 'O')
{
continue;
}
//print(rNext.y, rNext.x, bNext.y, bNext. x);
if (rNext.y != rPos.y || rNext.x != rPos.x || bNext.y != bPos.y || bNext.x != bPos.x)
{
bfs.push({ {rNext, bNext}, { cnt + 1, i } });
}
}
}
return -1;
}
int main()
{
input();
std::cout << solve();
return 0;
}
|
cs |
테스트케이스를 돌려보기 전에 먼저
실수한 부분이 없는지(오타 등), 내 생각대로 코드가 흘러가는지
먼저 생각해보고
그 뒤에도 틀린다면 예외를 생각해보자
'알고리즘' 카테고리의 다른 글
백준 2798 : 블랙잭 (0) | 2020.08.14 |
---|---|
백준 1568 : 새 (0) | 2020.08.14 |
백준 2178 : 미로 탐색 (0) | 2020.08.10 |
백준 1697 : 숨바꼭질 (0) | 2020.08.10 |
koi : 별그리기 (0) | 2020.08.07 |