Post

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

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

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


2. ์ฝ”๋“œ

Python3 31256KB 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
k = int(input())
n = int(input())
alpha = [chr(65 + i) for i in range(k)]
start_order = list(' '.join(alpha))
final_order = list(' '.join(list(input())))
h_len = len(start_order)
ladder = []
ladder.append(start_order)
for i in range(n):
    line = list(input())
    line = list(' ' + ' '.join(line) + ' ')
    if '?' in line:
        hidden = i + 1  # ๋ฌผ์Œํ‘œ ๊ฐ€๋กœ์ค„ ์œ„์น˜
    ladder.append(line)
ladder.append(final_order)

final_idx = []
for a in final_order:
    final_idx.append(start_order.index(a))

# ๊ฐ™์€ ์•ŒํŒŒ๋ฒณ ๋ฌผ์Œํ‘œ์—์„œ ๋งŒ๋‚˜์„œ ๊ธธ์ฐพ๊ธฐ
def connect_road(idx):
    sx, sy = 0, idx
    ex, ey = n+1, final_idx.index(idx)
    # ์œ„์—์„œ ๋ฌผ์Œํ‘œ๊นŒ์ง€ ๋‚ด๋ ค๊ฐ€๊ธฐ
    while sx != hidden:
        sx += 1
        # ๋ฌผ์Œํ‘œ ๋ผ์ธ ๋„์ฐฉ์‹œ ์›€์ง์ด์ง€ ์•Š๋„๋ก ํ•จ
        if sx == hidden:
            break
        # ์™ผ์ชฝ์— ๋ง‰๋Œ€๊ธฐ ์žˆ์„ ์‹œ ์™ผ์ชฝ์œผ๋กœ ์ด๋™
        if 0 < sy - 1 and ladder[sx][sy - 1] == '-':
            sy -= 2
        # ์˜ค๋ฅธ์ชฝ์— ๋ง‰๋Œ€๊ธฐ ์žˆ์„ ์‹œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™
        elif sy + 1 < h_len and ladder[sx][sy + 1] == '-':
            sy += 2
    # ๋ฐ‘์—์„œ ๋ฌผ์Œํ‘œ๊นŒ์ง€ ์˜ฌ๋ผ๊ฐ€๊ธฐ
    while ex != hidden:
        ex -= 1
        # ๋ฌผ์Œํ‘œ ๋ผ์ธ ๋„์ฐฉ์‹œ ์›€์ง์ด์ง€ ์•Š๋„๋ก ํ•จ
        if ex == hidden:
            break
        # ์™ผ์ชฝ์— ๋ง‰๋Œ€๊ธฐ ์žˆ์„ ์‹œ ์™ผ์ชฝ์œผ๋กœ ์ด๋™
        if 0 < ey - 1 and ladder[ex][ey - 1] == '-':
            ey -= 2
        # ์˜ค๋ฅธ์ชฝ์— ๋ง‰๋Œ€๊ธฐ ์žˆ์„ ์‹œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™
        elif ey + 1 < h_len and ladder[ex][ey + 1] == '-':
            ey += 2
    # ๊ฐ€๋กœ ๊ธธ์ด ์ฐจ์ด๊ฐ€ 3์ด์ƒ ๋‚  ๊ฒฝ์šฐ(์‹ค์ œ๋กœ๋Š” ๊ฐ€๋กœ ํ•œ ์นธ ์ฐจ์ด๋ณด๋‹ค ํด ๊ฒฝ์šฐ)
    if abs(sy - ey) > 2:
        return False    # ์‚ฌ๋‹ค๋ฆฌ ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ
    # ๋‹ค๋ฆฌ๋ฅผ ์•ˆ๋†”๋„ ๋งŒ๋‚˜๋Š” ๊ฒฝ์šฐ
    if sy == ey:
        return True
    # ๋‹ค๋ฆฌ ๋†“๊ธฐ(์ค‘๋ณต์œผ๋กœ ๋†“๊ธฐ ๊ฐ€๋Šฅ)
    ladder[hidden][max(sy, ey)-1] = '-'
    return True

fail = False

for i in range(h_len):
    if start_order[i] == ' ':
        continue
    # ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ
    if not connect_road(i): 
        print('x' * (k-1))
        fail = True
        break

if not fail:
    print(''.join(ladder[hidden]).replace(' ', '').replace('?', '*'))


3. ํ•ด์„ค

1
2
3
4
5
6
7
A   B   C   D   E   F   G   H   I   J 
  *   -   *   *   *   -   *   *   *   
  -   *   -   *   *   *   *   *   *   
  ?   ?   ?   ?   ?   ?   ?   ?   ?   
  -   *   *   -   *   *   *   -   *   
  *   *   -   *   -   *   -   *   -   
A   C   G   B   E   D   J   F   I   H

์œ„์ฒ˜๋Ÿผ ์‹œ์ž‘์ ๊ณผ ๋นˆ์นธ์ด ์žˆ๋Š” ์‚ฌ๋‹ค๋ฆฌ ๋ชจ์–‘์„ ๋งŒ๋“ ๋‹ค.

๊ฐ™์€ ์•ŒํŒŒ๋ฒณ์˜ ์‹œ์ž‘ ์œ„์น˜์™€ ๋ ์œ„์น˜์˜ ์ธ๋ฑ์Šค๋ฅผ ?์˜ ์œ„์น˜๋กœ ์‚ฌ๋‹ค๋ฆฌ๋ฅผ ํƒ€๋ฉด์„œ ์˜ฎ๊ธด ํ›„

  1. ๋ฐ”๋กœ ๋งŒ๋‚˜๋ฉด(์ธ๋ฑ์Šค๊ฐ€ ๋˜‘๊ฐ™์œผ๋ฉด) ๋‹ค๋ฆฌ๋ฅผ ๋†“์ง€ ์•Š๊ณ 
  2. ๊ฐ€๋กœ๋กœ 2์นธ ์ฐจ์ด๊ฐ€ ๋‚  ๋•Œ๋Š” ๊ทธ ์‚ฌ์ด์— ๋‹ค๋ฆฌ๋ฅผ ๋†“๊ณ 
  3. ๊ฐ€๋กœ๊ฐ€ 2์นธ๋ณด๋‹ค ๋” ํฌ๊ฒŒ ์ฐจ์ด๊ฐ€ ๋‚  ๋•Œ๋Š” ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์ž„(๋‹ค๋ฆฌ๊ฐ€ ๊ธธ์ด 1์ด ๋„˜์œผ๋ฉด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ)

3๋ฒˆ์ผ ๊ฒฝ์šฐ๋Š” ๋ฐ”๋กœ x๋ฅผ k ๊ธธ์ด๋งŒํผ ์ถœ๋ ฅํ•˜๊ณ  2์˜ ๊ฒฝ์šฐ ๋‹ค๋ฆฌ๋ฅผ ๋‹ค ๋†“์€ ํ›„ ? ์ž๋ฆฌ๋Š” *๋กœ ๋ณ€๊ฒฝํ•ด ์ถœ๋ ฅ

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