-
BOJ 백준 파이썬 2636 치즈백준 2022. 6. 29. 05:53
boj 5574 일루미네이션이랑 똑같은 문제라 보면 된다.
일루미네이션은 육각형이라 배열 확장이 필요하지만 이 문제는 필요 없다.
결국, 문제는 달라도 푸는 아이디어는 똑같구나라는 생각을 하게 되었으며
문제 풀이 아이디어를 기록하기 위해 블로그를 하게 된 계기인 문제이다.
바깥쪽인 0부터 탐색하며 바깥쪽 0과 접촉한 1은 0으로 변한다는 아이디어를 적용하면 된다.
문제 풀이의 시작이자 끝
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960import sysfrom collections import dequefrom itertools import combinationsdef bfs(x,y):global okglobal tempq.append((x,y)) ##큐에 원소 집어넣기while q:x,y=q.popleft()for k in range(4):nx=x+dx[k]ny=y+dy[k]if 0<=nx<n and 0<=ny<m:if board[nx][ny]==1:board[nx][ny]=0ch[nx][ny]=1ok=1temp+=1#print(999)else: ## 0일시if ch[nx][ny]==-1:q.append((nx,ny))ch[nx][ny]=1if __name__=='__main__':# sys.stdin=open("input.txt", "rt")n,m=map(int,input().split()) ##n=세로(행), m=가로(열)board=[list(map(int,input().split())) for _ in range(n)] ##입력 받기dx=[0,0,-1,1]dy=[1,-1,0,0]time=0q=deque()res=0while True:temp=0ch=[[-1]*m for _ in range(n)]ch[0][0]=1ok=0bfs(0,0)#print(999)time+=1if temp!=0:res=tempif ok==0:print(time-1)print(res)breakcs '백준' 카테고리의 다른 글
BOJ 백준 파이썬 2668 숫자 고르기 (0) 2022.06.29 BOJ 백준 파이썬 13023 ABCDE (0) 2022.06.29 BOJ 백준 파이썬 1600 말이 되고픈 원숭이 (0) 2022.06.29 BOJ 백준 파이썬 16954 움직이는 미로 탈출 (0) 2022.06.29 BOJ 백준 파이썬 16973 직사각형 탈출 (0) 2022.06.29