ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [1600] 말이 되고픈 원숭이
    BOJ 2021. 9. 5. 01:28

    https://www.acmicpc.net/problem/1600

     

    1600번: 말이 되고픈 원숭이

    첫째 줄에 정수 K가 주어진다. 둘째 줄에 격자판의 가로길이 W, 세로길이 H가 주어진다. 그 다음 H줄에 걸쳐 W개의 숫자가 주어지는데, 0은 아무것도 없는 평지, 1은 장애물을 뜻한다. 장애물이 있

    www.acmicpc.net

    <문제>

    4방향 bfs에서 제한적인 방향데이터 추가가 필요하다. 제한적이다는 의미는 더 빠른대신 횟수가 제한되어있다는 뜻이다.

    그렇기에 현재의 상태를 나타낼 변수를 아래처럼 구성했다.

    {R, C, cnt, turn}

    cnt : 더 빠른 방향데이터를 사용한 횟수 (k번 사용하면 더 사용할 수 없다)

    turn : 현재의 [R][C]까지 걸린 시간, 목표에 도달하면 출력할 값이다.

     

    그렇기에 visit도 약간의 추가가 필요했고, 아래처럼 구성해주었다

    visit[R][C][cnt] : map[R][C]를 cnt번의 빠른 이동을 이용해 도달했다면 true

     

    <소스코드>

    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
    #include<stdio.h>
    #include<queue>
    using namespace std;
    const int INF = 2121212121;
    int k, n, m, a[201][201];
    typedef struct { int y, x, cnt, turn; }state;
    queue<state>q;
    int dy[4= { 0,1,0,-1 }, dx[4= { 1,0,-1,0 };
    int fastdy[8= { -2,-1,1,2,2,1,-1,-2 }, fastdx[8= { 1,2,2,1,-1,-2,-2,-1 };
    bool visit[201][201][31];// y, x, cnt
    void f(void) {
        int i, y, x;
        while (!q.empty()) {
            state cur = q.front();
            q.pop();
            if (cur.y == n - 1 && cur.x == m - 1) {
                printf("%d", cur.turn);
                return;
            }
            //4방향
            for (i = 0; i < 4; i++) {
                y = cur.y + dy[i];
                x = cur.x + dx[i];
                if (y < 0 || x < 0 || y >= n || x >= m)continue;
                if (visit[y][x][cur.cnt] == false && a[y][x] == 0) {
                    q.push({ y,x,cur.cnt,cur.turn + 1 });
                    visit[y][x][cur.cnt] = true;
                }
            }
            if (cur.cnt == k)continue;
            //8방향
            for (i = 0; i < 8; i++) {
                y = cur.y + fastdy[i];
                x = cur.x + fastdx[i];
                if (y < 0 || x < 0 || y >= n || x >= m)continue;
                if (visit[y][x][cur.cnt + 1== false && a[y][x] == 0) {
                    q.push({ y,x,cur.cnt + 1,cur.turn + 1 });
                    visit[y][x][cur.cnt + 1= true;
                }
            }
        }
        printf("-1");
        return;
    }
    int main(void) {
        int i, j;
        scanf("%d"&k);
        scanf("%d %d"&m, &n);
        for (i = 0; i < n; i++)
            for (j = 0; j < m; j++)
                scanf("%d"&a[i][j]);
        q.push({ 0,0,0,0 });
        visit[0][0][0= true;
        f();
        return 0;
    }
    cs

     

     

     

    'BOJ' 카테고리의 다른 글

    [8980] 택배  (0) 2021.09.05
    [5639] 이진 검색 트리  (0) 2021.09.05
    [17142] 연구소 3  (0) 2021.09.02
    [1806] 부분합  (0) 2021.09.02
    [17609] 회문  (0) 2021.09.02

    댓글