parkit 2018. 11. 16. 00:06
728x90
반응형

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


벽 부수고 이동하기 문제류와 비슷하다.


이런 BFS 유형은 틀리면 안 된다.



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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <math.h>
#include <algorithm>
#include <map>
 
using namespace std;
 
char Map[1001][1001];
 
int dy[4= { -1010 };
int dx[4= { 0-101 };
 
int w = 0, h = 0;
 
bool visit[1001][1001][2= { false, };
 
vector<pair<intint> > v;
 
int sy = 0, sx = 0;
 
// 0 : 불, 1 : 상근이
 
int BFS()
{
    queue<pair<pair<intint>int> > q;
 
    for (int i = 0; i < v.size(); i++)
    {
        q.push({ { v.at(i).first, v.at(i).second }, 0 });
 
        visit[v.at(i).first][v.at(i).second][0= true;
    }
 
    q.push({ {sy, sx}, 1 });
 
    visit[sy][sx][1= true;
 
    int ret = 1;
 
    while (!q.empty())
    {
        int qSize = q.size();
 
        while (qSize--)
        {
            int r = q.front().first.first;
            int c = q.front().first.second;
            int sit = q.front().second;
 
            q.pop();
 
            if ( (r == 0 || r == h - 1 || c == 0 || c == w - 1&& sit == 1)
            {
                return ret;    
            }
 
            if (sit == 0)
            {
                // 불
 
                for (int i = 0; i < 4; i++)
                {
                    int y = r + dy[i];
                    int x = c + dx[i];
 
                    if (y < 0 || y >= h || x < 0 || x >= w) continue;
 
                    if (Map[y][x] == '.' && !visit[y][x][0])
                    {
                        visit[y][x][0= true;
                        q.push({ {y, x}, 0 });
                        Map[y][x] = '*';
                    }
                }
            }
            else if (sit == 1)
            {
                // 상근이
 
                for (int i = 0; i < 4; i++)
                {
                    int y = r + dy[i];
                    int x = c + dx[i];
 
                    if (y < 0 || y >= h || x < 0 || x >= w || Map[y][x] == '*' || Map[y][x] == '#' || visit[y][x][1]) continue;
 
                    q.push({ {y,x}, 1 });
                    visit[y][x][1= true;
                }
            }
        }
 
        ++ret;
    }
 
    return -1;
}
 
int main(void)
{
    int T = 0;
 
    scanf("%d"&T);
 
    for (int i = 0; i < T; i++)
    {
        scanf("%d %d"&w, &h);
 
        for (int j = 0; j < h; j++)
        {
            for (int k = 0; k < w; k++)
            {
                cin >> Map[j][k];
 
                if (Map[j][k] == '*')
                {
                    v.push_back({ j, k });
                }
                else if (Map[j][k] == '@')
                {
                    sy = j;
                    sx = k;
                    Map[j][k] = '.';
                }
            }
        }
 
        int result = BFS();
 
        if (result != -1printf("%d\n", result);
        else printf("IMPOSSIBLE\n");
 
        if(!v.empty()) v.clear();
        memset(Map, 0sizeof(Map));
        memset(visit, falsesizeof(visit));
    }
 
    return 0;
}
cs


728x90
반응형