-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9328.cpp
71 lines (61 loc) · 1.93 KB
/
9328.cpp
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
#include <iostream>
#include <queue>
#include <vector>
#include <string>
using namespace std;
typedef long long ll;
void solve(){
int N, M; cin >> N >> M;
bool keys[26];
for (int i = 0; i < 26; i++) keys[i] = false;
string map[N]; for (int i =0 ;i < N; i++) cin >> map[i];
string temp; cin >> temp;
if (temp != "0") for (int i = 0; i < temp.size(); i++) keys[temp[i] - 'a'] = true;
int xxxx[4] = {-1, 0, 1, 0};
int yyyy[4] = {0, 1, 0, -1};
int result = 0;
while (1) {
bool keep = false;
bool visited[N][M]; for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) visited[i][j] = false;
queue<pair<int, int>> q;
for (int i = 0; i < N; i++) {
q.push({i, 0});
q.push({i, M-1});
}
for (int i = 0; i < M; i++) {
q.push({0,i});
q.push({N-1,i});
}
while (q.size()) {
auto [x, y] = q.front(); q.pop();
if (map[x][y] == '*' || map[x][y] >= 'A' && map[x][y] <= 'Z' && !keys[map[x][y] - 'A']) continue;
if (map[x][y] >= 'a' && map[x][y] <= 'z') {
keys[map[x][y] -'a'] = true;
keep = true;
map[x][y] = '.';
}
if (map[x][y] == '$') {
map[x][y] = '.';
result++;
}
visited[x][y] = true;
for (int i = 0; i < 4; i++) {
int nx = x + xxxx[i];
int ny = y + yyyy[i];
if (nx < 0 || nx >= N || ny < 0 || ny >= M) continue;
if (visited[nx][ny]) continue;
visited[nx][ny] = true;
q.push({nx, ny});
}
}
if (!keep) break;
}
cout << result << '\n';
}
int main()
{
cin.tie(0) -> sync_with_stdio(0);
int T; cin >> T;
while(T--) solve();
return 0;
}