Post

๐Ÿข ์žฅ๊ตฐ

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

16509๋ฒˆ: ์žฅ๊ตฐ


2. ์ฝ”๋“œ

Python3 34136KB 68ms

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""
[16509] ์žฅ๊ตฐ

๐Ÿ’› ๋ฌธ์ œ
์˜ค๋žœ๋งŒ์— ํœด๊ฐ€๋ฅผ ๋‚˜์˜จ ํ˜ธ๊ทผ์ด๋Š” ๋ฌธ๋“ ๋™์•„๋ฆฌ๋ฐฉ์— ์žˆ๋Š” ์žฅ๊ธฐ๊ฐ€ ํ•˜๊ณ  ์‹ถ์–ด์กŒ๋‹ค. 
ํ•˜์ง€๋งŒ ์žฅ๊ธฐ๋ฅผ ์˜ค๋žซ๋™์•ˆ ํ•˜์ง€ ์•Š์€ ํƒ“์ธ์ง€ ์˜ˆ์ „์—๋Š” ์ž˜ ์“ฐ๋˜ ์ƒ์„ ์ œ๋Œ€๋กœ ์“ฐ๋Š” ๊ฒƒ์ด ๋„ˆ๋ฌด ํž˜๋“ค์—ˆ๋‹ค. 
ํ˜ธ๊ทผ์ด๋ฅผ ์œ„ํ•ด ์ƒ์„ ์–ด๋–ป๊ฒŒ ์จ์•ผ ํ• ์ง€ ๋„์™€์ฃผ์ž.

์œ„ ๊ทธ๋ฆผ์€ 10ร—9 ํฌ๊ธฐ์˜ ์žฅ๊ธฐํŒ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, 
์ƒ์€ (5, 4)์—, ์™•์€ (1, 4)์— ์ž๋ฆฌ ์žก๊ณ  ์žˆ๋Š” ๊ธฐ๋ฌผ์ด๋‹ค. 
(0, 3)๊ณผ (2, 5)๋ฅผ ๊ผญ์ง“์ ์œผ๋กœ ํ•˜๋Š” ์‚ฌ๊ฐํ˜•๊ณผ, (7, 3)๊ณผ (9, 5)๋ฅผ ๊ผญ์ง“์ ์œผ๋กœ ํ•˜๋Š” ์‚ฌ๊ฐํ˜•์€ ์™•์ด ์œ„์น˜ํ•  ์ˆ˜ ์žˆ๋Š” ๊ถ์„ฑ์ด๋ผ๊ณ  ํ•œ๋‹ค.
์ƒ์€ ์œ„ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด 8๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ์›€์ง์ผ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ƒ, ํ•˜, ์ขŒ, ์šฐ๋กœ ํ•œ ์นธ์„ ์ด๋™ํ•œ ํ›„์— ๊ฐ™์€ ๋ฐฉํ–ฅ ์ชฝ ๋Œ€๊ฐ์„ ์œผ๋กœ ๋‘ ์นธ ์ด๋™ํ•œ๋‹ค.

๋งŒ์•ฝ ์ƒ์ด ์ด๋™ํ•˜๋Š” ๊ฒฝ๋กœ์— ์œ„ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋‹ค๋ฅธ ๊ธฐ๋ฌผ์ด ์žˆ๋‹ค๋ฉด ์ƒ์€ ๊ทธ์ชฝ์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์—†๋‹ค. 
๋˜ํ•œ, ์ƒ์ด ์žฅ๊ธฐํŒ์„ ๋ฒ—์–ด๋‚  ์ˆ˜๋„ ์—†๋‹ค.

10ร—9 ํฌ๊ธฐ์˜ ์žฅ๊ธฐํŒ ์œ„์— ์ƒ๊ณผ ์™•์˜ ์ฒ˜์Œ ์œ„์น˜๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, 
์ƒ์ด ์™•์—๊ฒŒ ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ์†Œ ์ด๋™ ํšŸ์ˆ˜๋ฅผ ๊ตฌํ•˜์—ฌ๋ผ.

