Post

๐Ÿน ๋ฏธ๋กœ ํƒˆ์ถœ ๋ช…๋ ์–ด

1. ๋ฌธ์ œ ๋งํฌ

๋ฏธ๋กœ ํƒˆ์ถœ ๋ช…๋ ์–ด


2. ์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
dx = [1, 0, 0, -1]
dy = [0, -1, 1, 0]
move = ['d', 'l', 'r', 'u']

from collections import deque
import sys

def solution(n, m, x, y, r, c, k):
    sys.setrecursionlimit(5000)
    q = deque([])
    q.append((x, y, 0, ''))
    # ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ๊ธธ์ด
    min_dist = abs(x-r) + abs(y-c)
    
    # ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ณด๋‹ค ์ด๋™ ๊ฐ€๋Šฅ ๊ฑฐ๋ฆฌ๊ฐ€ ์ž‘๊ฑฐ๋‚˜ ๋„๋‹ฌ์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ์œ„์น˜์ผ ๋•Œ 
    if min_dist > k or (k - min_dist) % 2 == 1:
        return 'impossible'
    
    def find_path(a, b, count, path):
        # k๋ฒˆ์œผ๋กœ ๋„๋‹ฌํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ 
        if a == r and b == c and (k-count) % 2 == 1:
            return 'impossible'
        # k๋ฒˆ์งธ ๋„๋‹ฌ
        if a == r and b == c and count == k:
            return path
        for i in range(4):
            na = a + dx[i]
            nb = b + dy[i]
            if 0 < na <= n and 0 < nb <= m:
                # ์ด๋™ ์œ„์น˜๊ฐ€ ์ตœ์ข… ์œ„์น˜์— ๋„๋‹ฌํ•  ์ˆ˜ ์—†์„๋งŒํผ ๋ฉ€์–ด์งˆ ๋•Œ 
                if abs(na-r) + abs(nb-c) > k - (count+1):
                    continue
                result = find_path(na, nb, count+1, path + move[i])
                return result
    
    return find_path(x, y, 0, '')
  • ์ •ํ™•์„ฑ
ํ…Œ์ŠคํŠธ 1 ใ€‰ํ†ต๊ณผ (0.21ms, 10.4MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰ํ†ต๊ณผ (0.15ms, 10.4MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰ํ†ต๊ณผ (0.04ms, 10.4MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰ํ†ต๊ณผ (10.15ms, 16MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰ํ†ต๊ณผ (7.63ms, 16.1MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰ํ†ต๊ณผ (9.03ms, 16.1MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰ํ†ต๊ณผ (6.72ms, 16MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰ํ†ต๊ณผ (7.26ms, 16.1MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰ํ†ต๊ณผ (8.84ms, 16MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰ํ†ต๊ณผ (6.66ms, 16.2MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰ํ†ต๊ณผ (6.79ms, 15.7MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰ํ†ต๊ณผ (6.54ms, 15.9MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰ํ†ต๊ณผ (8.29ms, 15.9MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰ํ†ต๊ณผ (6.31ms, 16.2MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰ํ†ต๊ณผ (9.90ms, 15.8MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰ํ†ต๊ณผ (8.67ms, 15.8MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰ํ†ต๊ณผ (8.65ms, 15.8MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰ํ†ต๊ณผ (13.91ms, 16.1MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰ํ†ต๊ณผ (9.83ms, 16.1MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰ํ†ต๊ณผ (6.76ms, 15.8MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰ํ†ต๊ณผ (9.31ms, 15.8MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰ํ†ต๊ณผ (9.86ms, 16MB)
ํ…Œ์ŠคํŠธ 28 ใ€‰ํ†ต๊ณผ (9.50ms, 15.8MB)
ํ…Œ์ŠคํŠธ 29 ใ€‰ํ†ต๊ณผ (9.52ms, 16MB)
ํ…Œ์ŠคํŠธ 30 ใ€‰ํ†ต๊ณผ (9.42ms, 16MB)
ํ…Œ์ŠคํŠธ 31 ใ€‰ํ†ต๊ณผ (0.01ms, 10.3MB)


3. ํ•ด์„ค

์ด ๋ฌธ์ œ๋Š” ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๋ฅผ ์ž˜ ๊ฑธ๋Ÿฌ๋‚ด์ฃผ์–ด ์‹œ๊ฐ„์„ ์ค„์ด๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค.

๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. (x, y) โ†’ (r, c)์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ณด๋‹ค k(์ด๋™ ๊ฐ€๋Šฅ ๊ฑฐ๋ฆฌ)๊ฐ€ ์ž‘์„ ๋•Œ
  2. k(์ด๋™ ๊ฐ€๋Šฅ ๊ฑฐ๋ฆฌ)์—์„œ (x, y) โ†’ (r, c)์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ ๊ธธ์ด๋ฅผ ๋บ€ ๊ธธ์ด๊ฐ€ 2์˜ ๋ฐฐ์ˆ˜๊ฐ€ ์•„๋‹ ๋•Œ

    โ†’ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์ด๋™ ํ›„ ์ขŒ,์šฐ ํ˜น์€ ์œ„,์•„๋ž˜๋กœ ์›€์ง์—ฌ ๊ฐ™์€ ์ž๋ฆฌ๋กœ ๋Œ์•„์˜ฌ ์ˆ˜ ์žˆ์–ด์•ผํ•œ๋‹ค.

  3. ์ด๋™์ค‘์˜ ์œ„์น˜์—์„œ ์ตœ์ข… ์œ„์น˜๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๋‚จ์€ ์›€์ง์ž„์œผ๋กœ ๊ฐˆ ์ˆ˜ ์—†์„ ๋•Œ

์ด๋•Œ ์‚ฌ์ „์ˆœ์œผ๋กœ ๊ฒฐ๊ณผ๋ฅผ ์ฐพ์•„์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— d(์•„๋ž˜), l(์™ผ์ชฝ), r(์˜ค๋ฅธ์ชฝ), u(์œ„์ชฝ) ์ˆœ์œผ๋กœ ์›€์ง์ด๋„๋ก ๋‚˜์—ดํ•˜๊ณ  ๊ฐ€์žฅ ์ฒซ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

์ฒ˜์Œ์—๋Š” bfs๋กœ ํ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ์ง€๋งŒ, bfs๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ตœ์ข… ๋ชฉ์ ์ง€๋กœ ๋ฐ”๋กœ๊ฐ€์ง€์•Š๊ณ  ์ฃผ๋ณ€์„ ๋ชจ๋‘ ํ™•์ธํ•˜๊ณ  ์ตœ์ข… ๋ชฉ์ ์ง€๋กœ ๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค.

dfs๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ์ข… ๋ชฉ์ ์ง€๊นŒ์ง€ ๋ฐ”๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋„๋ก ์ฝ”๋“œ๋ฅผ ์žฌ๊ตฌํ˜„ํ–ˆ๋‹ค. ์ด๋•Œ setrecursionlimit์„ ํ•˜์ง€ ์•Š์œผ๋ฉด ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋œฐ ์ˆ˜ ์žˆ๋‹ค.

This post is licensed under CC BY 4.0 by the author.