Post

๐Ÿข ์‚ฌ๋‹ค๋ฆฌ ํƒ€๊ธฐ

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

2469๋ฒˆ: ์‚ฌ๋‹ค๋ฆฌ ํƒ€๊ธฐ


2. ์ฝ”๋“œ

Python3 32276KB 84ms

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
133
134
135
136
137
138
139
140
141
142
"""
[2469] ์‚ฌ๋‹ค๋ฆฌ ํƒ€๊ธฐ

๐Ÿ’› ๋ฌธ์ œ
k๋ช…์˜ ์ฐธ๊ฐ€์ž๋“ค์ด ์‚ฌ๋‹ค๋ฆฌ ํƒ€๊ธฐ๋ฅผ ํ†ตํ•˜์—ฌ ์–ด๋–ค ์ˆœ์„œ๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค.
์ฐธ๊ฐ€์ž๋“ค์€ ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž ์ฒซ k๊ฐœ๋กœ ํ‘œํ˜„๋˜๋ฉฐ,
์‚ฌ๋‹ค๋ฆฌ ํƒ€๊ธฐ๋ฅผ ์‹œ์ž‘ํ•  ๋•Œ์˜ ์ˆœ์„œ๋Š” ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ํ•ญ์ƒ ์•ŒํŒŒ๋ฒณ ์ˆœ์„œ๋Œ€๋กœ์ด๋‹ค.

k=10 ์ธ ์˜ˆ๋ฅผ ๋“ค์–ด ๋ณด์ž.
10๋ช…์˜ A, B, C, D, E, F, G, H, I, J ์ฐธ๊ฐ€์ž๋“ค์ด ์‚ฌ๋‹ค๋ฆฌ ํƒ€๊ธฐ๋ฅผ ์ค€๋น„ํ•œ๋‹ค.

์•„๋ž˜ ๊ทธ๋ฆผ์€ 10๊ฐœ์˜ ์„ธ๋กœ ์ค„๊ณผ 5๊ฐœ์˜ ๊ฐ€๋กœ ์ค„์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์‚ฌ๋‹ค๋ฆฌ์˜ ํ•œ ์˜ˆ๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ๋‹ค.

์ด ์‚ฌ๋‹ค๋ฆฌ์—์„œ ์ ์„ ์€ ๊ฐ€๋กœ ๋ง‰๋Œ€๊ฐ€ ์—†์Œ์„, ๊ตต์€ ๊ฐ€๋กœ ์‹ค์„ ์€ ์˜†์œผ๋กœ ๊ฑด๋„ˆ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฐ€๋กœ ๋ง‰๋Œ€๊ฐ€ ์žˆ์Œ
์ œ์‹œ๋œ ์‚ฌ๋‹ค๋ฆฌ๋ฅผ ํƒ€๋ฉด ๊ทธ ์ตœ์ข… ๋„๋‹ฌ๋œ ์ˆœ์„œ๋Š” ์™ผ์ชฝ์œผ๋กœ๋ถ€ํ„ฐ A, C, G, B, E, D, J, F, I, H ๊ฐ€ ๋œ๋‹ค.

์‚ฌ๋‹ค๋ฆฌ ํƒ€๊ธฐ๋Š” ์„ธ๋กœ ๋ง‰๋Œ€๋ฅผ ํƒ€๊ณ  ๋‚ด๋ ค์˜ค๋Š” ์ค‘์— ๊ฐ€๋กœ๋ง‰๋Œ€๋ฅผ ๋งŒ๋‚˜๋ฉด ๊ทธ ์ชฝ์œผ๋กœ ์˜ฎ๊ฒจ ๊ฐ€๋ฉด์„œ ๋๊นŒ์ง€ ๋‚ด๋ ค๊ฐ€๋Š” ๊ณผ์ •์ด๋‹ค.
์‚ฌ๋‹ค๋ฆฌ ํƒ€๊ธฐ์˜ ๊ทœ์น™ ํŠน์„ฑ์ƒ ๋‘ ๊ฐ€๋กœ ๋ง‰๋Œ€๊ฐ€ ์ง์ ‘ ์—ฐ๊ฒฐ๋  ์ˆ˜๋Š” ์—†์œผ๋ฏ€๋กœ ์ด ์ƒํ™ฉ์€ ์ด ๋ฌธ์ œ์—์„œ ๊ณ ๋ คํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

์šฐ๋ฆฌ๋Š” ํ•˜๋‚˜์˜ ๊ฐ€๋กœ ์ค„์ด ๊ฐ์ถ”์–ด์ง„ ์‚ฌ๋‹ค๋ฆฌ๋ฅผ ๋ฐ›์•„์„œ
๊ทธ ์ค„์˜ ๊ฐ ์นธ์— ๊ฐ€๋กœ ๋ง‰๋Œ€๋ฅผ ์ ์ ˆํžˆ ๋„ฃ์–ด์„œ ์ฐธ๊ฐ€์ž๋“ค์˜ ์ตœ์ข… ์ˆœ์„œ๊ฐ€ ์›ํ•˜๋Š” ์ˆœ์„œ๋Œ€๋กœ ๋‚˜์˜ค๋„๋ก ๋งŒ๋“ค๋ ค๊ณ  ํ•œ๋‹ค.

์ž…๋ ฅ์—์„œ ์‚ฌ๋‹ค๋ฆฌ์˜ ์ „์ฒด ๋ชจ์–‘์€ ๊ฐ ์ค„์— ์žˆ๋Š” ๊ฐ€๋กœ ๋ง‰๋Œ€์˜ ์œ ๋ฌด๋กœ ํ‘œํ˜„๋œ๋‹ค.
๊ฐ ์ค„์—์„œ ๊ฐ€๋กœ ๋ง‰๋Œ€๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” โ€˜*โ€™(๋ณ„)๋ฌธ์ž, ์žˆ์„ ๊ฒฝ์šฐ์—๋Š” โ€˜-โ€™(๋นผ๊ธฐ) ๋ฌธ์ž๋กœ ํ‘œ์‹œ๋œ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ๊ฐ์ถ”์–ด์ง„ ํŠน์ • ๊ฐ€๋กœ ์ค„์€ ๊ธธ์ด k-1์ธ โ€˜?โ€™ (๋ฌผ์Œํ‘œ) ๋ฌธ์ž์—ด๋กœ ํ‘œ์‹œ๋˜์–ด ์žˆ๋‹ค.

๐Ÿ’š ์ž…๋ ฅ
์ฒซ ์ค„์—๋Š” ์ฐธ๊ฐ€ํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜ k๊ฐ€ ๋‚˜์˜จ๋‹ค(3 โ‰ค k โ‰ค 26).
๊ทธ ๋‹ค์Œ ์ค„์—๋Š” ๊ฐ€๋กœ ๋ง‰๋Œ€๊ฐ€ ๋†“์ผ ์ „์ฒด ๊ฐ€๋กœ ์ค„์˜ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” n์ด ๋‚˜์˜จ๋‹ค(3 โ‰ค n โ‰ค 1,000).
์„ธ ๋ฒˆ์งธ ์ค„์—๋Š” ์‚ฌ๋‹ค๋ฆฌ๋ฅผ ํƒ€๊ณ  ๋‚œ ํ›„ ๊ฒฐ์ •๋œ ์ฐธ๊ฐ€์ž๋“ค์˜ ์ตœ์ข… ์ˆœ์„œ๊ฐ€ ๊ธธ์ด k์ธ ๋Œ€๋ฌธ์ž ๋ฌธ์ž์—ด๋กœ ๋“ค์–ด์˜จ๋‹ค.
์ด์–ด์ง€๋Š” n๊ฐœ์˜ ์ค„์—๋Š” ์•ž์„œ ์„ค๋ช…ํ•œ ๋ฐ”์™€ ๊ฐ™์ด โ€˜*โ€™์™€ โ€˜-โ€™ ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๊ธธ์ด k-1์ธ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค.
๊ทธ ์ค‘ ๊ฐ์ถ”์–ด์ง„ ๊ฐ€๋กœ ์ค„์€ ๊ธธ์ด๊ฐ€ k-1์ธ โ€˜?โ€™ ๋ฌธ์ž์—ด๋กœ ํ‘œ์‹œ๋˜์–ด ์žˆ๋‹ค.

๐Ÿ’™ ์ถœ๋ ฅ
์ž…๋ ฅ ํŒŒ์ผ ์„ธ ๋ฒˆ์งธ ์ค„์—์„œ ์ง€์ •ํ•œ ๋„์ฐฉ์ˆœ์„œ๊ฐ€ ํ•ด๋‹น ์‚ฌ๋‹ค๋ฆฌ์—์„œ ๋งŒ๋“ค์–ด์งˆ ์ˆ˜ ์žˆ๋„๋ก ๊ฐ์ถ”์–ด์ง„ ๊ฐ€๋กœ ์ค„์„ ๊ตฌ์„ฑํ•ด์•ผ ํ•œ๋‹ค.
๊ฐ์ถ”์–ด์ง„ ๊ฐ€๋กœ ์ค„์˜ ์ƒํƒœ๋ฅผ ์žฌ๊ตฌ์„ฑํ•˜์—ฌ ์ด๋ฅผ โ€˜*โ€™(๋ณ„) ๋ฌธ์ž์™€ โ€˜-โ€™(๋นผ๊ธฐ) ๋ฌธ์ž๋กœ ๊ตฌ์„ฑ๋œ ๊ธธ์ด k-1์ธ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค์–ด ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์–ด๋–ค ๊ฒฝ์šฐ์—๋Š” ๊ทธ ๊ฐ์ถ”์–ด์ง„ ๊ฐ€๋กœ ์ค„์„ ์–ด๋–ป๊ฒŒ ๊ตฌ์„ฑํ•ด๋„ ์›ํ•˜๋Š” ์ˆœ์„œ๋ฅผ ์–ป์„ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค.
์ด ๊ฒฝ์šฐ์—๋Š”  โ€˜xโ€™(์†Œ๋ฌธ์ž ์—‘์Šค)๋กœ ๊ตฌ์„ฑ๋œ ๊ธธ์ด k-1์ธ ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.
"""

