Skip to content

Commit 3e027eb

Browse files
committed
docs: Add boj/7562.md
1 parent 57d2c22 commit 3e027eb

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

โ€Ždocs/ps/boj/7562.mdโ€Ž

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
title: '7562 - ๋‚˜์ดํŠธ์˜ ์ด๋™'
3+
eng_title: '7562 - ๋‚˜์ดํŠธ์˜ ์ด๋™'
4+
image: https://til.qriosity.dev/img/m_banner_background.jpg
5+
sidebar_label: '7562 - ๋‚˜์ดํŠธ์˜ ์ด๋™'
6+
sidebar_position: 7562
7+
created_date: 2024-10-24
8+
---
9+
10+
# 7562 - ๋‚˜์ดํŠธ์˜ ์ด๋™
11+
12+
:::info
13+
14+
- **๋ฌธ์ œ ๋ณด๊ธฐ**: [7562 - ๋‚˜์ดํŠธ์˜ ์ด๋™](https://www.acmicpc.net/problem/7562)
15+
- **์†Œ์š” ์‹œ๊ฐ„**: 15๋ถ„ 22์ดˆ
16+
- **ํ’€์ด ์–ธ์–ด**: `python`
17+
- **์ฒด๊ฐ ๋‚œ์ด๋„**: 1๏ธโƒฃ
18+
- **๋ฆฌ๋ทฐ ํšŸ์ˆ˜**: โœ…
19+
20+
:::
21+
22+
<br />
23+
24+
### ํ’€์ด ํ‚ค์›Œ๋“œ
25+
26+
<details>
27+
<summary>์Šคํฌ์ฃผ์˜</summary>
28+
29+
`BFS`
30+
31+
</details>
32+
33+
<br />
34+
35+
### ํ’€์ด ์ฝ”๋“œ
36+
37+
:::info
38+
39+
- **๋ฉ”๋ชจ๋ฆฌ**: 34096 KB
40+
- **์‹œ๊ฐ„**: 1856 ms
41+
42+
:::
43+
44+
```python
45+
from collections import deque
46+
import sys
47+
input = sys.stdin.readline
48+
49+
l = 0
50+
endX, endY = 0, 0
51+
dx = [-1, 1, -2, 2, -2, 2, -1, 1]
52+
dy = [-2, -2, -1, -1, 1, 1, 2, 2]
53+
board = [] # visited
54+
55+
def bfs(sx, sy):
56+
global board
57+
q = deque()
58+
q.append((sx, sy))
59+
board[sx][sy] = 0
60+
61+
while q:
62+
x, y = q.popleft()
63+
if x == endX and y == endY:
64+
return
65+
for i in range(8):
66+
nx = x + dx[i]
67+
ny = y + dy[i]
68+
if 0 <= nx < l and 0 <= ny < l and board[nx][ny] == -1:
69+
board[nx][ny] = board[x][y] + 1
70+
q.append((nx, ny))
71+
72+
73+
def solution():
74+
global l, endX, endY, board
75+
l = int(input())
76+
startX, startY = map(int, input().split())
77+
endX, endY = map(int, input().split())
78+
board = [[-1 for _ in range(l)] for _ in range(l)]
79+
80+
bfs(startX, startY)
81+
print(board[endX][endY])
82+
83+
84+
if __name__ == '__main__':
85+
tc = int(input())
86+
for _ in range(tc):
87+
solution()
88+
```
89+
90+
<li><span style={{fontSize:32+'px'}}><b>์ตœ๋‹จ๊ฑฐ๋ฆฌ</b></span>์ด๋ฏ€๋กœ ๋„ˆ๋น„์šฐ์„ ํƒ์ƒ‰์ด ์ง๊ด€์ ์ด๋‹ค.</li>
91+
92+
<br />
93+
94+
### ํ’€์ด ํ•ด์„ค
95+
96+
#### `list` ๋Œ€์‹  `deque`์„ ์‚ฌ์šฉํ•œ ์ด์œ ?
97+
- ํŒŒ์ด์ฌ ์œ„ํ‚ค์— ๋”ฐ๋ฅด๋ฉด, queue ๋ชฉ์ ์˜ ์‚ฌ์šฉ์€ `deque`๊ฐ€ ์šฐ์›”ํ•˜๋‹ค.
98+
> If you need to add/remove at both ends, consider using a collections.deque instead.
99+
100+
*(ํ•˜์ง€๋งŒ index๋กœ ์ ‘๊ทผํ•˜๋ ค๋ฉด `list`๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•จ)*
101+
102+
#### visited ์ฒ˜๋ฆฌ
103+
- ์ตœ๋‹จ๊ฑฐ๋ฆฌ์ด๋ฏ€๋กœ ์™”๋˜ ์ขŒํ‘œ์— ๋‹ค์‹œ ์˜ฌ ์ผ์ด ์—†๋‹ค.
104+
- `board`๋Š” ๋ฐฉ๋ฌธ ์ฒดํฌ์˜ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
105+
106+
<br />
107+
108+
### ๋ฉ”๋ชจ
109+
110+
- ์ค‘๊ฐ„์— return์œผ๋กœ ๋Š์–ด์ฃผ๋ฉด 1856 ms, ์•ˆํ•˜๋ฉด 3384 ms

0 commit comments

Comments
ย (0)