Today
Total
Archives
05-18 16:22
관리 메뉴

tony9402

[백준 5427] 불 본문

알고리즘/Baekjoon Online Judge

[백준 5427] 불

ssu_gongdoli 2018. 8. 22. 14:40
반응형

백준 5427






이 문제는 상근(@)이가 안전하게 맵 밖으로 나가면 된다. 문제를 보면 불(*)이 붙으려는 칸으로 이동할 수 없다고 한다. 이를 알고리즘으로 해결하기 위해서는 불부터 이동시키고 그다음 상근이를 이동시키면 된다.



일단 먼저 맵의 정보를 읽으면서 불의 위치와 상근이의 위치를 각각 큐에 넣는다. 상근이의 위치가 들어 있는 큐의 크기가 0일 때까지 불을 한칸씩 이동시키고 상근이의 위치를 한 칸씩 이동시키면 된다. 이를 반복하다가 상근이가 맵 밖으로 나간다면 상근이가 이동한 횟수를 출력하면 되고 상근이의 위치가 담긴 큐의 크기가 0일 때까지 반복하는데 상근이가 맵 밖으로 못나갔으면 IMPOSSIBLE을 출력하면 된다.




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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include<cstdio>
#include<iostream>
#include<queue>
 
using namespace std;
 
char map[1001][1001= { 0 };
queue<pair< pair<intint>int>> men;
queue<pair<intint>> fire;
 
int dx[] = { -1,1,0,0 };
int dy[] = { 0,0,1,-1 };
bool check = false;
 
int main()
{
    int TestCase;
    scanf("%d"&TestCase);
    for (; TestCase--;)
    {
        int w, h;
        scanf("%d %d"&w, &h);
        for (int i = 0; i < h; i++)
        {
            scanf("%s", map[i]);
            for (int j = 0; map[i][j]; j++)
            {
                if (map[i][j] == '@')
                {
                    men.push(make_pair(make_pair(i, j), 0));
                }
                else if (map[i][j] == '*')
                {
                    fire.push(make_pair(i, j));
                }
            }
        }
        check = false;
        while (1)
        {
            int size;
            int qx, qy, count;
            size = fire.size();
            while (size--) {
                qx = fire.front().second;
                qy = fire.front().first;
                fire.pop();
 
                for (int i = 0; i < 4; i++)
                {
                    if (qx + dx[i] < 0 || qx + dx[i] >= w || qy + dy[i] < 0 || qy + dy[i] >= h)continue;
                    if (map[qy + dy[i]][qx + dx[i]] == '.' || map[qy + dy[i]][qx + dx[i]] == '@')
                    {
                        map[qy + dy[i]][qx + dx[i]] = '*';
                        fire.push(make_pair(qy + dy[i], qx + dx[i]));
                    }
                }
            }
 
            size = men.size();
            while (size--) {
                qx = men.front().first.second;
                qy = men.front().first.first;
                count = men.front().second;
                men.pop();
 
                for (int i = 0; i < 4; i++)
                {
                    if (qx + dx[i] < 0 || qx + dx[i] >= w || qy + dy[i] < 0 || qy + dy[i] >= h)
                    {
                        check = true;
                        break;
                    }
                    if (map[qy + dy[i]][qx + dx[i]] == '.')
                    {
                        map[qy + dy[i]][qx + dx[i]] = '@';
                        men.push(make_pair(make_pair(qy + dy[i], qx + dx[i]), count + 1));
                    }
                }
            }
            if (check == true)
            {
                printf("%d\n", count + 1);
                while (!men.empty())men.pop();
                while (!fire.empty())fire.pop();
                break;
            }
            if (men.empty())
            {
                while (!men.empty())men.pop();
                while (!fire.empty())fire.pop();
                printf("IMPOSSIBLE\n");
                break;
            }
            
        }
 
    }
    return 0;
}
cs




이 문제와 거의 같은 문제가 있다. 그 문제는 불! 이라는 문제이다. 불! 문제도 이 문제와 거의 같은 방식으로 푸는 문제인걸로 기억한다.

반응형

'알고리즘 > Baekjoon Online Judge' 카테고리의 다른 글

[백준 7576] 토마토(2차원)  (0) 2018.08.23
[백준 4179] 불!  (0) 2018.08.22
[백준 2178] 미로탐색  (0) 2018.08.21
[백준 2606] 바이러스  (3) 2018.08.17
[백준 10026] 적록색약  (0) 2018.08.16
Comments