[백준] 2667, 2869 - Python3

shsh·2021년 10월 24일
0

백준

목록 보기
22/45

2869. 달팽이는 올라가고 싶다

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

내 풀이 - 성공

from sys import stdin
import math

A, B, V = map(int, stdin.readline().split())

if A == V:
    print(1)
else:
    V -= A
    ans = 1
    day = 0
    ans += math.ceil(V / (A-B))
    print(ans)

A == V 일 때는 하루면 되니까 1 print

나머지는 A 가 B 보다 크니까 마지막 하루는 A 미터 올라가도록 함 => V -= A & ans = 1
남은 날들은 낮 - 밤 값을 V 로 나누고 올림한 값이므로 모두 더해서 print


2667. 단지번호붙이기

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

내 풀이 - 성공

from sys import stdin

N = int(stdin.readline())

mapp = []
for _ in range(N):
    p = list(map(int, list(stdin.readline().strip())))
    mapp.append(p)

def func(i, j):
    mapp[i][j] = 0

    cnt = 0
    if i > 0 and mapp[i-1][j]:
        cnt += func(i-1, j)
    if i < N-1 and mapp[i+1][j]:
        cnt += func(i+1, j)
    if j > 0 and mapp[i][j-1]:
        cnt += func(i, j-1)
    if j < N-1 and mapp[i][j+1]:
        cnt += func(i, j+1)
    return cnt+1

ans = []
for i in range(N):
    for j in range(N):
        if mapp[i][j]:
            c = func(i, j)
            ans.append(c)

print(len(ans))
ans.sort()
for a in ans:
    print(a)

지도를 훑어보면서 집이 있으면 그 집과 연결된 이웃집들의 개수를 세서 ans 에 append

한번 본 집들은 mapp[i][j] = 0 으로 update 하고 cnt 로 개수 count 해서 return

총 단지수인 ans 의 길이 출력하고
각 단지내 집의 수는 오름차순으로 정렬한 후 출력

파이썬 지역변수의 범위 참고
https://dojang.io/mod/page/view.php?id=2365

profile
Hello, World!

0개의 댓글