티스토리 뷰

백준 온라인 저지

190326 BOJ 3190번 뱀

Stolen Moments 2019. 3. 26. 23:31

구현


- 배열 인덱스를 잘못 잡아서 헤맸다...입력 인덱스가 1~N인데  0~N-1로 잡는 실수.

- 큐에 뱀의 꼬리 좌표를 저장하는 식으로 구현.



소스 코드



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
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath>
#define FOR(i,a,b) for(int i = a; i < b; i++)
#define mp make_pair
#define pii pair<intint>
using namespace std;
int map[101][101];
int N, K, L, x, y, t;
char D;
int main() {
    cin >> N;
    cin >> K;
    while (K--) {
        cin >> x >> y;
        map[x][y] = 2;
    }
    map[1][1= 1;
    cin >> L;
    x = 1, y = 1;
    int dx = 0, dy = 1;
    queue<pii> q;
    q.push(mp(11));
    int T = 0;
    cin >> t >> D;
    L--;
    while (1) {
        x += dx;
        y += dy;
        if (x > N || x <= 0 || y > N || y <= 0) {
            cout << T + 1;
            return 0;
        }
        if (map[x][y] == 2) {
            q.push(mp(x, y));
            map[x][y] = 1;
        }
        else if (map[x][y] == 1) {
            cout << T + 1;
            return 0;
        }
        else {
            map[q.front().first][q.front().second] = 0;
            q.pop();
            q.push(mp(x, y));
            map[x][y] = 1;
        }
        T++;
        if (T == t) {
            if ((dx == 0 && D == 'L'|| (dx != 0 && D == 'D')) dx *= -1, dy *= -1;
            swap(dx, dy);
            if (L > 0) {
                cin >> t >> D;
                L--;
            }
        }
    }
}
cs


반응형
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/09   »
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
글 보관함