def dfs(x,y,cnt): global result if cnt > result: return if x >= 10: result = min(result,cnt) return if y >= 10: dfs(x+1,0,cnt) return if arr[x][y] == 1: for paper_size in range(5,0,-1): if paper[paper_size]: if x + paper_size <= 10 and y + paper_size <= 10: flag = False for dx in range(paper_size): for dy in range(paper_size): if not arr[x+dx][y+dy]: flag = True break if flag: break if not flag: for dx in range(paper_size): for dy in range(paper_size): arr[x+dx][y+dy] = 0 paper[paper_size] -= 1 dfs(x,y+paper_size-1,cnt+1) paper[paper_size] += 1 for dx in range(paper_size): for dy in range(paper_size): arr[x+dx][y+dy] = 1 else: dfs(x,y+1,cnt) arr = [list(map(int,input().split())) for _ in range(10)] paper = [0]+[5]*5 result = float('inf') dfs(0,0,0) print(result if result != float('inf') else -1)
def check(x,y,size): if x + size > 10 or y + size >10: return False for dx in range(size): for dy in range(size): if not arr[x+dx][y+dy]: return False return True def set_position(x,y,size,val): for dx in range(size): for dy in range(size): arr[x+dx][y+dy] = val def dfs(x,y,cnt): global result if cnt >= result: return finish = True while x < 10: while y < 10: if arr[x][y] == 1: finish = False for paper_size in range(5,0,-1): if paper[paper_size] and check(x,y,paper_size): set_position(x,y,paper_size,0) paper[paper_size] -= 1 dfs(x,y+paper_size-1,cnt+1) paper[paper_size] += 1 set_position(x,y,paper_size,1) return y += 1 x += 1 y = 0 if finish: result = min(result,cnt) arr = [list(map(int,input().split())) for _ in range(10)] paper = [0]+[5]*5 result = float('inf') dfs(0,0,0) print(result if result != float('inf') else -1)
'알고리즘 > 백준_복기_미완료' 카테고리의 다른 글
[BOJ/백준] 17281 ⚾ (0) | 2021.05.06 |
---|---|
[BOJ/백준] 17114 하이퍼 토마토 (0) | 2021.05.06 |
[BOJ/백준] 16954 움직이는 미로 탈출 (0) | 2021.05.06 |
[BOJ/백준] 16637 괄호 추가하기 (0) | 2021.05.06 |
[BOJ/백준] 4803 트리 (0) | 2021.05.06 |