문제:
코드:
아이템 주워먹고 맵에서 지우기!
/* 1. 아이템 먹고 맵에서 지우기 */
#include <iostream>
#include <string>
#include <vector>
#define is_lc(ch) ('a' <= (ch) && (ch) <= 'z')
#define is_uc(ch) ('A' <= (ch) && (ch) <= 'Z')
#define uc(ch) (ch - 'a' + 'A')
#define IN(r, c) (0 <= (r) && (r) < R && 0 <= (c) && (c) < C)
using namespace std;
typedef pair<int, int> p;
int T, tc, R, C;
char map[104][104];
//vector<p> door['Z' + 1]; // 초기화 완료
int key['Z' + 1]; // tc
//vector<p> str; // 초기화 완료
int ans;
int dr[] = { -1, 0, 1, 0 };
int dc[] = { 0, 1, 0, -1 };
// dfs1
int vst[104][104], v; // tc
bool dfs(int r, int c) {
vst[r][c] = v;
bool ret = 0;
if (map[r][c] == '$') {
++ans;
map[r][c] = '.';
}
else if (is_lc(map[r][c])) {
key[uc(map[r][c])] = tc;
map[r][c] = '.';
ret = 1;
}
for (int d = 0; d < 4; ++d) {
int nr = r + dr[d], nc = c + dc[d];
if (map[nr][nc] == '*') continue;
if (is_uc(map[nr][nc]) && key[map[nr][nc]] != tc) continue;
if (vst[nr][nc] == v) continue;
ret |= dfs(nr, nc);
}
return ret;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> T;
for (tc = 1; tc <= T; ++tc) {
cin >> R >> C;
for (auto r : { 0, R + 3 })
fill(map[r], map[r] + C + 4, '*');
for (auto r : { 1, R + 2 }) {
map[r][0] = map[r][C + 3] = '*';
fill(map[r] + 1, map[r] + C + 3, '.');
}
for (int r = 2; r < R + 2; ++r) {
map[r][0] = map[r][C + 3] = '*';
map[r][1] = map[r][C + 2] = '.';
for (int c = 2; c < C + 2; ++c)
cin >> map[r][c];
}
string keys; cin >> keys;
for (auto ch : keys)
key[uc(ch)] = tc;
ans = 0;
for (++v; dfs(1, 1); ++v);
cout << ans << '\n';
}
return 0;
}