문제
https://www.acmicpc.net/problem/3190
해설
간단한 조건의 길찾기 문제
코드
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
|
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
//방향 => 오른쪽 +1 왼쪽 -1
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
int n,k,l,turn[10104],mp[104][104],ret = 0;
void go(){
queue<pair<int,int>> q;
q.push({0,0});
mp[0][0] = -1;
int len = 1;
int direc = 0;
int x=0,y=0;
while(true){
ret++;
x = x + dx[direc];
y = y + dy[direc];
if(x>=0&&x<n&&y>=0&&y<n){
if(mp[y][x]==0){
q.push({y,x});
int tmpx = q.front().second,tmpy = q.front().first;
q.pop();
mp[tmpy][tmpx] = 0;
}
else if(mp[y][x] == 1){
q.push({y,x});
}
else{
break;
}
mp[y][x] = -1;
}
else{
break;
}
direc = ((direc + turn[ret]))%4;
if(direc<0) direc+=4;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
cin >> n >> k;
int x=0,y=0;
for(int i=0;i<k;i++){
cin >> y >>x;
mp[y-1][x-1] = 1;
}
cin >> l;
int t=0;
char direc = ' ';
for(int i=0;i<l;i++){
cin >> t >> direc;
if(direc == 'D'){
turn[t] = 1;
}
else{
turn[t] = -1;
}
}
go();
cout << ret;
return 0;
}
|
cs |
'백준 알고리즘(C++)' 카테고리의 다른 글
백준 2343번 기타 레슨 ( C++ ) (0) | 2024.09.10 |
---|---|
백준 2792번 보석 상자 ( C++ ) (0) | 2024.09.09 |
백준 12100번 2048 (Easy) ( C++ ) (0) | 2024.09.06 |
백준 14889번 스타트와 링크 ( C++ ) (0) | 2024.09.05 |
백준 17144번 미세먼지 안녕! ( C++ ) (1) | 2024.09.04 |