# ์ฐธ๊ฐ€ํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜
k = int(input())
# ๊ฐ€๋กœ ๋ง‰๋Œ€๊ฐ€ ๋†“์ผ ์ „์ฒด ๊ฐ€๋กœ ์ค„์˜ ์ˆ˜
n = int(input())
# ์‚ฌ๋‹ค๋ฆฌ๋ฅผ ํƒ€๊ณ  ๋‚œ ํ›„ ๊ฒฐ์ •๋œ ์ฐธ๊ฐ€์ž๋“ค์˜ ์ตœ์ข… ์ˆœ์„œ๊ฐ€ ๊ธธ์ด (๋Œ€๋ฌธ์ž ๋ฌธ์ž)
ladder = list(input())
# โ€˜*โ€™์™€ โ€˜-โ€™ ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๊ธธ์ด k-1์ธ ๋ฌธ์ž์—ด
graph = []
for i in range(n):
    graph.append(list(input()))

up = [[-1] * k for _ in range(n)]
down = [[-1] * k for _ in range(n)]
for i in range(k):
    up[0][i] = i
    down[-1][i] = ord(ladder[i]) - 65


def up_ladder():
    for i in range(n):
        if (graph[i][0] == '?'):
            temp = up[i]
            return temp

        else:
            for j in range(k-1):
                if j > 0 and j < k - 2:
                    if (graph[i][j] == '-'):
                        up[i+1][j+1] = up[i][j]
                    elif (graph[i][j-1] == '-'):
                        up[i+1][j-1] = up[i][j]
                    else:
                        up[i+1][j] = up[i][j]
                elif j == k-2:
                    if (graph[i][j-1] == '-'):
                        up[i+1][j-1] = up[i][j]
                        up[i+1][j+1] = up[i][j+1]
                    elif (graph[i][j] == '-'):
                        up[i+1][j] = up[i][j+1]
                        up[i+1][j+1] = up[i][j]
                    else:
                        up[i+1][j+1] = up[i][j+1]
                        up[i+1][j] = up[i][j]
                else:
                    if (graph[i][j] == '-'):
                        up[i+1][j+1] = up[i][j]
                    else:
                        up[i+1][j] = up[i][j]


