Skip to content

Commit 21f655a

Browse files
authored
Merge pull request #109 from YoonYn9915/main
YoonYn9915 / 12월 3주차 / 3문제
2 parents 444f7e3 + d653d5b commit 21f655a

File tree

3 files changed

+188
-0
lines changed

3 files changed

+188
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from collections import deque
2+
3+
n, m = map(int, input().split())
4+
graph = []
5+
6+
visited = [[[0] * 2 for _ in range(m)] for _ in range(n)]
7+
visited[0][0][0] = 1
8+
9+
for i in range(n):
10+
graph.append(list(map(int, input())))
11+
12+
dx = [0, 0, 1, -1]
13+
dy = [1, -1, 0, 0]
14+
15+
16+
def bfs(x, y, z):
17+
queue = deque()
18+
queue.append((x, y, z))
19+
20+
while queue:
21+
a, b, c = queue.popleft()
22+
# 끝 점에 도달하면 이동 횟수를 출력
23+
if a == n - 1 and b == m - 1:
24+
return visited[a][b][c]
25+
for i in range(4):
26+
nx = a + dx[i]
27+
ny = b + dy[i]
28+
if nx < 0 or nx >= n or ny < 0 or ny >= m:
29+
continue
30+
# 다음 이동할 곳이 벽이고, 벽파괴 기회를 사용하지 않은 경우
31+
if graph[nx][ny] == 1 and c == 0 :
32+
visited[nx][ny][1] = visited[a][b][0] + 1
33+
queue.append((nx, ny, 1))
34+
# 다음 이동할 곳이 벽이 아니고, 아직 한 번도 방문하지 않은 곳이면
35+
elif graph[nx][ny] == 0 and visited[nx][ny][c] == 0:
36+
visited[nx][ny][c] = visited[a][b][c] + 1
37+
queue.append((nx, ny, c))
38+
return -1
39+
40+
41+
print(bfs(0, 0, 0))
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
from collections import deque
2+
import sys
3+
4+
input = sys.stdin.readline
5+
6+
n, m = map(int, input().split()) # 행, 열
7+
8+
board = [list(input().rstrip()) for _ in range(n)]
9+
visited = []
10+
11+
dx = [1, -1, 0, 0]
12+
dy = [0, 0, -1, 1]
13+
cnt = 0
14+
15+
16+
def getPos():
17+
rx, ry, bx, by = 0, 0, 0, 0
18+
for x in range(n):
19+
for y in range(m):
20+
if board[x][y] == "R":
21+
rx, ry = x, y
22+
if board[x][y] == "B":
23+
bx, by = x, y
24+
return rx, ry, bx, by
25+
26+
27+
def move(x, y, dx, dy):
28+
cnt = 0
29+
# 이동하는 위치가 벽이아니고, 구멍에 들어가지 않을 동안 반복
30+
while board[x + dx][y + dy] != "#" and board[x][y] != "O":
31+
x += dx
32+
y += dy
33+
cnt += 1
34+
return x, y, cnt
35+
36+
37+
def bfs():
38+
rx, ry, bx, by = getPos()
39+
40+
q = deque()
41+
q.append((rx, ry, bx, by, 1))
42+
visited.append((rx, ry, bx, by))
43+
44+
while q:
45+
rx, ry, bx, by, result = q.popleft()
46+
47+
if result > 10:
48+
break
49+
50+
for i in range(4):
51+
nrx, nry, rcnt = move(rx, ry, dx[i], dy[i])
52+
nbx, nby, bcnt = move(bx, by, dx[i], dy[i])
53+
54+
# 파란 구슬이 구멍에 들어갈 경우
55+
if board[nbx][nby] == "O":
56+
continue
57+
58+
# 빨간 구슬이 들어갈 경우 성공
59+
if board[nrx][nry] == "O":
60+
print(result)
61+
return
62+
63+
# 둘이 겹쳐있을경우 더 많이 이동한녀석을 1칸 뒤로 보낸다.
64+
if nrx == nbx and nry == nby:
65+
if rcnt > bcnt:
66+
nrx -= dx[i]
67+
nry -= dy[i]
68+
else:
69+
nbx -= dx[i]
70+
nby -= dy[i]
71+
72+
# 탐색하지 않은 방향 탐색
73+
if (nrx, nry, nbx, nby) not in visited:
74+
visited.append((nrx, nry, nbx, nby))
75+
q.append((nrx, nry, nbx, nby, result + 1))
76+
print(-1)
77+
78+
79+
bfs()
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import sys
2+
3+
# 입력 받기
4+
R, C, T = map(int, sys.stdin.readline().split())
5+
pan = [list(map(int, sys.stdin.readline().split())) for _ in range(R)]
6+
7+
# 방향 설정 (상, 우, 하, 좌)
8+
dx = [-1, 0, 1, 0]
9+
dy = [0, 1, 0, -1]
10+
11+
# 공기 청정기 위치 찾기
12+
cleaner = []
13+
for i in range(R):
14+
if pan[i][0] == -1:
15+
cleaner.append(i)
16+
17+
# 먼지 확산 함수
18+
def spread():
19+
temp = [[0] * C for _ in range(R)]
20+
for x in range(R):
21+
for y in range(C):
22+
if pan[x][y] > 0:
23+
value = pan[x][y] // 5
24+
count = 0
25+
for d in range(4):
26+
nx, ny = x + dx[d], y + dy[d]
27+
if 0 <= nx < R and 0 <= ny < C and pan[nx][ny] != -1:
28+
temp[nx][ny] += value
29+
count += 1
30+
pan[x][y] -= value * count
31+
for x in range(R):
32+
for y in range(C):
33+
pan[x][y] += temp[x][y]
34+
35+
# 공기 청정기 작동 함수
36+
def clean():
37+
# 위쪽 청정기
38+
top = cleaner[0]
39+
for i in range(top - 1, 0, -1):
40+
pan[i][0] = pan[i - 1][0]
41+
for i in range(C - 1):
42+
pan[0][i] = pan[0][i + 1]
43+
for i in range(top):
44+
pan[i][C - 1] = pan[i + 1][C - 1]
45+
for i in range(C - 1, 1, -1):
46+
pan[top][i] = pan[top][i - 1]
47+
pan[top][1] = 0
48+
49+
# 아래쪽 청정기
50+
bottom = cleaner[1]
51+
for i in range(bottom + 1, R - 1):
52+
pan[i][0] = pan[i + 1][0]
53+
for i in range(C - 1):
54+
pan[R - 1][i] = pan[R - 1][i + 1]
55+
for i in range(R - 1, bottom, -1):
56+
pan[i][C - 1] = pan[i - 1][C - 1]
57+
for i in range(C - 1, 1, -1):
58+
pan[bottom][i] = pan[bottom][i - 1]
59+
pan[bottom][1] = 0
60+
61+
# 시뮬레이션 실행
62+
for _ in range(T):
63+
spread()
64+
clean()
65+
66+
# 남은 미세먼지 계산
67+
result = sum(sum(row) for row in pan) + 2 # 공기청정기 위치(-1)를 더하지 않음
68+
print(result)

0 commit comments

Comments
 (0)