백준
1. Python
from collections import deque
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
def bfs(x, y):
queue.append((x,y))
c[x][y] = 1
while queue:
qlen = len(queue)
while qlen:
x, y = queue.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < m:
if graph[nx][ny] == '.' and c[nx][ny] == 0:
c[nx][ny] = c[x][y] + 1
queue.append((nx, ny))
elif graph[nx][ny] == 'D':
print(c[x][y])
return
qlen -= 1
water()
print("KAKTUS")
return
def water():
qlen = len(wqueue)
while qlen:
x, y = wqueue.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < m:
if graph[nx][ny] == '.':
graph[nx][ny] = '*'
wqueue.append((nx, ny))
qlen -= 1
n, m = map(int, input().split())
'''타임에러
#2차원 리스트의 맵 정보 입력받기
graph = []
for i in range(n):
graph.append(list(map(str, input())))
'''
graph = [list(map(str, input())) for _ in range(n)]
c = [[0]*m for _ in range(n)]
queue, wqueue = deque(), deque()
for i in range(n):
for j in range(m):
if graph[i][j] == 'S':
x1, y1 = i, j
graph[i][j] = '.'
elif graph[i][j] == '*':
wqueue.append((i, j))
water()
bfs(x1, y1)
- 앞으로 물이 찰 곳을 못간다는 것은 물을 먼저 채우는 것으로 해결할 수 있다.
2. C++ - 공간복잡도 에러
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
int r, c;
char forest[50][51];
pii ddg;
vector<pii> water;
pii biber;
queue<pii> water_q, ddg_q;
int ans;
int water_vt[50][50] = {0, }, ddg_vt[50][50] = {0, };
int dr[] = {-1, 1, 0, 0}, dc[] = {0, 0, -1, 1};
bool check_rc(int param_r, int param_c) {
return 0 <= param_r && param_r < r && 0 <= param_c && param_c < c;
}
int main() {
scanf("%d%d", &r, &c);
for (int i = 0 ; i < r ; i++) {
scanf("%s", forest[i]);
}
for (int i = 0 ; i < r ; i++) {
for (int j = 0 ; j < c ; j++) {
if (forest[i][j] == 'S') {
ddg = pii(i, j);
}
if (forest[i][j] == '*') {
water.push_back(pii(i, j));
}
if (forest[i][j] == 'D') {
biber = pii(i, j);
}
}
}
for (int i = 0 ; i < water.size() ; i++) {
pii cur_water = water[i];
water_q.push(cur_water);
water_vt[cur_water.first][cur_water.second] = 1;
}
ddg_q.push(ddg);
ddg_vt[ddg.first][ddg.second] = 1;
while (!ddg_q.empty()) {
int water_qsz = water_q.size();
for (int i = 0 ; i < water_qsz ; i++) {
pii cur_water = water_q.front();
water_q.pop();
for (int j = 0 ; j < 4 ; j++) {
int new_r, new_c;
new_r = cur_water.first + dr[j];
new_c = cur_water.second + dc[j];
if (!check_rc(new_r, new_c)) continue;
if (forest[new_r][new_c] == 'D' || forest[new_r][new_c] == 'X' || water_vt[new_r][new_c] != 0) continue;
water_vt[new_r][new_c] = water_vt[cur_water.first][cur_water.second] + 1;
water_q.push(pii(new_r, new_c));
}
}
int ddg_qsz = ddg_q.size();
for (int i = 0 ; i < ddg_qsz ; i++) {
pii cur_ddg = ddg_q.front();
ddg_q.pop();
for (int j = 0 ; j < 4 ; j++) {
int new_r, new_c;
new_r = cur_ddg.first + dr[j];
new_c = cur_ddg.second + dc[j];
if (!check_rc(new_r, new_c)) continue;
if (forest[new_r][new_c] == 'X' || water_vt[new_r][new_c] != 0) continue;
if (forest[new_r][new_c] == 'D') {
ans = ddg_vt[cur_ddg.first][cur_ddg.second] + 1;
ans--;
printf("%d", ans);
return 0;
}
else {
ddg_vt[new_r][new_c] = ddg_vt[cur_ddg.first][cur_ddg.second] + 1;
ddg_q.push(pii(new_r, new_c));
}
}
}
}
printf("KAKTUS");
}
}
3. C++ - 공간 복잡도 해결