def down_ladder():
    temp = []
    for i in range(n-1, -1, -1):
        if (graph[i][0] == '?'):
            temp = down[i]
            return temp
        for j in range(k-1):
            if j > 0 and j < k - 2:
                if (graph[i][j] == '-'):
                    down[i-1][j+1] = down[i][j]
                elif (graph[i][j-1] == '-'):
                    down[i-1][j-1] = down[i][j]
                else:
                    down[i-1][j] = down[i][j]
            elif j == k-2:
                if (graph[i][j-1] == '-'):
                    down[i-1][j-1] = down[i][j]
                    down[i-1][j+1] = down[i][j+1]
                elif (graph[i][j] == '-'):
                    down[i-1][j] = down[i][j+1]
                    down[i-1][j+1] = down[i][j]
                else:
                    down[i-1][j+1] = down[i][j+1]
                    down[i-1][j] = down[i][j]
            elif j == 0:
                if (graph[i][j] == '-'):
                    down[i-1][j+1] = down[i][j]
                else:
                    down[i-1][j] = down[i][j]


up_temp = up_ladder()
down_temp = down_ladder()

answer = []
for i in range(k-1):
    if up_temp[i] == down_temp[i]:
        answer.append("*")
    else:
        # ๋‹ค๋ฆฌ ๋†”์ฃผ๊ธฐ
        if up_temp[i] == down_temp[i+1]:
            answer.append("-")
        # ์ด์–ด์ฃผ๊ธฐ
        elif i != 0 and up_temp[i] == down_temp[i-1]:
            answer.append("*")
        else:
            answer = ['x' for _ in range(k-1)]
            break

