https://www.acmicpc.net/problem/9205
๋งจํดํผ ๊ฑฐ๋ฆฌ์ธ ์ค ๋ชจ๋ฅด๊ณ ์ฝ์งํ๋ค. ๋ฌธ์ ๋ฅผ ์ ์ฝ์ด๋ณด์.
์์ด๋์ด
1. ๊ทธ๋ํ ํ์
- ์ฌํํ๊ฒ ๋งฅ์ฃผ 20๋ณ = 1000m๋ผ๊ณ ์๊ฐํ๊ณ 1000m๋ฅผ ๊ธฐ์ค์ผ๋ก ๊ฐ ์ ์๋์ง ์๋์ง ํ์ธํ๋ฉด ๋๋ค.
- bfs๋ฅผ ์ฌ์ฉํ์ฌ ํด๊ฒฐํ๋ค.
์ ์ฒด ์ฝ๋
from collections import deque
def cal_distance(x, y, nx, ny):
return abs(nx - x) + abs(ny - y)
def bfs():
if cal_distance(home_x, home_y, rock_x, rock_y) <= 1000:
visited[n] = True
return
que = deque()
for i in range(len(conveni)):
temp = cal_distance(home_x, home_y, conveni[i][0], conveni[i][1])
if temp <= 1000:
que.append(conveni[i])
visited[i] = True
while que:
cx, cy = que.popleft()
for i in range(len(conveni)):
temp = cal_distance(cx, cy, conveni[i][0], conveni[i][1])
if temp <= 1000 and not visited[i]:
que.append(conveni[i])
visited[i] = True
t = int(input())
for _ in range(t):
n = int(input())
visited = [False] * (n + 1)
conveni = []
home_x, home_y = map(int, input().split())
for _ in range(n):
conveni.append([int(a) for a in input().split()])
rock_x, rock_y = map(int, input().split())
conveni.append([rock_x, rock_y])
bfs()
if visited[n] == True:
print('happy')
else:
print('sad')
์ฝ๋ ์ค๋ช
1. ๋ฉ์ธ
t = int(input())
for _ in range(t):
n = int(input())
visited = [False] * (n + 1)
conveni = []
home_x, home_y = map(int, input().split())
for _ in range(n):
conveni.append([int(a) for a in input().split()])
rock_x, rock_y = map(int, input().split())
conveni.append([rock_x, rock_y])
bfs()
if visited[n] == True:
print('happy')
else:
print('sad')
์ ๋ ฅ๋ฐ์ ํ ์คํธ ์ผ์ด์ค ๊ฐ์ t๊ฐ๋งํผ ๋ฐ๋ณตํ๋ค.
์ ๋ ฅ๋ฐ์ ํ์คํฐ๋ฒ ์ขํ๋ ํธ์์ ๋ฆฌ์คํธ์ ์ถ๊ฐํด ์ค๋ค.
conveni[n]์ ํ์คํฐ๋ฒ์ ์ธ๋ฑ์ค๊ฐ ๋๋ค.
๋ฐ๋ผ์ ๊ทธ๋ํ ํ์(bfs)์ ํ๊ณ visited[n] = True์ด๋ฉด ํ์คํฐ๋ฒ์ ๊ฐ ์ ์๋ค๋ ๋ป์ด๋ฏ๋ก
'happy'๋ฅผ ์ถ๋ ฅํด ์ค๋ค. False๋ฉด 'sad'๋ฅผ ์ถ๋ ฅํด ์ค๋ค.
2. bfs
def bfs():
if cal_distance(home_x, home_y, rock_x, rock_y) <= 1000:
visited[n] = True
return
que = deque()
for i in range(len(conveni)):
temp = cal_distance(home_x, home_y, conveni[i][0], conveni[i][1])
if temp <= 1000:
que.append(conveni[i])
visited[i] = True
while que:
cx, cy = que.popleft()
for i in range(len(conveni)):
temp = cal_distance(cx, cy, conveni[i][0], conveni[i][1])
if temp <= 1000 and not visited[i]:
que.append(conveni[i])
visited[i] = True
๋จผ์ , ์ง์์ ๋ฐ๋ก ํ์คํฐ๋ฒ๋ก ๊ฐ ์ ์์ผ๋ฉด ํ์ ์์ด ๋ฐ๋ก ๋ฆฌํดํด์ค๋ค.
ํ์์ ์ ์ง์์ ๊ฐ ์ ์๋ ํธ์์ ์ ์ ๋ถ ํ์ ์ถ๊ฐํด ์ค๋ค.
์ ๋ ฅ๋ฐ์ ํธ์์ ์ขํ๋ค์ while๋ฌธ์์ ํ๋์ฉ pop์ ํ๋ฉฐ ๊ทธ๋ํ๋ฅผ ํ์ํด ๋๊ฐ๋ค.
๋ฐฉ๋ฌธํ๋ฉด visited = True๋ก ๋ณ๊ฒฝํด ์ค๋ค.
3. calculate distance(๋งจํดํผ ๊ฑฐ๋ฆฌ)
def cal_distance(x, y, nx, ny):
return abs(nx - x) + abs(ny - y)
๊ฐ๋จํ ๊ฑฐ๋ฆฌ ๊ตฌํ๋ ์ฝ๋๋ฅผ ๊ตฌํํ๋ค. x, y ๊ฑฐ๋ฆฌ์ฐจ๋ฅผ ๋ํด์ฃผ๋ฉด ๋๋ค.
'๐งฉ Problem Solving > [๋ฐฑ์ค]' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 28250 ์ด๋ธ, ํ์์ผ ๊ทธ๋ฆฌ๊ณ ํธ๋ฅธ MEX์ ์๋ด (python ํ์ด์ฌ) (0) | 2023.07.03 |
---|---|
[๋ฐฑ์ค] 28017 ๊ฒ์์ ํด๋ฆฌ์ดํ์ (python ํ์ด์ฌ) (0) | 2023.07.01 |
[๋ฐฑ์ค] 1759 ์ํธ๋ง๋ค๊ธฐ (python ํ์ด์ฌ) (0) | 2023.03.30 |
[๋ฐฑ์ค] 11559 puyopuyo (ํ์ด์ฌ python) (0) | 2023.03.26 |
[๋ฐฑ์ค] 2583 ์์ญ ๊ตฌํ๊ธฐ(python ํ์ด์ฌ) (0) | 2023.03.16 |