Post

๐Ÿข ๋กœ๋ด‡์ฒญ์†Œ๊ธฐ

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

14503๋ฒˆ: ๋กœ๋ด‡ ์ฒญ์†Œ๊ธฐ


2. ์ฝ”๋“œ

Python3 30840KB 72ms

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
"""
[1091] ์นด๋“œ ์„ž๊ธฐ

๐Ÿ’› ๋ฌธ์ œ
๋กœ๋ด‡ ์ฒญ์†Œ๊ธฐ์™€ ๋ฐฉ์˜ ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ฒญ์†Œํ•˜๋Š” ์˜์—ญ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

๋กœ๋ด‡ ์ฒญ์†Œ๊ธฐ๊ฐ€ ์žˆ๋Š” ๋ฐฉ์€ N X N ํฌ๊ธฐ์˜ ์ง์‚ฌ๊ฐํ˜•์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ,
1 X 1 ํฌ๊ธฐ์˜ ์ •์‚ฌ๊ฐํ˜• ์นธ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๋‹ค. ๊ฐ๊ฐ์˜ ์นธ์€ ๋ฒฝ ๋˜๋Š” ๋นˆ ์นธ์ด๋‹ค.

์ฒญ์†Œ๊ธฐ๋Š” ๋ฐ”๋ผ๋ณด๋Š” ๋ฐฉํ–ฅ์ด ์žˆ์œผ๋ฉฐ, ์ด ๋ฐฉํ–ฅ์€ ๋™, ์„œ, ๋‚จ, ๋ถ ์ค‘ ํ•˜๋‚˜์ด๋‹ค.
๋ฐฉ์˜ ๊ฐ ์นธ์€ ์ขŒํ‘œ (r, c)๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๊ณ , ๊ฐ€์žฅ ๋ถ์ชฝ ์ค„์˜ ๊ฐ€์žฅ ์„œ์ชฝ ์นธ์˜ ์ขŒํ‘œ๊ฐ€ (0, 0), ๊ฐ€์žฅ ๋‚จ์ชฝ ์ค„์˜ ๊ฐ€์žฅ ๋™์ชฝ ์นธ์˜ ์ขŒํ‘œ๊ฐ€ (N-1, M-1)
์ขŒํ‘œ (r, c)๋Š” ๋ถ์ชฝ์—์„œ (r+1)๋ฒˆ์งธ์— ์žˆ๋Š” ์ค„์˜ ์„œ์ชฝ์—์„œ (c+1)๋ฒˆ์งธ ์นธ์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค. ์ฒ˜์Œ์— ๋นˆ ์นธ์€ ์ „๋ถ€ ์ฒญ์†Œ๋˜์ง€ ์•Š์€ ์ƒํƒœ์ด๋‹ค.

๋กœ๋ด‡ ์ฒญ์†Œ๊ธฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘๋™ํ•œ๋‹ค.
1 ํ˜„์žฌ ์นธ์ด ์•„์ง ์ฒญ์†Œ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, ํ˜„์žฌ ์นธ์„ ์ฒญ์†Œํ•œ๋‹ค.
2. ํ˜„์žฌ ์นธ์˜ ์ฃผ๋ณ€ 4์นธ ์ค‘ ์ฒญ์†Œ๋˜์ง€ ์•Š์€ ๋นˆ ์นธ์ด ์—†๋Š” ๊ฒฝ์šฐ,
- ๋ฐ”๋ผ๋ณด๋Š” ๋ฐฉํ–ฅ์„ ์œ ์ง€ํ•œ ์ฑ„๋กœ ํ•œ ์นธ ํ›„์ง„ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ํ•œ ์นธ ํ›„์ง„ํ•˜๊ณ  1๋ฒˆ์œผ๋กœ ๋Œ์•„๊ฐ„๋‹ค.
- ๋ฐ”๋ผ๋ณด๋Š” ๋ฐฉํ–ฅ์˜ ๋’ค์ชฝ ์นธ์ด ๋ฒฝ์ด๋ผ ํ›„์ง„ํ•  ์ˆ˜ ์—†๋‹ค๋ฉด ์ž‘๋™์„ ๋ฉˆ์ถ˜๋‹ค.
3. ํ˜„์žฌ ์นธ์˜ ์ฃผ๋ณ€ 4์นธ ์ค‘ ์ฒญ์†Œ๋˜์ง€ ์•Š์€ ๋นˆ ์นธ์ด ์žˆ๋Š” ๊ฒฝ์šฐ,
- ๋ฐ˜์‹œ๊ณ„ ๋ฐฉํ–ฅ์œผ๋กœ 90๋„ ํšŒ์ „ํ•œ๋‹ค.
- ๋ฐ”๋ผ๋ณด๋Š” ๋ฐฉํ–ฅ์„ ๊ธฐ์ค€์œผ๋กœ ์•ž์ชฝ ์นธ์ด ์ฒญ์†Œ๋˜์ง€ ์•Š์€ ๋นˆ ์นธ์ธ ๊ฒฝ์šฐ ํ•œ ์นธ ์ „์ง„ํ•œ๋‹ค.
- 1๋ฒˆ์œผ๋กœ ๋Œ์•„๊ฐ„๋‹ค.

๐Ÿ’š ์ž…๋ ฅ
์ฒซ์งธ ์ค„์— ๋ฐฉ์˜ ํฌ๊ธฐ N๊ณผ M์ด ์ž…๋ ฅ๋œ๋‹ค. (3 <= N, M <= 50)
๋‘˜์งธ ์ค„์— ์ฒ˜์Œ์— ๋กœ๋ด‡ ์ฒญ์†Œ๊ธฐ๊ฐ€ ์žˆ๋Š” ์นธ์˜ ์ขŒํ‘œ (r, c)์™€ ์ฒ˜์Œ์— ๋กœ๋ด‡ ์ฒญ์†Œ๊ธฐ๊ฐ€ ๋ฐ”๋ผ๋ณด๋Š” ๋ฐฉํ–ฅ d๊ฐ€ ์ž…๋ ฅ๋œ๋‹ค.
d๊ฐ€ 0์ธ ๊ฒฝ์šฐ ๋ถ์ชฝ, 1์ธ ๊ฒฝ์šฐ ๋™์ชฝ, 2์ธ ๊ฒฝ์šฐ ๋‚จ์ชฝ, 3์ธ ๊ฒฝ์šฐ ์„œ์ชฝ์„ ๋ฐ”๋ผ๋ณด๊ณ  ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

์…‹์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฐ ์žฅ์†Œ์˜ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” N X M๊ฐœ์˜ ๊ฐ’์ด ํ•œ ์ค„์— M๊ฐœ์”ฉ ์ž…๋ ฅ๋œ๋‹ค.
i๋ฒˆ์งธ ์ค„์˜ j๋ฒˆ์งธ ๊ฐ’์€ ์นธ (i, j)์˜ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ์ด ๊ฐ’์ด 0์ธ ๊ฒฝ์šฐ (i, j)๊ฐ€ ์ฒญ์†Œ๋˜์ง€ ์•Š์€ ๋นˆ ์นธ์ด๊ณ ,
1์ธ ๊ฒฝ์šฐ (i, j)์— ๋ฒฝ์ด ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. ๋ฐฉ์˜ ๊ฐ€์žฅ ๋ถ์ชฝ, ๊ฐ€์žฅ ๋‚จ์ชฝ, ๊ฐ€์žฅ ์„œ์ชฝ, ๊ฐ€์žฅ ๋™์ชฝ ์ค„ ์ค‘ ํ•˜๋‚˜ ์ด์ƒ์— ์œ„์น˜ํ•œ ๋ชจ๋“  ์นธ์—๋Š” ๋ฒฝ์ด ์žˆ๋‹ค.
๋กœ๋ด‡ ์ฒญ์†Œ๊ธฐ๊ฐ€ ์žˆ๋Š” ์นธ์€ ํ•ญ์ƒ ๋นˆ ์นธ์ด๋‹ค.

๐Ÿ’™ ์ถœ๋ ฅ
๋กœ๋ด‡ ์ฒญ์†Œ๊ธฐ๊ฐ€ ์ž‘๋™์„ ์‹œ์ž‘ํ•œ ํ›„ ์ž‘๋™์„ ๋ฉˆ์ถœ ๋•Œ๊นŒ์ง€ ์ฒญ์†Œํ•˜๋Š” ์นธ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
"""

