Skip to content

Commit dbd48ff

Browse files
committed
[BOJ] 경비원 / 실버 1 / 40분 힌트사용
https://www.acmicpc.net/problem/2564
1 parent a9a847c commit dbd48ff

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
'''
2+
1. 아이디어
3+
- 방향이 두가지이고 최소거리는 둘레 // 2 보다 작거나 같아야 하므로, 시계방향 거리를 구해서 둘레 // 2와 비교하며 처리하자
4+
- 동근이와 상점의 시계방향 거리를 구할때는 (0,0)을 기준점으로 잡고 기준점부터 동근이의 거리, 기준점부터 상점의 거리를 구한 후 차이를 구하자
5+
6+
2. 시간복잡도
7+
상점 위치 입력 → O(nums)
8+
거리 계산 → O(nums)
9+
최대 O(100)로 1초안에 가능 (n이 100이하니까)
10+
11+
3. 구현
12+
3-1. 입력 받기
13+
3-2. 전체 둘레 계산
14+
3-3. 시계 방향으로 거리 계산 함수 정의
15+
3-4. (0,0)에서부터 시계방향으로 동근이와 상점의 거리 구하기
16+
3-5. 3-4를 기반으로 시계방향으로 동근이와 상점의 거리 구하기
17+
3-6. 시계방향 거리가 전체 둘레의 절반보다 작다면 시계방향을 더하고, 크다면 반시계방향 거리 더하기
18+
3-7. 결과 출력
19+
'''
20+
21+
import sys
22+
23+
inp = sys.stdin.readline
24+
25+
row, col = map(int, inp().strip().split())
26+
27+
# 전체 둘레
28+
round_length = row * 2 + col * 2
29+
answer = 0
30+
31+
# 동근이의 위치와 상점의 좌표 저장
32+
locations = []
33+
n = int(inp().strip())
34+
35+
for _ in range(n + 1):
36+
dir, num = map(int, inp().strip().split())
37+
locations.append((dir, num))
38+
39+
40+
def calculate_distance(dir, num):
41+
if dir == 1:
42+
return num
43+
elif dir == 2:
44+
return row + col + (row - num)
45+
elif dir == 3:
46+
return row + col + row + (col - num)
47+
else:
48+
return row + num
49+
50+
51+
for i in range(n):
52+
# 동근이 좌표
53+
dir, num = locations[n]
54+
55+
# 상점 좌표
56+
store_dir, store_num = locations[i]
57+
58+
# (0,0)에서부터 시계방향으로 동근이의 거리
59+
d1 = calculate_distance(dir, num)
60+
61+
# (0,0)에서부터 시계방향으로 상점의 거리
62+
d2 = calculate_distance(store_dir, store_num)
63+
64+
# 시계방향 동근이 - 상점간 거리
65+
distance = abs(d2 - d1)
66+
67+
if distance < round_length // 2:
68+
answer += distance
69+
else:
70+
answer += round_length - distance
71+
72+
print(answer)

0 commit comments

Comments
 (0)