12908. 텔레포트 3
12908번: 텔레포트 3
첫째 줄에 xs와 ys가, 둘째 줄에 xe, ye가 주어진다. (0 ≤ xs, ys, xe, ye ≤ 1,000,000,000) 셋째 줄부터 세 개의 줄에는 텔레포트의 정보 x1, y1, x2, y2가 주어진다. (0 ≤ x1, y1, x2, y2 ≤ 1,000,000,000) 입력으로 주
www.acmicpc.net
- 첫 풀이 - fail
- 36퍼에서 틀림
- int(1e9)하면 터짐
- → int(1e10) or float("inf")하니까 됨
- 36퍼에서 틀림
# 노드 간의 거리는 abs(y1-y2) + abs(x1-x2)
xs, ys = map(int, input().split())
xe, ye = map(int, input().split())
graph = []
graph.append((xs, ys))
distance = [[int(1e9) for _ in range(8)] for _ in range(8)]
for i in range(3):
x1, y1, x2, y2 = map(int, input().split())
graph.append((x1, y1))
graph.append((x2, y2))
idx=len(graph)
#거리가 탤레포트보다 빠른지 확인
distance[idx-2][idx- 1] = min(abs(x1 - x2) + abs(y1 - y2), 10)
#반대편도 같은 값 넣어줌
distance[idx-1][idx-2]= distance[idx-2][idx- 1]
graph.append((xe, ye))
# 노드 간 거리 계산
for i in range(8):
for j in range(8):
distance[i][j] = min(distance[i][j], abs(
graph[i][0] - graph[j][0]) + abs(graph[i][1] - graph[j][1]))
# 플로이드 워셜 - 바로가느냐, 거쳐서 가느냐
for k in range(8):
for i in range(8):
for j in range(8):
distance[i][j] = min(
distance[i][j], distance[i][k] + distance[k][j])
print(distance[0][7])
- 맞는 풀이
# 노드 간의 거리는 abs(y1-y2) + abs(x1-x2)
xs, ys = map(int, input().split())
xe, ye = map(int, input().split())
graph = []
graph.append((xs, ys))
distance = [[int(1e9) for _ in range(8)] for _ in range(8)]
for i in range(3):
x1, y1, x2, y2 = map(int, input().split())
graph.append((x1, y1))
graph.append((x2, y2))
idx=len(graph)
#거리가 탤레포트보다 빠른지 확인
distance[idx-2][idx- 1] = min(abs(x1 - x2) + abs(y1 - y2), 10)
#반대편도 같은 값 넣어줌
distance[idx-1][idx-2]= distance[idx-2][idx- 1]
graph.append((xe, ye))
# 노드 간 거리 계산
for i in range(8):
for j in range(8):
distance[i][j] = min(distance[i][j], abs(
graph[i][0] - graph[j][0]) + abs(graph[i][1] - graph[j][1]))
# 플로이드 워셜 - 바로가느냐, 거쳐서 가느냐
for k in range(8):
for i in range(8):
for j in range(8):
distance[i][j] = min(
distance[i][j], distance[i][k] + distance[k][j])
print(distance[0][7])
'코딩테스트 > BOJ' 카테고리의 다른 글
| [백준][BFS/플로이드워셜] 2660. 회장뽑기 (0) | 2023.12.09 |
|---|---|
| [백준][DP] 17404. RGB거리 2 (1) | 2023.12.02 |
| [백준][순열] 15659. 연산자 끼워넣기(3) (0) | 2023.11.05 |
| [백준][진법] 1394. 암호 (0) | 2023.10.29 |
| [백준][BFS][트리] 14367. 회사문화1 (0) | 2023.10.29 |