[C++]๋ฐฑ์ค 17198๋ฒ Bucket Brigade
[C++]๋ฐฑ์ค 17198๋ฒ Bucket Brigade
๐๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/17198
๐๋ฌธ์ ์ค๋ช
B๋ถํฐ ์์ํด์ L๊น์ง์ ์ต๋จ๊ฑฐ๋ฆฌ๋ฅผ ์ฐพ์์ฃผ๋ฉด ๋๋ค.
๐์ฝ๋
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
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
bool visited[11][11];
char mp[11][11];
int dx[4]= {0, 0, 1, -1};
int dy[4]= {1, -1, 0, 0};
int startx, starty, endx, endy;
struct comp{
int x, y, cnt;
};
void input(){
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
cin >> mp[i][j];
if(mp[i][j] == 'B'){
startx = i;
starty = j;
}
else if(mp[i][j] == 'L'){
endx = i;
endy = j;
}
}
}
}
void bfs(){
queue<comp> q;
q.push({startx, starty, 0});
visited[startx][starty] = true;
while(!q.empty()){
auto [x, y, cnt] = q.front();
q.pop();
for(int i = 0; i < 4; i++){
int nx = x + dx[i];
int ny = y + dy[i];
if(nx < 0 || nx >= 10 || ny < 0 || ny >= 10 || visited[nx][ny] || mp[nx][ny] == 'R') continue;
if(nx == endx && ny == endy){
cout << cnt;
return;
}
visited[nx][ny] = true;
q.push({nx, ny, cnt + 1});
}
}
}
void solve(){
input();
bfs();
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
This post is licensed under CC BY 4.0 by the author.