print(''.join(answer))


3. ํ•ด์„ค

์„ ์ˆ˜๋Š” k, ์‚ฌ๋‹ค๋ฆฌlist๋Š” k-1 ๋ผ์„œ ๋งˆ์ง€๋ง‰ ์‚ฌ๋‹ค๋ฆฌ๋ฅผ ๊ฒฐ์ •ํ•  ๋•Œ ๋งˆ์ง€๋ง‰์ด๋ž‘, ๊ทธ์ „ ์‚ฌ๋‹ค๋ฆฌ์˜ ์กฐ๊ฑด์„ ๊ฐ™์ด ๋ฌถ์–ด๋†จ๋‹ค

up, down ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์—ˆ๊ณ  ? ์˜ค๊ธฐ ์ „๊นŒ์ง€ ์„ ์ˆ˜๋“ค์˜ ์œ„์น˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ–ˆ๋‹ค

๋งˆ์ง€๋ง‰์œผ๋กœ ? ์ „ํ›„๋กœ ์œ„์น˜๊ฐ€ ๊ฐ™์œผ๋ฉด * ๋‘๊ฐœ

? ์ „ํ›„๋กœ ์œ„์น˜๊ฐ€ cross ๋˜์–ด์žˆ์œผ๋ฉด *-

์•„์˜ˆ ์•ˆ๋œ๋‹ค๋ฉด ๋ฐ”๋กœ xxxxx~~ ๋กœ ํ•˜๊ฒŒ๋” ํ–ˆ๋‹ค

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