기술 블로그

9328번 열쇠 본문

알고리즘 문제/BOJ

9328번 열쇠

parkit 2018. 9. 2. 17:29
728x90
반응형

BFS 문제이다.


단지, 새로운 열쇠(a ~ z)를 획득하였을 때만, 주의 해주면 된다.


또한, map[y][x]를 '.'로 바꾸는 것도 신경쓰면 된다.



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




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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <iostream>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <math.h>
#include <algorithm>
 
using namespace std;
 
char map[102][102];
 
int dy[5= { -10100 };
int dx[5= { 0-1010 };
 
int h = 0, w = 0;
 
bool key[123= { false, }; // z 값 = 122
 
int result = 0// 답
 
bool update = true;
 
string alreadyKeys;
 
 
bool visit[102][102= { false, };
 
void init()
{
    memset(visit, falsesizeof(visit));
    memset(key, falsesizeof(key));
    update = true;
    result = 0;
}
 
void BFS(int r, int c)
{
    queue<pair<intint> > q;
 
    q.push({ r,c });
 
    while (!q.empty())
    {
        int qSize = q.size();
 
        while (qSize--)
        {
            int row = q.front().first;
            int column = q.front().second;
 
            q.pop();
 
            for (int i = 0; i < 5; i++)
            {
                int y = row + dy[i];
                int x = column + dx[i];
 
                if (0 <= y && y < h && 0 <= x && x < w && !visit[y][x] && map[y][x] != '*')
                {
                    if ('A' <= map[y][x] && map[y][x] <= 'Z')
                    {
                        if (key[(int)map[y][x] + 32])
                        {
                            q.push({ y, x });
                            map[y][x] = '.';
                            visit[y][x] = true;
                        }
                    }
                    else if ('a' <= map[y][x] && map[y][x] <= 'z')
                    {
                        key[(int)map[y][x]] = true;
                        q.push({ y, x });
                        map[y][x] = '.';
                        visit[y][x] = true;
                        update = true
                        // 새로운 열쇠를 먹었으므로, update 갱신.
                        // 즉, 다시 map을 처음부터 실행
                    }
                    else if (map[y][x] == '.' || map[y][x] == '$')
                    {
                        if (map[y][x] == '$')
                        {
                            ++result;
                            map[y][x] = '.';
                        }
 
                        q.push({ y, x });
                        visit[y][x] = true;
                    }
                }
            }
        }
    }
}
 
int main(void)
{
    int T = 0;
 
    scanf("%d"&T);
 
    for (int tc = 1; tc <= T; tc++)
    {
        init();
 
        scanf("%d %d"&h, &w);
 
        for (int i = 0; i < h; i++)
        {
            for (int j = 0; j < w; j++)
            {
                cin >> map[i][j];
            }
        }
 
        cin >> alreadyKeys;
 
        if (alreadyKeys[0!= '0')
        {
            int Size = alreadyKeys.length();
 
            for (int i = 0; i < Size; i++)
            {
                key[alreadyKeys[i]] = true;
            }
        }    
 
        while (update)
        {
            update = false;
 
            memset(visit, falsesizeof(visit));
 
            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < w; j++)
                {
                    if (i == 0 || j == 0 || i == h - 1 || j == w - 1)
                    {
                        if (map[i][j] == '.' || map[i][j] == '$')
                        {
                            BFS(i, j);
                        }
                        else if ('A' <= map[i][j] && map[i][j] <= 'Z')
                        {
                            if (key[(int)map[i][j] + 32])
                            {
                                BFS(i, j);
                            }        
                        }
                        else if ('a' <= map[i][j] && map[i][j] <= 'b')
                        {
                            key[(int)map[i][j]] = true;
 
                            BFS(i, j);
                        }
                    }
                }
            }
        }
 
        printf("%d\n", result);
    }
 
    return 0;
}
cs


728x90
반응형

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

2146번 다리 만들기  (0) 2018.09.04
14502번 연구소  (0) 2018.09.02
14500번 테트로미노  (0) 2018.08.30
1389번 케빈 베이컨의 6단계 법칙  (0) 2018.08.30
6603번 로또  (0) 2018.08.30