|
| 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