기술 블로그

4179번 불! 본문

알고리즘 문제/BOJ

4179번 불!

parkit 2018. 12. 30. 00:56
728x90
반응형

문제 : https://www.acmicpc.net/problem/4179


테스트 케이스 : http://acm.student.cs.uwaterloo.ca/~acm00/090613/data/


참고로 B번이다.



기본적인 유형의 BFS이다. 


틀리면 안 된다.


핵심은 지훈이와 불에 대한 방문 처리를 각각 따로 해준다.

그리고 return 할 때, 그 상태(p==0)가 지훈이인지도 조건문에 넣어주면 된다.



참고로 불을 먼저 큐에 push해준다.





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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <math.h>
#include <algorithm>
#include <map>
 
using namespace std;
 
int R = 0, C = 0, jy = 0, jx = 0;
 
int dy[4= { 0-101 };
int dx[4= { -1010 };
 
char Map[1001][1001];
 
queue<pair< pair<intint>int > > q;
// 0 = 지훈, 1 = 불
 
bool Fire[1001][1001= { false, };
bool Jee[1001][1001= { false, };
 
int BFS()
{
    // 지훈
    q.push({ { jy, jx }, 0 });
    Jee[jy][jx] = true;
 
    int ret = 1;
 
    while (!q.empty())
    {
        int qSize = q.size();
 
        while (qSize--)
        {
            int y = q.front().first.first;
            int x = q.front().first.second;
            int p = q.front().second;
 
            q.pop();
 
            if ((x == 0 || x == C - 1 || y == 0 || y == R - 1&& p == 0)
            {
                return ret;
            }
 
            if (p == 0// 지훈
            {
                for (int i = 0; i < 4; i++)
                {
                    int ny = y + dy[i];
                    int nx = x + dx[i];
 
                    if (ny < 0 || ny >= R || nx < 0 || nx >= C || Jee[ny][nx]) continue;
 
                    if (Map[ny][nx] == '.')
                    {
                        q.push({ { ny, nx }, 0 });
                        Jee[ny][nx] = true;
                    }
                }
            }
            else if (p == 1// 불
            {
                for (int i = 0; i < 4; i++)
                {
                    int ny = y + dy[i];
                    int nx = x + dx[i];
 
                    if (ny < 0 || ny >= R || nx < 0 || nx >= C || Fire[ny][nx]) continue;
 
                    if (Map[ny][nx] == '.')
                    {
                        q.push({ { ny, nx }, 1 });
                        Fire[ny][nx] = true;
                        Map[ny][nx] = 'F';
                    }
                }
            }
        }
 
        ++ret;
    }
 
    return -1;
}
 
int main(void)
{
    scanf("%d %d"&R, &C);
 
    for (int i = 0; i < R; i++)
    {
        for (int j = 0; j < C; j++)
        {
            cin >> Map[i][j];
 
            if (Map[i][j] == 'J')
            {
                jy = i;
                jx = j;
                Map[i][j] = '.';
            }
            else if (Map[i][j] == 'F')
            {
                q.push({ { i, j }, 1 }); // 불
                Fire[i][j] = true;
            }
        }
    }
 
    int r = BFS();
 
    if (r != -1printf("%d\n", r);
    else printf("IMPOSSIBLE\n");
 
    return 0;
}
cs


728x90
반응형

'알고리즘 문제 > BOJ' 카테고리의 다른 글

16724번 피리 부는 사나이  (0) 2019.01.01
16724번 원영이는 ZOAC과 영원하고 싶다  (0) 2018.12.31
1019번 책 페이지  (0) 2018.12.29
1120번 문자열  (0) 2018.12.29
10448번 유레카 이론  (0) 2018.12.29