문제
크기가 1×1인 정사각형으로 나누어진 W×H 크기의 지도가 있다. 지도의 각 칸은 빈 칸이거나 벽이며, 두 칸은 'C
'로 표시되어 있는 칸이다.
'C
'로 표시되어 있는 두 칸을 레이저로 통신하기 위해서 설치해야 하는 거울 개수의 최솟값을 구하는 프로그램을 작성하시오. 레이저로 통신한다는 것은 두 칸을 레이저로 연결할 수 있음을 의미한다.
레이저는 C에서만 발사할 수 있고, 빈 칸에 거울('/
', '\
')을 설치해서 방향을 90도 회전시킬 수 있다.
아래 그림은 H = 8, W = 7인 경우이고, 빈 칸은 '.
', 벽은 '*
'로 나타냈다. 왼쪽은 초기 상태, 오른쪽은 최소 개수의 거울을 사용해서 두 'C
'를 연결한 것이다.
7 . . . . . . . 7 . . . . . . .
6 . . . . . . C 6 . . . . . /-C
5 . . . . . . * 5 . . . . . | *
4 * * * * * . * 4 * * * * * | *
3 . . . . * . . 3 . . . . * | .
2 . . . . * . . 2 . . . . * | .
1 . C . . * . . 1 . C . . * | .
0 . . . . . . . 0 . \-------/ .
0 1 2 3 4 5 6 0 1 2 3 4 5 6
출력
첫째 줄에 C를 연결하기 위해 설치해야 하는 거울 개수의 최솟값을 출력한다.
W3sicHJvYmxlbV9pZCI6IjYwODciLCJwcm9ibGVtX2xhbmciOiIwIiwidGl0bGUiOiJcdWI4MDhcdWM3NzRcdWM4MDAgXHVkMWI1XHVjMmUwIiwiZGVzY3JpcHRpb24iOiI8cD5cdWQwNmNcdWFlMzBcdWFjMDAgMSZ0aW1lczsxXHVjNzc4IFx1YzgxNVx1YzBhY1x1YWMwMVx1ZDYxNVx1YzczY1x1Yjg1YyBcdWIwOThcdWIyMDRcdWM1YjRcdWM5YzQgVyZ0aW1lcztIIFx1ZDA2Y1x1YWUzMFx1Yzc1OCZuYnNwO1x1YzljMFx1YjNjNFx1YWMwMCBcdWM3ODhcdWIyZTQuIFx1YzljMFx1YjNjNFx1Yzc1OCBcdWFjMDEgXHVjZTc4XHVjNzQwIFx1YmU0OCBc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B8IC5cclxuMiAuIC4gLiAuICogLiAuICAgICAgICAgMiAuIC4gLiAuICogfCAuXHJcbjEgLiBDIC4gLiAqIC4gLiAgICAgICAgIDEgLiBDIC4gLiAqIHwgLlxyXG4wIC4gLiAuIC4gLiAuIC4gICAgICAgICAwIC4gXFwtLS0tLS0tXC8gLlxyXG4gIDAgMSAyIDMgNCA1IDYgICAgICAgICAgIDAgMSAyIDMgNCA1IDZcclxuPFwvcHJlPlxyXG5cclxuPHA+RGV0ZXJtaW5lIHRoZSBtaW5pbXVtIG51bWJlciBvZiBtaXJyb3JzIE0gdGhhdCBtdXN0IGJlIGluc3RhbGxlZCB0byBtYWludGFpbiBsYXNlciBjb21tdW5pY2F0aW9uIGJldHdlZW4gdGhlIHR3byBjb3dzLCBhIGZlYXQgd2hpY2ggaXMgYWx3YXlzIHBvc3NpYmxlIGluIHRoZSBnaXZlbiB0ZXN0IGRhdGEuPFwvcD5cclxuIiwiaW5wdXQiOiI8dWw+XHJcblx0PGxpPkxpbmUgMTogVHdvIHNwYWNlIHNlcGFyYXRlZCBpbnRlZ2VyczogVyBhbmQgSDxcL2xpPlxyXG5cdDxsaT5MaW5lcyAyLi5IKzE6IFRoZSBlbnRpcmUgcGFzdHVyZS48XC9saT5cclxuPFwvdWw+XHJcbiIsIm91dHB1dCI6Ijx1bD5cclxuXHQ8bGk+TGluZSAxOiBBIHNpbmdsZSBpbnRlZ2VyOiBNPFwvbGk+XHJcbjxcL3VsPlxyXG4iLCJoaW50IjoiIiwib3JpZ2luYWwiOiIxIiwiaHRtbF90aXRsZSI6IjAiLCJwcm9ibGVtX2xhbmdfdGNvZGUiOiJFbmdsaXNoIn1d
접근 방법
- 다익스트라 최단경로 알고리즘을 활용해 현재 위치에서 상하좌우로 이동한다.
- 이때 *나 범위를 벗어나게 될 경우 거울 설치 개수를 하나 늘린다.
- 이후 다른 c에 도착한다면 해당 위치에서의 거울 설치 개수를 출력한다.
코드
# https://www.acmicpc.net/problem/6087
# 접근 방법
# 다익스트라 최단경로 알고리즘을 활용해 현재 위치에서 상하좌우로 이동한다.
# 이때 *나 범위를 벗어나게 될 경우 거울 설치 개수를 하나 늘린다.
# 이후 다른 c에 도착한다면 해당 위치에서의 거울 설치 개수를 출력한다.
def dijkstra():
r, c = target[0]
graph[r][c] = 0
q = []
for i in range(4):
heapq.heappush(q, [0, r, c, i])
direction = [[1, 0], [-1, 0], [0, 1], [0, -1]]
while q:
count, row, col, dir = heapq.heappop(q)
if graph[row][col] not in ['.', '*'] and graph[row][col] < count:
continue
for i in range(4):
dr, dc = direction[i]
num = count + 1
if i == dir:
num = count
if 0<=row+dr= num):
graph[row+dr][col+dc] = num
heapq.heappush(q, [num, row+dr, col+dc, i])
import heapq
w, h = map(int, input().split())
graph = [[x for x in input()] for _ in range(h)]
target = []
for r in range(h):
for c in range(w):
if graph[r][c] == 'C':
target.append([r, c])
dijkstra()
r, c = target[1]
print(graph[r][c])