백준 / 2578번 빙고 - 구현 / Python 파이썬

2025. 3. 29. 22:11·coding test - python/백준
728x90
반응형

 
*문제 출처는 백준에 있습니다.

문제 제목: 2578번 빙고

문제 사이트: https://www.acmicpc.net/problem/2578
 
 


나의 풀이
 
후 빡코딩이였다 오랜만에 집중함
for문이 반복되는만큼 탈출해야하는 것이랑 제대로 다 생각해서 코딩해야함
maps 같이 2차원 행렬 사용된다싶으면 무조건  pseudo  code 만들고 진입하자 ㅠㅠ
 

import sys

input = sys.stdin.readline
# 세 개 이상 그어지는 순간 "빙고"
maps = []
visited = [[False] * 5 for i in range(5)]
cnt = 0
answer = 0 # 사회자가 몇번 불렀는지 카운트
# 체크했던 인덱스들 저장
c_row = [0] * 5
c_cal = [0] * 5

c_right = False
c_left = False
for i in range(5):
    maps.append(list(map(int,input().split())))

def check_row():
    global visited, cnt, answer
    global c_row
    if cnt == 3:
        return
    
    for i in range(5):
        if c_row[i] != 1:
            if visited[i][:] == [True, True, True, True, True]:
                cnt += 1
                c_row[i] = 1
        else:
            continue


def check_cal():
    global visited, cnt, answer
    global c_cal
    if cnt == 3:
        return
    
    for i in range(5):
        tmp = []
        if c_cal[i] == 1:
            continue
        tmp_num = 0
        for j in range(5):
            tmp_num = i
            if visited[j][i] == True:
                tmp.append(True)

        if tmp == [True, True, True, True, True]:
            cnt += 1
            c_cal[tmp_num] = 1

def check_right(): # 오른쪽 위부터 시작하는 대각선
    global visited, cnt, answer
    global c_right
    tmp = []
    if cnt == 3:
        return
    
    if c_right == True:
        return
    else:
        for i in range(5):
            if visited[i][5-i-1] == True:
                tmp.append(True)
        if tmp == [True, True, True, True, True]:
            cnt += 1
            c_right = True

def check_left(): # 왼쪽 위부터 시작하는 대각선
    global visited, cnt, answer
    global c_left
    tmp = []
    if cnt == 3:
        return
    
    if c_left == True:
        return
    else:
        for i in range(5):
            if visited[i][i] == True:
                tmp.append(True)
        if tmp == [True, True, True, True, True]:
            cnt += 1
            c_left = True

def call_number(number):
    global answer
    answer += 1
    for i in range(5):
        for j in range(5):
            if maps[i][j] == number:
                visited[i][j] = True
                check_row()
                check_cal()
                check_right()
                check_left()
                return


while cnt < 3:
    numbers = list(map(int,input().split()))
    for i in range(5):
        call_number(numbers[i])
        if cnt >= 3:
            break
print(answer)

 

GPT 답안
훨씬 최적화 잘돼있음 자주 보고 익히자

import sys

input = sys.stdin.readline
# 세 개 이상 그어지는 순간 "빙고"
maps = []
visited = [[False] * 5 for _ in range(5)]
cnt = 0
answer = 0  # 사회자가 몇 번 불렀는지 카운트

# 체크했던 인덱스들 저장
c_row = [0] * 5
c_cal = [0] * 5
c_right = False
c_left = False

for i in range(5):
    maps.append(list(map(int, input().split())))

def check_all():
    global c_row, c_cal, c_left, c_right
    bingo = 0

    # 가로 줄 검사
    for i in range(5):
        if c_row[i] == 0 and visited[i] == [True] * 5:
            c_row[i] = 1
            bingo += 1

    # 세로 줄 검사
    for j in range(5):
        if c_cal[j] == 0 and all(visited[i][j] for i in range(5)):
            c_cal[j] = 1
            bingo += 1

    # 왼쪽 대각선
    if not c_left and all(visited[i][i] for i in range(5)):
        c_left = True
        bingo += 1

    # 오른쪽 대각선
    if not c_right and all(visited[i][4 - i] for i in range(5)):
        c_right = True
        bingo += 1

    return bingo

def call_number(number):
    global answer, cnt
    answer += 1
    for i in range(5):
        for j in range(5):
            if maps[i][j] == number:
                visited[i][j] = True
                cnt += check_all()
                return

while cnt < 3:
    numbers = list(map(int, input().split()))
    for i in range(5):
        call_number(numbers[i])
        if cnt >= 3:
            break

print(answer)

※ 알아야 할 것

- all()은 파이썬에서 자주 쓰이는 내장 함수로, 모든 요소가 참(True)이면 True, 하나라도 거짓(False)이면 False를 반환
 
 

728x90
반응형

'coding test - python > 백준' 카테고리의 다른 글

백준 / 14500번 테트로미노- 백트래킹, dfs / Python 파이썬  (0) 2025.04.01
백준 / 1197번 최소 스패닝 트리 - 힙,해시 / Python 파이썬  (0) 2025.03.30
백준 / 1834번 나머지와 몫이 같은 수 / Python 파이썬  (0) 2025.03.29
백준 / 17298번 오큰수 - 스택 / Python 파이썬  (0) 2025.03.29
백준 / 15654번 N과 M(5) / Python 파이썬  (0) 2023.07.14
'coding test - python/백준' 카테고리의 다른 글
  • 백준 / 14500번 테트로미노- 백트래킹, dfs / Python 파이썬
  • 백준 / 1197번 최소 스패닝 트리 - 힙,해시 / Python 파이썬
  • 백준 / 1834번 나머지와 몫이 같은 수 / Python 파이썬
  • 백준 / 17298번 오큰수 - 스택 / Python 파이썬
sillon
sillon
꾸준해지려고 합니다..
    반응형
  • sillon
    sillon coding
    sillon
  • 전체
    오늘
    어제
    • menu (614)
      • notice (2)
      • python (68)
        • 자료구조 & 알고리즘 (23)
        • 라이브러리 (19)
        • 기초 (8)
        • 자동화 (14)
        • 보안 (1)
      • coding test - python (301)
        • Programmers (166)
        • 백준 (76)
        • Code Tree (22)
        • 기본기 문제 (37)
      • coding test - C++ (5)
        • Programmers (4)
        • 백준 (1)
        • 기본기문제 (0)
      • 공부정리 (5)
        • 신호처리 시스템 (0)
        • Deep learnig & Machine lear.. (41)
        • Data Science (18)
        • Computer Vision (17)
        • NLP (40)
        • Dacon (2)
        • 모두를 위한 딥러닝 (강의 정리) (4)
        • 모두의 딥러닝 (교재 정리) (9)
        • 통계 (2)
      • HCI (23)
        • Haptics (7)
        • Graphics (11)
        • Arduino (4)
      • Project (21)
        • Web Project (1)
        • App Project (1)
        • Paper Project (1)
        • 캡스톤디자인2 (17)
        • etc (1)
      • OS (10)
        • Ubuntu (9)
        • Rasberry pi (1)
      • App & Web (9)
        • Android (7)
        • javascript (2)
      • C++ (5)
        • 기초 (5)
      • Cloud & SERVER (8)
        • Git (2)
        • Docker (1)
        • DB (4)
      • Paper (7)
        • NLP Paper review (6)
      • 데이터 분석 (0)
        • GIS (0)
      • daily (2)
        • 대학원 준비 (0)
      • 영어공부 (6)
        • job interview (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    소수
    programmers
    Python
    백준
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
sillon
백준 / 2578번 빙고 - 구현 / Python 파이썬
상단으로

티스토리툴바