We will find a way, we always have.

-interstellar

Problem Solving/๋ฐฑ์ค€

[๋ฐฑ์ค€] 1331๋ฒˆ: ๋‚˜์ดํŠธ ํˆฌ์–ด - ํŒŒ์ด์ฌ

Redddy 2022. 6. 13. 21:39

๐Ÿ”ˆ ๋ฌธ์ œ

๋‚˜์ดํŠธ ํˆฌ์–ด๋Š” ์ฒด์ŠคํŒ์—์„œ ๋‚˜์ดํŠธ๊ฐ€ ๋ชจ๋“  ์นธ์„ ์ •ํ™•ํžˆ ํ•œ ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธํ•˜๋ฉฐ, ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ฐฉ๋ฌธํ•˜๋Š” ์นธ์—์„œ ์‹œ์ž‘์ ์œผ๋กœ ๋Œ์•„์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ์ด๋‹ค. ๋‹ค์Œ ๊ทธ๋ฆผ์€ ๋‚˜์ดํŠธ ํˆฌ์–ด์˜ ํ•œ ์˜ˆ์ด๋‹ค.

 

์˜์‹์ด๋Š” 6×6 ์ฒด์ŠคํŒ ์œ„์—์„œ ๋˜ ๋‹ค๋ฅธ ๋‚˜์ดํŠธ ํˆฌ์–ด์˜ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์œผ๋ ค๊ณ  ํ•œ๋‹ค. ์ฒด์ŠคํŒ์˜ ํ•œ ์นธ์€ A, B, C, D, E, F ์ค‘์—์„œ ํ•˜๋‚˜์™€ 1, 2, 3, 4, 5, 6 ์ค‘์—์„œ ํ•˜๋‚˜๋ฅผ ์ด์–ด ๋ถ™์ธ ๊ฒƒ์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ์˜์‹์ด์˜ ๋‚˜์ดํŠธ ํˆฌ์–ด ๊ฒฝ๋กœ๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ์ด๊ฒƒ์ด ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์ด๋ฉด Valid, ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์œผ๋ฉด Invalid๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

 

๐Ÿ“์ž…๋ ฅ

36๊ฐœ์˜ ์ค„์— ๋‚˜์ดํŠธ๊ฐ€ ๋ฐฉ๋ฌธํ•œ ์ˆœ์„œ๋Œ€๋กœ ์ž…๋ ฅ์ด ์ฃผ์–ด์ง„๋‹ค. ์ฒด์ŠคํŒ์— ์กด์žฌํ•˜๋Š” ์นธ๋งŒ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค.

 

๐Ÿ“‘์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๋ฌธ์ œ์˜ ์ •๋‹ต์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

๐Ÿ“š ๋ฌธ์ œ ํ•ด์„

๋‚˜์ดํŠธ์˜ ์ด๋™์€ (1,2),(1,-2),(2,1),(2,-1),(-1,2),(-1,-2),(-2,1),(-2,-1) ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.
Invalid ํŒ์ •์„ ๋ฐ›๋Š” ๊ฒฝ์šฐ๋Š” 3๊ฐ€์ง€ ์ •๋„ ์žˆ๋‹ค.

์ฒซ๋ฒˆ์งธ : ๋‚˜์ดํŠธ์˜ ์ด๋™์— ๋”ฐ๋ฅด์ง€ ์•Š๋Š” ๊ฒฝ์šฐ. ์˜ˆ๋ฅผ ๋“ค์–ด (3,0) ์ด๋ ‡๊ฒŒ ์ด๋™
๋‘๋ฒˆ์งธ : ๋ฐฉ๋ฌธํ–ˆ๋˜ ๊ณณ์„ ๋‹ค์‹œ ๋ฐฉ๋ฌธํ•˜๋Š” ๊ฒฝ์šฐ. 36๋ฒˆ๋งŒ์— ๋ชจ๋“  ์ฒด์ŠคํŒ์„ ๋ฐฉ๋ฌธํ•˜๋ ค๋ฉด ๊ฐ™์€๊ณณ ๋‹ค์‹œ ๋ฐฉ๋ฌธํ•˜๋ฉด ์•ˆ๋œ๋‹ค.
์„ธ๋ฒˆ์งธ : ๋งˆ์ง€๋ง‰๊นŒ์ง€ ์ด๋™์„ ๋งˆ์นœ ์ž๋ฆฌ์—์„œ ํ•œ๋ฒˆ ์›€์ง์—ฌ์„œ ๋‹ค์‹œ ์ฒ˜์Œ ์œ„์น˜๋กœ ๋„๋‹ฌํ•  ์ˆ˜ ์—†์„ ๋•Œ.

์ž…๋ ฅ๋ฐ›์€ ๊ฐ’๋“ค์„ ์œ„ ์‚ฌํ•ญ๋“ค๋กœ ์ฒดํฌํ•˜์—ฌ ๋‹ต์„ ์œ ์ถ”ํ•ด๋‚ด์—ˆ๋‹ค.

 

 

๋‚˜์ดํŠธ์˜ ์ด๋™์— ๋”ฐ๋ฅด๋Š” ๊ฒฝ์šฐ

if (abs(ord(pre[0]) - ord(now[0])) ==1 and abs(int(pre[1])-int(now[1])) == 2):
	pass
elif (abs(ord(pre[0]) - ord(now[0])) ==2 and abs(int(pre[1])-int(now[1])) == 1):
    pass

 

๋ฐฉ๋ฌธํ–ˆ๋˜ ๊ณณ์„ ๋‹ค์‹œ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ

if now not in visited:
	pass

 

๐Ÿ’ป ์ฝ”๋“œ

import sys
input = sys.stdin.readline
ans = "Valid"
visited = [] # ๋ฐฉ๋ฌธํ–ˆ๋˜ ๊ณณ์„ ๋‹ด์„ ๋ฆฌ์ŠคํŠธ
for i in range(36):
    if i == 0: # ๋งˆ์ง€๋ง‰๊ฐ’๊ณผ ์ฒดํฌํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ๋”ฐ๋กœ ๋นผ๋†“์Œ
        start = input().rstrip()
        pre = start
    elif i < 35:
        now = input().rstrip()
        if ((abs(ord(pre[0]) - ord(now[0])) ==1 and abs(int(pre[1])-int(now[1])) == 2) or (abs(ord(pre[0]) - ord(now[0])) ==2 and abs(int(pre[1])-int(now[1])) == 1)) and now not in visited:
            pass
        else:   
            ans = "Invalid"
        visited.append(now) # ๋ฐฉ๋ฌธ๋ฆฌ์ŠคํŠธ์— ์‚ฝ์ž…
        pre = now
    else:
        now = input().rstrip()
        if ((abs(ord(now[0]) - ord(start[0])) ==1 and abs(int(now[1])-int(start[1])) == 2) or (abs(ord(now[0]) - ord(start[0])) ==2 and abs(int(now[1])-int(start[1])) == 1)) and now not in visited:
            pass
        else:
            ans = "Invalid"

print(ans)