1. 문제
https://www.acmicpc.net/problem/1780
1780번: 종이의 개수
N×N크기의 행렬로 표현되는 종이가 있다. 종이의 각 칸에는 -1, 0, 1 중 하나가 저장되어 있다. 우리는 이 행렬을 다음과 같은 규칙에 따라 적절한 크기로 자르려고 한다. 만약 종이가 모두 같은 수
www.acmicpc.net
2. 풀이
import sys
input = sys.stdin.readline
# n : 종이의 크기
n = int(input())
# cntList : -1, 0, 1의 개수를 저장할 리스트
cntList = [0 for i in range(3)]
# paperList : 종이의 수들을 입력할 이차원 리스트
paperList = [list(map(int, input().strip().split())) for i in range(n)]
# for i in paperList:
# print(i)
def checkPaper(paperList, cntList, istrIndex, iendIndex, jstrIndex, jendIndex):
# print("istrIndex :", istrIndex, "iendIndex :", iendIndex)
# print("jstrIndex :", jstrIndex, "jendIndex :", jendIndex)
standardNum = paperList[istrIndex][jstrIndex]
cntNum = 0
for i in paperList[istrIndex:iendIndex]:
cntNum += i[jstrIndex:jendIndex].count(standardNum)
if cntNum == ((iendIndex - istrIndex) ** 2):
cntList[standardNum] += 1
else:
iterWidth = (iendIndex - istrIndex) // 3
for i in range(3):
for j in range(3):
checkPaper(paperList, cntList, istrIndex + (iterWidth * i), istrIndex + (iterWidth * (i + 1)),
jstrIndex + (iterWidth * j), jstrIndex + (iterWidth * (j + 1)))
checkPaper(paperList, cntList, 0, len(paperList), 0, len(paperList))
print(cntList[-1])
print(cntList[0])
print(cntList[1])
3. FeedBack
- 오랜만에 재귀 문제이다.
재귀같은 경우는 각 케이스마다 문제의 조건을 적용하여 재귀를 개발해야한다는 점에서 코딩 숙련도가 가장 잘드러나는 알고리즘인듯하다.
처음 백준을 시작할 때보다는 알고리즘을 생각해내고 개발하는데까지 빨라지기는 했지만, 더 숙련되어서 지금보다 더 효율적이고, 빠르게 개발하는 개발자로서의 목표을 다시 되새기는 시간이였다.
'알고리즘 공부 > 백준' 카테고리의 다른 글
(Python)백준 코딩테스트 연습 - 연속합(1912) (0) | 2023.05.19 |
---|---|
(Python)백준 코딩테스트 연습 - 타일링(1793) (0) | 2023.05.11 |
(Python)백준 코딩테스트 연습 - 크로스워드(1706) (2) | 2023.04.30 |
(Python)백준 코딩테스트 연습 - 제곱수의 합(1699) (0) | 2023.04.28 |
(Python)백준 코딩테스트 연습 - 랜선 자르기(1654) (0) | 2023.04.26 |