반응형

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

 

16933번: 벽 부수고 이동하기 3

첫째 줄에 N(1 ≤ N ≤ 1,000), M(1 ≤ M ≤ 1,000), K(1 ≤ K ≤ 10)이 주어진다. 다음 N개의 줄에 M개의 숫자로 맵이 주어진다. (1, 1)과 (N, M)은 항상 0이라고 가정하자.

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. 방문을 체크할 visited [1001][1001][11][2]를 만듭니다. (y, x, crash, day)

 

2. 낮일 때만 벽을 부술 수 있는 로직을 추가합니다.

 

3. 밤일 때 현재 자리에 벽이 있을 수 있으므로 분기를 추가해줍니다.

 

4. y == N, x == M일 때 cnt를 출력하고, 도착하지 못한다면 -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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<iostream>
#include<queue>
 
using namespace std;
 
struct pos {
    int y;        //y좌표
    int x;        //x좌표
    int cnt;    //이동 거리
    int crash;    //부순 벽 개수
    int day;    //0 : 밤, 1 : 낮
};
 
int N, M, K;
char arr[1002][1002];
int visited[1001][1001][11][2];        //visited[y][x][crash][day]
const int dy[5= { 0,-1,1,0,0 };
const int dx[5= { 0,0,0,-1,1 };
 
int main()
{
    scanf("%d %d %d"&N, &M, &K);
 
    for (int i = 1; i <= N; i++) {
        scanf("%s"&arr[i][1]);
    }
 
    queue<pos> q;
 
    q.push({ 1,1,1,0,1 });
    visited[1][1][0][1= 1;
 
    while (!q.empty()) {
        const int y = q.front().y;
        const int x = q.front().x;
        const int cnt = q.front().cnt;
        const int crash = q.front().crash;
        const int day = q.front().day;
        q.pop();
 
        if (y == N && x == M) {
            printf("%d", cnt);
            return 0;
        }
 
        for (int i = 0; i < 5; i++) {
            const int yy = y + dy[i];
            const int xx = x + dx[i];
 
            if (yy > 0 && yy <= N && xx > 0 && xx <= M) {
                if (day == 0) {    //밤일 때
                    if (i == 0) {
                        if (arr[yy][xx] == '1' && visited[yy][xx][crash][1!= 1) {
                            visited[yy][xx][crash][1= 1;
                            q.push({ yy,xx,cnt + 1,crash,1 });
                        }
                    }
                    if (arr[yy][xx] == '0' && visited[yy][xx][crash][1!= 1) {
                        visited[yy][xx][crash][1= 1;
                        q.push({ yy,xx,cnt + 1,crash,1 });
                    }
                }
                else {    //낮일 때
                    if (i != 0) {
                        if (arr[yy][xx] == '0' && visited[yy][xx][crash][0!= 1) {
                            visited[yy][xx][crash][0= 1;
                            q.push({ yy,xx,cnt + 1,crash,0 });
                        }
                        else if (arr[yy][xx] == '1' && crash < K) {
                            if (visited[yy][xx][crash + 1][0!= 1) {
                                visited[yy][xx][crash + 1][0= 1;
                                q.push({ yy,xx,cnt + 1,crash + 1,0 });
                            }
                        }
                    }
                }
            }
        }
    }
 
    printf("%d"-1);
}
cs
반응형

+ Recent posts