반응형

www.acmicpc.net/problem/3190

 

3190번: 뱀

 'Dummy' 라는 도스게임이 있다. 이 게임에는 뱀이 나와서 기어다니는데, 사과를 먹으면 뱀 길이가 늘어난다. 뱀이 이리저리 기어다니다가 벽 또는 자기자신의 몸과 부딪히면 게임이 끝난다. 게임

www.acmicpc.net

 

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 <queue>
#define pii pair<intint>
#define rep(i,n) for(int i=1;i<=n;i++)
using namespace std;
int arr[101][101], n, k, a, b, l, x;
int dx[] = { 010-1 };
int dy[] = { 10-10 };
char c, turn[10001];
bool visited[101][101];
queue<pii> q;
void dfs(int x, int y, int direction, int sec) {
    // 종료 조건
    // 1. 벽에 부딪힘
    // 2. 자기 몸에 부딪힘
    if (x <= 0 || x > n || y <= 0 || y > n || visited[x][y]) {
        cout << sec;
        exit(0);
    }
 
    // 머리 방문
    visited[x][y] = 1;
    q.push({ x, y });
 
    // 사과 없으면 꼬리 줄이기
    if (!arr[x][y]) {
        int tx = q.front().first;
        int ty = q.front().second;
        q.pop();
        visited[tx][ty] = 0;
    }
 
    // 사과 한 번 먹으면 더 이상 못 먹음
    arr[x][y] = 0;    
 
    // 방향 전환
    int next_direction = direction;
    if (turn[sec] == 'L'
        next_direction = (direction + 3) % 4;
    else if(turn[sec] == 'D')
        next_direction = (direction + 1) % 4;
 
    // 머리 다음 위치
    int nx = x + dx[next_direction];
    int ny = y + dy[next_direction];
    dfs(nx, ny, next_direction, sec + 1);
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> k;
    rep(i, k) {
        cin >> a >> b;
        arr[a][b] = 1;
    }
    cin >> l;
    rep(i, l) {
        cin >> x >> c;
        turn[x] = c;
    }
    q.push({ 1,1 });
    visited[1][1= 1;
    dfs(1201);
}
cs
반응형

'백준 > 삼성기출' 카테고리의 다른 글

백준 15683  (0) 2021.04.30
백준 13458  (0) 2021.04.29
백준 13460  (0) 2021.03.07
백준 12100 [복습 필수]  (0) 2021.03.03
백준 20055  (0) 2021.02.23

+ Recent posts