# ๋ฐฉ์˜ ํฌ๊ธฐ N๊ณผ M
N, M = map(int, input().split())

# ์ขŒํ‘œ์™€ ๋ฐฉํ–ฅ (0 ๋ถ, 1 ๋™, 2 ๋‚จ, 3 ์„œ)
R, C, D = map(int, input().split())

graph = []
for i in range(N):
    graph.append(list(map(int, input().split())))

dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]


def turn():
    global D
    # ์ขŒํ‘œ์™€ ๋ฐฉํ–ฅ (0 ๋ถ, 1 ๋™, 2 ๋‚จ, 3 ์„œ)
    D = (D - 1) % 4

count = 1
# ์ฒญ์†Œ๊ธฐ ์œ„์น˜
graph[R][C] = 2

while True:
    check = False

    for i in range(4):
        turn()
        nx = R + dx[D]
        ny = C + dy[D]

        if 0 <= nx < N and 0 <= ny < M:
            if graph[nx][ny] == 0:
                count += 1
                graph[nx][ny] = 2
                R, C = nx, ny
                # ๋ฐฉ๋ฌธ์œผ๋กœ check
                check = True
                break

    # ํ›„์ง„ํ•˜๊ธฐ
    if not check:
        nx = R - dx[D]
        ny = C - dy[D]

        if 0 <= nx < N and 0 <= ny < M:
            if graph[nx][ny] == 2:         # ์ด๋ฏธ ๋ฐฉ๋ฌธ
                R, C = nx, ny
            elif graph[nx][ny] == 1:       # ๋ฒฝ
                print(count)
                break
        else:
            print(count)
            break


3. ํ•ด์„ค

global D ๋ฅผ ํ†ตํ•ด ๋ฐฉํ–ฅ์„ ์ œ์‹œํ•จ

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