[백준] 빙고

Hyunwoo Park·2021년 4월 8일
0

알고리즘

목록 보기
15/19

solved.ac 기준 실버 5인데, 생각보다 까다로웠다.

bingo 라는 함수를 통하여 각각의 가로줄, 세로줄, 대각선에서 0이 연속으로 5개인 경우 total 변수를 1 증가시켜 준다.
함수의 마지막 부분에서 total 변수가 3 이상인 경우 True를 반환한다.

change 함수를 통하여 해당 인덱스의 값이 함수 인자와 같으면 0으로 바꿔준다.

main 부분에서 반복문을 돌며 ct 변수를 증가시키며 빙고판을 하나씩 0으로 바꿔주고 bingo가 True를 반환하는 경우 반복문을 빠져나오고, ct를 출력한다.

def bingo():
    
    global total
    total = 0
    #가로 줄
    for i in range(5):
        ct = 0
        for j in range(5):
            if arr[i][j] == 0:
                ct += 1
                
        if ct == 5:
            total += 1
        
    #세로 줄
    for i in range(5):
        ct = 0
        for j in range(5):
            if arr[j][i] == 0:
                ct += 1
                
        if ct == 5:
            total += 1
        
    #대각선
    ct = 0
    for i in range(5):
        for j in range(5):
            if i == j and arr[i][j] == 0:
                ct += 1
                
        if ct == 5:
            total += 1
        
    ct = 0
    for i in range(5):
        for j in range(5):
            if i == 4-j and arr[4-j][j] == 0:
                ct += 1
                
        if ct == 5:
            total += 1
        
    if total >= 3:
        return True
    
    else:
        return False


def change(a):
    
    for i in range(5):
        for j in range(5):
            if arr[i][j] == a:
                arr[i][j] = 0
                return

arr = []
ans = []
bing_cnt = 0
total = 0
ct = 0
for i in range(5):
    arr.append(list(map(int, input().split())))
    
for i in range(5):
    ans.append(list(map(int, input().split())))
    
for i in range(5):
    for j in range(5):
        ct += 1
        change(ans[i][j])
        
        if bingo():
            break
            
    if bingo():
        break
        
print(ct)
profile
만나서 반갑습니다.

0개의 댓글