๐Ÿ’š ์ž…๋ ฅ
์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ์ƒ์˜ ์œ„์น˜๋ฅผ ์˜๋ฏธํ•˜๋Š” ์ •์ˆ˜ R1, C1์ด ์ฃผ์–ด์ง„๋‹ค.
๋‘ ๋ฒˆ์งธ ์ค„์—๋Š” ์™•์˜ ์œ„์น˜๋ฅผ ์˜๋ฏธํ•˜๋Š” ์ •์ˆ˜ R2, C2๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. 

์žฅ๊ธฐํŒ์—์„œ Ri (0 โ‰ค Ri โ‰ค 9)๋Š” ํ–‰์„, Ci (0 โ‰ค Ci โ‰ค 8)๋Š” ์—ด์„ ์˜๋ฏธํ•œ๋‹ค.
์™•์€ ํ•ญ์ƒ ๊ถ์„ฑ์— ์ž๋ฆฌ ์žก๊ณ  ์žˆ์œผ๋ฉฐ, ์ƒ๊ณผ ์™•์˜ ์œ„์น˜๋Š” ๊ฒน์น˜์ง€ ์•Š๋Š”๋‹ค.

๐Ÿ’™ ์ถœ๋ ฅ
์ƒ์ด ์™•์—๊ฒŒ ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ์†Œ ์ด๋™ ํšŸ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ ๋„๋‹ฌํ•  ์ˆ˜ ์—†๋‹ค๋ฉด -1์„ ์ถœ๋ ฅํ•œ๋‹ค.
"""

from collections import deque

# ์ƒ์˜ ์œ„์น˜
r1, c1 = map(int, input().split())

# ์™•์˜ ์œ„์น˜
r2, c2 = map(int, input().split())

step_x = [
        #### [์ƒํ•˜์ขŒ์šฐ ์ค‘ ํ•œ์นธ ์ด๋™, ๋Œ€๊ฐ์„ ์œผ๋กœ ํ•œ์นธ ์ด๋™]
        # ์œ„
		[-1, -2],
        [-1, -2],
        # ์™ผ์ชฝ
        [0, -1],
        [0, 1],
        # ์•„๋ž˜์ชฝ
        [1, 2],
        [1, 2],
        # ์˜ค๋ฅธ์ชฝ
        [0, -1],
        [0, 1]
	]
step_y = [
        # ์œ„
		[0, -1],
        [0, 1],
        # ์™ผ์ชฝ
        [-1, -2],
        [-1, -2],
        # ์•„๋ž˜์ชฝ
        [0, -1],
        [0, 1],
        # ์˜ค๋ฅธ์ชฝ
        [1, 2],
        [1, 2]
	]

visit = [[0] * 9 for _ in range(10)]
# ์œ„, ์™ผ์ชฝ, ์•„๋ž˜์ชฝ, ์˜ค๋ฅธ์ชฝ
dx = [-3, -3, -2, 2, 3, 3, -2, 2]
dy = [-2, 2, -3, -3, -2, 2, 3, 3]

def in_range(nx, ny) :
    return 0 <= nx < 10 and 0 <= ny < 9

# ์ด๋™ ๊ฒฝ๋กœ ์‚ดํŽด๋ณด๊ธฐ
def move(x, y, i) :
    for j in range(2) :
        # [์ƒํ•˜์ขŒ์šฐ ์ค‘ ํ•œ์นธ ์ด๋™, ๋Œ€๊ฐ์„ ์œผ๋กœ ํ•œ์นธ ์ด๋™]
        nx = x + step_x[i][j]
        ny = y + step_y[i][j]
        
        # ์ด๋™ ๊ฒฝ๋กœ์— ์™•์ด ์žˆ์œผ๋ฉด
        if (nx == r2 and ny == c2) :        
            return 0
    return 1


visited = [[0] * 9 for _ in range(10)]

# ์ƒ์˜ ์ƒํ•˜์ขŒ์šฐ ํ•œ์นธ + ๋Œ€๊ฐ์„  ๋‘์นธ ํ›„ ์œ„์น˜
def bfs(x, y) :
    queue = deque()
    queue.append([x, y, 0])
    visited[x][y] = 1

    while queue :
        x, y, cnt = queue.popleft()

        for i in range(8) :
            nx = x + dx[i]
            ny = y + dy[i]

            # ์ƒํ•˜์ขŒ์šฐ ์ค‘ ํ•œ์นธ + ๋Œ€๊ฐ์„  ๋‘์นธ ํ–ˆ์„ ๋•Œ ๋ฒ”์œ„ ๋„˜์–ด๊ฐ€๋ฉด! for ๋ฌธ์œผ๋กœ ๋Œ์•„๊ฐ„๋‹ค
            if not in_range(nx, ny) :
                continue
        
            # ๊ทธ ์ „์— ์ด๋™๊ฒฝ๋กœ ์‚ดํŽด๋ณด๊ธฐ.. ์™• ์žˆ๋‹ค๋ฉด! for ๋ฌธ์œผ๋กœ ๋Œ์•„๊ฐ„๋‹ค
            # ๊ธฐ์กด ์ƒ์˜ ์œ„์น˜ + ์ด๋™ํ•œ ๋ฐฉํ–ฅ ์ธ๋ฑ์Šค
            if not move(x, y, i) :
                continue

            # ์ด๋ฏธ ๋ฐฉ๋ฌธ ํ–ˆ๋‹ค๋ฉด
            if visited[nx][ny] == 1 :
                continue
            
            # ์™• ๋งŒ๋‚œ๋‹ค๋ฉด
            if nx == r2 and ny == c2 :
                # ์ƒ์ด ์™•์—๊ฒŒ ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ์†Œ ์ด๋™ ํšŸ์ˆ˜๋ฅผ ์ถœ๋ ฅ
                return cnt + 1

            visited[nx][ny] = 1
            # cnt += 1 ํ•˜๊ณ  ๋ฐ‘์— cnt๋ฅผ ๋Œ€์ž…ํ–ˆ๋Š”๋ฐ ์ˆซ์ž๊ฐ€ ๋” ์ปค์ ธ์„œ ๋‚˜์˜ด ๋ญ˜๊นŒ?
            queue.append([nx, ny, cnt + 1])

    # ๋งŒ์•ฝ ๋„๋‹ฌํ•  ์ˆ˜ ์—†๋‹ค๋ฉด -1์„ ์ถœ๋ ฅ
    return -1

# ์ƒ์˜ ์œ„์น˜
print(bfs(r1, c1))


3. ํ•ด์„ค

8๋ฐฉํ–ฅ๋งˆ๋‹ค, ํ•œ์นธ ์ด๋™ + ๋Œ€๊ฐ์„  ๋‘๋ฒˆ ์ด๋™ ํ•˜๋Š” ๋ฐฉํ–ฅ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ๋‹ค.

  • ๋จผ์ € ๋ฐฉํ–ฅ์„ ์ง€์ •ํ•ด์„œ ํ•œ์นธ์ด๋™ + ๋Œ€๊ฐ์„  ๋‘๋ฒˆ ํ–ˆ์„ ๋•Œ ๋ฒ”์œ„ ๋„˜์–ด๊ฐ€๋ฉด pass
  • ๊ทธ์ „์— ์ด๋™ ๊ณผ์ •์—์„œ 2๊ฐ€์ง€ ๊ฒฝ์šฐ์— ์™•์ด ์žˆ๋‹ค๋ฉด pass
  • ์ด๋ฏธ ๋ฐฉ๋ฌธ ํ–ˆ๋‹ค๋ฉด pass
  • ๋งˆ์ง€๋ง‰์œผ๋กœ ์™• ๋งŒ๋‚ฌ๋‹ค๋ฉด ์ตœ์†Œ ์ด๋™ ํšŸ์ˆ˜๋ฅผ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค
  • ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ ํ•˜๊ณ  ํ˜„์žฌ ์œ„์น˜๋ฅผ queue ์— append ํ•˜๊ณ  ๋นŒ ๋•Œ๊นŒ์ง€ while ๋ฌธ ๋Œ๋ฆฐ๋‹ค
This post is licensed under CC BY 4.0 by the author.