728x90
https://www.acmicpc.net/problem/9205
- 50m๋น ๋งฅ์ฃผ 1๋ณ์ด ์์ง๋๋ค๋ ๋ง์, ํ ๋ฒ์ ์ต๋ 1000m๊น์ง ์ด๋ํ ์ ์๋ค๋ ๋ป์ด๋ค.
- ์ด๋ ๊ฒฝ๋ก:
์ง (-> ํธ์์ 1 -> ํธ์์ 2 -> .... -> ํธ์์ n) -> ํ์คํฐ๋ฒ
- ๊ฐ๊ฐ์ ์ด๋ ๊ฑฐ๋ฆฌ๊ฐ 1000๋ณด๋ค ํฌ์ง ์์ ์ํ๋ฅผ ์ ์งํ๋ฉฐ ๋์ฐฉ์ง๊น์ง ๋๋ฌํ๊ฒ ๋๋ฉด happy๋ฅผ ์ถ๋ ฅํ๋ฉด ๋๋ ๋ฌธ์ ์ด๋ค.
import sys
from collections import deque
input = sys.stdin.readline
def dist(x1, y1, x2, y2):
return abs(x1-x2) + abs(y1-y2)
def bfs(x,y):
q = deque()
q.append((x,y))
visited=[] #๋ฐฉ๋ฌธํ ํธ์์ ์ขํ๋ฅผ ์ ์ฅ
while q:
a,b=q.popleft()
#์ด๋๊ฑฐ๋ฆฌ 1000์ดํ๋ก๋์ฐฉ์ ์ ๋๋ฌํ๋ฉด True๋ฅผ ์ถ๋ ฅ
if dist(a,b,endX,endY) <= 1000:
return True
#ํธ์์ ๋ค ์ค ๋ค๋ฅผ ์ ์๋ ๊ณณ์ด ์๋์ง ์ฒดํฌ
for i in range(n):
storeX, storeY = store[i]
#๋ฐฉ๋ฌธํ ์ ์๊ณ ๊ฑฐ๋ฆฌ 1000์ดํ๋ก ๊ฐ ์ ์๋ ํธ์์ ์ ์ฐพ์ผ๋ฉด ๋ฐฉ๋ฌธ์ฒ๋ฆฌํ ํ์ ๋ฃ๋๋ค
if (storeX, storeY) not in visited:
if dist(a,b,storeX,storeY) <= 1000:
visited.append((storeX, storeY))
q.append((storeX,storeY))
return False
t=int(input())
for _ in range(t):
n = int(input())
startX, startY = map(int, input().split())
store=[]
for i in range(n):
x,y = map(int,input().split())
store.append((x,y))
endX, endY = map(int, input().split())
if bfs(startX, startY)==True:
print('happy')
else:
print('sad')
์ด๋ ค์ก
728x90
'์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ํ์ด > DFS,BFS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค]18405๋ฒ: ๊ฒฝ์์ ์ ์ผ (0) | 2023.03.03 |
---|---|
[๋ฐฑ์ค]7569๋ฒ: ํ ๋งํ (0) | 2023.02.28 |
[๋ฐฑ์ค]11724๋ฒ: ์ฐ๊ฒฐ ์์์ ๊ฐ์ (0) | 2023.02.26 |
[๋ฐฑ์ค]7576๋ฒ: ํ ๋งํ (0) | 2023.02.25 |
[๋ฐฑ์ค]2468๋ฒ: ์์ ์์ญ (0) | 2023.02.25 |