https://www.acmicpc.net/problem/14890
์ฌ๊ธฐ์, ์๊ณ ๋ฆฌ์ฆ ๊ตฌํํ ๋ ์ฐธ๊ณ ํ๋ฉด ์ข์ ๋ถ๋ถ์ ๋ค์๊ณผ ๊ฐ๋ค.
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]
๋ฐ์ํ
'๊ฐ๋ฐ > ๐ค ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์๊ณ ๋ฆฌ์ฆ ํ์ด์์. neetcode ์ฐธ๊ณ ํด์ leetcode ์ค๋นํ๊ธฐ (leethub) (0) | 2023.09.28 |
---|---|
baekjoon. 2559 ์์ด [Silver III][python] (0) | 2023.08.20 |
baekjoon. 11729 ํ๋ ธ์ด ํ ์ด๋ ์์ [Silver I][python] (0) | 2023.07.12 |
baekjoon. 15486 ํด์ฌ 2 [Gold V][python] (0) | 2023.07.06 |
โก๏ธ baekjoon. 1489 ๋๊ฒฐ [Gold I][python] (0) | 2023.06.29 |