โ๋ฌธ์
https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14QpAaAAwCFAYi
์ฌ๊ทํธ์ถ โก๏ธ ๋ฉ๋ชจ๋ฆฌ: 45,056 KB, ์๊ฐ: 117 ms, ์ฝ๋๊ธธ์ด: 696 Bytes
๋ฐ๋ณต๋ฌธ โก๏ธ ๋ฉ๋ชจ๋ฆฌ: 44,812 KB, ์๊ฐ: 139 ms, ์ฝ๋๊ธธ์ด: 407 Bytes
โ๐ปํ์ด
์ธ๋ก๋ก ์ด์ด์ง ํ๋ฌธ์ ๊ฐ์๋ฅผ ์ธ๊ธฐ ์ํด ์ ์นํ๋ ฌ๋ก ๋ฐ๊พผ๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก ๊ธ์ํ์ ํฌ๊ธฐ๋ 8์ธ๋ฐ l์ ํ๋ฌธ์ ๊ธธ์ด๋ผ๊ณ ํ๋ฉด 8-l+1 ๋งํผ ๋ฐ๋ณตํ์ฌ ๊ธ์๋ง๋ ๋ค.
๊ทธ ๊ธ์๊ฐ ํ๋ฌธ์ธ ๊ฒฝ์ฐ 1์ ๋ํ๋ค.
๐ป์ฝ๋
# ์ฌ๊ทํธ์ถ
def palindromic(y, x, s):
if len(s) == l:
if s == "".join(s[::-1]):
return 1
else:
return 0
return palindromic(y, x + 1, s + board[y][x])
def t_palindromic(y, x, s):
if len(s) == l:
if s == "".join(s[::-1]):
return 1
else:
return 0
return t_palindromic(y, x + 1, s + t_board[y][x])
for tc in range(1, 11):
l = int(input())
board = [list(input()) for _ in range(8)]
t_board = list(map(list, zip(*board)))
res = 0
for i in range(8):
for j in range(8-l+1):
res += palindromic(i, j, "")
res += t_palindromic(i, j, "")
print(f"#{tc} {res}")
# ๋ฐ๋ณต๋ฌธ
for tc in range(1, 11):
l = int(input())
board = [list(input()) for _ in range(8)]
t_board = list(map(list, zip(*board)))
res = 0
for i in range(8):
for j in range(8-l+1):
s = board[i][j:j+l]
if s == s[::-1]:
res += 1
t_s = t_board[i][j:j+l]
if t_s == t_s[::-1]:
res += 1
print(f"#{tc} {res}")
๐ํ๊ธฐ
์ฒ์์๋ backtracking์ธ๊ฐ ์๊ฐํ๋๋ฐ ๋ฐฉ๋ฌธ ํ์๋ฅผ ๊ตณ์ด ํ ํ์๊ฐ ์์ด์ ์ด๊ฑธ backtracking์ด๋ผ๊ณ ํ ์ ์์๊น ์ถ์ด์ visit์ ๋ง๋ค์ง ์๊ณ ๊ทธ๋ฅ ์ฌ๊ท๋ก ์๊ฐํ๊ณ ํ์๋ค. ๋ฌธ์ ๋๊ธ๋ณด๋๊น ์ผ์ค๋ฐ๋ณต๋ฌธ์ด๋ผ๋ ๋ง๋ ์์ด์ ์ผ์ค๋ฐ๋ณต๋ฌธ์ผ๋ก๋ ํ์ด๋ณด์๋ค. ์๊ฐ์ ์ฌ๊ท๊ฐ ์กฐ๊ธ ๋ ๊ฑธ๋ฆฌ๋ ๊ฒ ๊ฐ์ง๋ง ๋ฉ๋ชจ๋ฆฌ๋ ์ผ์ค๋ฐ๋ณต๋ฌธ์ด ๋ ์ฐจ์งํ๋ ๊ฒ ๊ฐ๋ค.
'Coding Test > Algorithms' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 1541.์์ด๋ฒ๋ฆฐ ๊ดํธ/Python - Silver2 (0) | 2024.11.27 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] N์ผ๋ก ํํ/Python - Lv.3 (1) | 2024.11.26 |
[SWEA] 1289.์์ฌ์ ๋ฉ๋ชจ๋ฆฌ ๋ณต๊ตฌํ๊ธฐ/Python - D3 (2) | 2024.11.14 |
[SWEA] 1225.์ํธ์์ฑ๊ธฐ/Python - D3 (1) | 2024.11.13 |
[SWEA] 5215.ํ๋ฒ๊ฑฐ ๋ค์ด์ดํธ/Python - D3 (1) | 2024.11.12 |