[백준] 4963 파이썬(python)

2022. 1. 8. 16:44·IT/알고리즘
반응형

문제

https://www.acmicpc.net/problem/4963

 

4963번: 섬의 개수

입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스의 첫째 줄에는 지도의 너비 w와 높이 h가 주어진다. w와 h는 50보다 작거나 같은 양의 정수이다. 둘째 줄부터 h개 줄에는 지도

www.acmicpc.net

문제 풀이

import collections
import sys

dx = [-1, 0, 1, 1, 1, 0, -1, -1]
dy = [-1, -1, -1, 0, 1, 1, 1, 0]

def bfs(graph, y, x):
    graph[y][x] = 0
    queue = collections.deque([(y, x)])

    while queue:
        yy, xx = queue.popleft()

        for i in range(8):
            nx = xx + dx[i]
            ny = yy + dy[i]
            if nx < 0 or nx >= w or ny < 0 or ny >= h:
                continue

            if graph[ny][nx] == 1:
                graph[ny][nx] = 0
                queue.append((ny, nx))

while True:
    w, h = map(int, sys.stdin.readline().split())
    if w == 0 and h == 0:
        break

    maps = [list(map(int, sys.stdin.readline().split())) for _ in range(h)]
    count = 0
    for y in range(h):
        for x in range(w):
            if maps[y][x] == 1:
                bfs(maps, y, x)
                count += 1
    print(count)
728x90
반응형
저작자표시 비영리 변경금지 (새창열림)
'IT/알고리즘' 카테고리의 다른 글
  • Consistent hashing, 일관된 해싱
  • [백준] 2468 파이썬(python)
  • [백준] 1697 파이썬(python)
  • [백준] 11403 파이썬(python)
상쾌한기분
상쾌한기분
  • 상쾌한기분
    상쾌한기분
    상쾌한기분
  • 전체
    오늘
    어제
    • 분류 전체보기 (251)
      • Python (44)
        • Python (26)
        • Django (6)
        • Flask (4)
        • Open Source (6)
      • Kotlin & Java (5)
        • Spring (2)
        • 프로젝트 (1)
      • Go (11)
      • Database (24)
        • MySQL (21)
        • Redis (3)
      • Infrastructure (2)
        • CDC (4)
        • Kafka (5)
        • Prometheus (2)
        • Fluentd (11)
        • Docker (1)
        • Airflow (2)
        • VPN (2)
      • IT (26)
        • AI (9)
        • Langchain (8)
        • Web (18)
        • Git (8)
        • 리팩토링 (9)
        • Micro Service Architecture (8)
        • Clean Code (16)
        • Design Pattern (0)
        • 수학 (1)
        • 알고리즘 (14)
      • OS (14)
        • Centos (10)
        • Ubuntu (3)
        • Mac (1)
      • Search Engine (2)
        • ElasticSearch (1)
        • Lucene Solr (1)
      • PHP (2)
        • Laravel (1)
        • Codeigniter (1)
  • 블로그 메뉴

    • Github 방문
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    Langchain
    CDC
    LLM
    performance
    go
    티스토리챌린지
    Golang
    ollama
    docker
    MYSQL
    Kafka
    git
    python
    파이썬
    Redis
    백준
    prompt
    fluentd
    오블완
    http
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
상쾌한기분
[백준] 4963 파이썬(python)
상단으로

티스토리툴바