반응형

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

 

14497번: 주난의 난(難)

주난이는 크게 화가 났다. 책상 서랍 안에 몰래 먹으려고 숨겨둔 초코바가 사라졌기 때문이다. 주난이는 미쳐 날뛰기 시작했다. 사실, 진짜로 뛰기 시작했다. ‘쿵... 쿵...’ 주난이는 점프의 파

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. visited[ 301 ][ 301 ] 배열을 만들어 각 노드의 방문을 기록해 줍니다.

 

2. arr 배열에 교실을 입력받고, bfs를 활용하여 queue에 주난이의 위치를 넣은 다음 1을 만나면 0으로 바꾸어주고, 0을 만나면 queue에 그 좌표를 넣어주고, #을 만나면 점프한 횟수를 출력합니다.

 

[ 소스코드 ]

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>
 
using namespace std;
 
int N, M;
int x, y, x2, y2;
int visited[301][301];
char arr[301][301];
int dx[4= { 1,-1,0,0 };
int dy[4= { 0,0,1,-1 };
 
int main()
{
    scanf("%d %d"&N, &M);
 
    scanf("%d %d %d %d"&x, &y, &x2, &y2);
 
    for (int i = 0; i < N; i++ ) {
        scanf("%s", arr[i]);
    }
 
    queue<pair<intint>>q;
 
    int cnt = 0;
 
    while (1) {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                visited[i][j] = 0;
            }
        }
        cnt++;
        q.push({ x - 1, y - 1 });
        visited[x - 1][y - 1= 1;
 
        while (!q.empty()) {
            const int x = q.front().first;
            const int y = q.front().second;
            q.pop();
 
            for (int i = 0; i < 4; i++) {
                int xx = x + dx[i];
                int yy = y + dy[i];
 
                if (xx >= 0 && xx < N && yy >= 0 && yy < M) {
                    if (visited[xx][yy] != 1) {
                        visited[xx][yy] = 1;
                        if (arr[xx][yy] == '1') {
                            arr[xx][yy] = '0';
                        }
                        else if (arr[xx][yy] == '0') {
                            q.push({ xx,yy });
                        }
                        else {
                            printf("%d", cnt);
                            return 0;
                        }
                    }
                }
            }
        }
    }
}
cs
반응형

+ Recent posts