https://www.acmicpc.net/problem/7569
해당 문제는 https://alpacalabs.tistory.com/18 와 같은 제목의 문제로 약간 만 다르다. 테스트 예제는 맞지만 계속 틀렸다고 하여 문제가 무엇인지 검색해 보았지만 의문은 해결되지 않았다. 아래는 테스는 예제는 맞지만 틀렸다고 한 코드이다.
import sys
input = sys.stdin.readline
col_length, row_length, z_length = map(int,input().split())
box = list()
stack1 = stack2 = list()
for _ in range(row_length*z_length):
box.append(list(map(int,input().split())))
for r in range(row_length*z_length):
for c in range(col_length):
if box[r][c] == 1:
stack1.append((r,c))
count = 0
result = True
while stack1:
stack2 = stack1[:]
stack1.clear()
while stack2:
row , col = stack2.pop()
if row + row_length < len(box) and box[row+row_length][col] == 0: # 추가된 부분
stack1.append((row_length+row,col))
box[row+row_length][col] = 1
if row - row_length >= 0 and box[row-row_length][col] == 0: # 추가된 부분 2
stack1.append((row-row_length,col))
box[row-row_length][col] = 1
if row + 1 < len(box) and box[row+1][col] == 0:
stack1.append((row+1,col))
box[row+1][col] = 1
if row - 1 >= 0 and box[row-1][col] == 0:
stack1.append((row-1,col))
box[row-1][col] = 1
if col+1 < len(box[0]) and box[row][col+1] == 0:
stack1.append((row,col+1))
box[row][col+1] = 1
if col - 1 >= 0 and box[row][col-1] == 0:
stack1.append((row,col-1))
box[row][col-1] = 1
count += 1
stack2.clear()
for r in range(row_length*z_length):
for c in range(col_length):
if box[r][c] == 0:
result = False
if result:
print(count-1)
else:
print(-1)
의문이 해결되지 않아 여러 테스트 케이스를 gpt 한테 부탁받아서 실행해보니 row+1 ,row-1 로 가는 과정에서 층을 넘나들면서 가는 것을 확인하고 해당 조건문을 수정해 주었더니 성공하였다.
import sys
input = sys.stdin.readline
col_length, row_length, z_length = map(int,input().split())
box = list()
stack1 = stack2 = list()
for _ in range(row_length*z_length):
box.append(list(map(int,input().split())))
for r in range(row_length*z_length):
for c in range(col_length):
if box[r][c] == 1:
stack1.append((r,c))
count = 0
result = True
while stack1:
stack2 = stack1[:]
stack1.clear()
while stack2:
row , col = stack2.pop()
if row + row_length < len(box) and box[row+row_length][col] == 0: # 추가된 부분
stack1.append((row_length+row,col))
box[row+row_length][col] = 1
if row - row_length >= 0 and box[row-row_length][col] == 0: # 추가된 부분 2
stack1.append((row-row_length,col))
box[row-row_length][col] = 1
# row+1 < ((row//row_length)+1)*row_length 조건을 추가하여 2차원을 3차원으로 되도록 층을 넘어가는 것을 막았다.
if row + 1 < len(box) and row+1 < ((row//row_length)+1)*row_length and box[row+1][col] == 0:
stack1.append((row+1,col))
box[row+1][col] = 1
# row-1 >= (row//row_length)*row_length 아래층 가지 않도록 조건 생성
if row - 1 >= 0 and row-1 >= (row//row_length)*row_length and box[row-1][col] == 0:
stack1.append((row-1,col))
box[row-1][col] = 1
if col+1 < len(box[0]) and box[row][col+1] == 0:
stack1.append((row,col+1))
box[row][col+1] = 1
if col - 1 >= 0 and box[row][col-1] == 0:
stack1.append((row,col-1))
box[row][col-1] = 1
count += 1
stack2.clear()
for r in range(row_length*z_length):
for c in range(col_length):
if box[r][c] == 0:
result = False
if result:
print(count-1)
else:
print(-1)
'코딩테스트' 카테고리의 다른 글
[python] 1389 번 : 케빈 베이컨의 6단계 법칙 (실버 1) (0) | 2024.07.19 |
---|---|
[python] 백준 2579 : 계단 오르기 (실버 3) (0) | 2024.07.18 |
[python] 백준 17219 : 비밀번호 찾기 (실버 4) (0) | 2024.07.17 |
[python] 백준 18870 번 : 좌표 압축 (실버 2) (0) | 2024.07.16 |
[python] 백준 14940 번 : 쉬운 최단거리 (실버 1) (0) | 2024.07.15 |