๊ฐœ๋ฐœ/๐Ÿค– ์•Œ๊ณ ๋ฆฌ์ฆ˜

baekjoon. 14890 ๊ฒฝ์‚ฌ๋กœ [Gold III][python]

ttoance 2023. 8. 13. 07:23

https://www.acmicpc.net/problem/14890

 

14890๋ฒˆ: ๊ฒฝ์‚ฌ๋กœ

์ฒซ์งธ ์ค„์— N (2 ≤ N ≤ 100)๊ณผ L (1 ≤ L ≤ N)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ์ง€๋„๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ์นธ์˜ ๋†’์ด๋Š” 10๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.

www.acmicpc.net

 

์—ฌ๊ธฐ์„œ, ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„ํ• ๋•Œ ์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์€ ๋ถ€๋ถ„์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 

1. ๋†’์€๊ณณ -> ๋‚ฎ์€๊ณณ์œผ๋กœ ๊ฒฝ์‚ฌ๋กœ๋ฅผ ์„ธ์šธ๋•Œ, 

- ๊ฒฝ์‚ฌ๋กœ์˜ ๋†’์ด๋Š” ๋‚ฎ์€๊ณณ์œผ๋กœ ๋™์ผํ•ด์•ผ ํ•˜๋ฉฐ 

- ๋‚ฎ์€๊ณณ ๋ฐ”๋กœ ๋‹ค์Œ ๋ธ”๋Ÿญ์˜ ๋†’์ด๋Š” ๋‚ฎ์€๊ณณ๊ณผ ๋™์ผํ•ด์•ผ ํ•œ๋‹ค

2. ๋‚ฎ์€๊ณณ -> ๋†’์€๊ณณ์œผ๋กœ ๊ฒฝ์‚ฌ๋กœ๋ฅผ ์„ธ์šธ๋•Œ, 

- ๊ฒฝ์‚ฌ๋กœ์˜ ๋†’์ด๋Š” ๋‚ฎ์€๊ณณ์œผ๋กœ ๋™์ผํ•˜์•ผ ํ•˜๋ฉฐ,

- ๋‚ฎ์€๊ณณ ๋ฐ”๋กœ ์ „์˜ ๋ธ”๋Ÿญ์€ ๋‚ฎ์€๊ณณ๊ณผ ๋™์ผํ•ด์•ผ ํ•œ๋‹ค. 

 

baekjoon. 14501

 

# ์ฒซ์งธ ์ค„์— N (2 ≤ N ≤ 100)๊ณผ L (1 ≤ L ≤ N)์ด ์ฃผ์–ด์ง„๋‹ค.
# L : ๊ฒฝ์‚ฌ๋กœ ๊ธธ์ด 
N, L = map(int, input().split())
arr = [[None for _ in range(N)] for _ in range(N)]
for i in range(N):
    for j,v in enumerate(input().split()):
        arr[i][j] = int(v)


# ๋ถ€๋ถ„๋ฐฐ์—ด๋กœ ๋ฐ›์•„์„œ ํ™•์ธํ•˜๋Š” ๋ถ€๋ถ„ 
def check(subArr):
    bridge = [False for _ in range(len(subArr))] # ๊ฒฝ์‚ฌ๋กœ 

    cur = 1
    result = True
    tempArr = subArr.copy()

    cnt = 0 
    while (cur < N):
        if result == False: 
            break

        prev = tempArr[cur - 1]
        # ์ฐจ์ด๊ฐ€ 1 ๋‚˜๋ฉด ๊ฒฝ์‚ฌ๋กœ ์ถ”๊ฐ€ํ•จ 
        if tempArr[cur] == prev:
            cur = cur + 1
            
        # ๋†’์€๊ณณ -> ๋‚ฎ์€๊ณณ 
        elif tempArr[cur] == (prev - 1):
            for i in range(L):
                if (cur + i) >= N or bridge[cur + i] == True or (cur + L -1)>=N:
                    result = False
                    break
                if  tempArr[cur + i] != tempArr[cur]:
                    result = False
                    break

                bridge[cur + i] = True 

            cur = cur + L
        # ๋‚ฎ์€๊ณณ -> ๋†’์€๊ณณ 
        elif tempArr[cur] == (prev + 1):
            for i in range(L):
                if (cur - 1 - i) < 0 or bridge[cur - 1 - i] == True:
                    result = False
                    break
                if tempArr[cur - 1 - i] != tempArr[cur - 1]:
                    result = False
                    break

            cur = cur + 1
        else:
            result = False
            break


    return result




# ์‹œ์ž‘๋ถ€ํ„ฐ, ์•„๋‹˜ ๋’ค์—์„œ๋ถ€ํ„ฐ 
debug = 0 
cnt = 0 
for i in range(N):
    tempRow = []
    tempCol = []

    for j in range(N):
        tempRow.append(arr[i][j])
        tempCol.append(arr[j][i])

    # ๊ฒฝ์‚ฌ๋กœ ํ™•์ธ
    if (check(tempRow) or check(tempRow[::-1])):
        cnt = cnt + 1
    if (check(tempCol) or check(tempCol[::-1])):
        cnt = cnt + 1


print(cnt)

 

ํ‡ด์‚ฌ [Silver III][python]

๋ฐ˜์‘ํ˜•