๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐‘†๐‘ข๐‘›๐‘ โ„Ž๐‘–๐‘›๐‘’ ๐‘Ž๐‘“๐‘ก๐‘’๐‘Ÿ ๐‘Ÿ๐‘Ž๐‘–๐‘›โœง

[Python] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ˆซ์ž์˜ ํ‘œํ˜„ & ๋ฐฑ์ค€ 6593 ์ƒ๋ฒ”๋นŒ๋”ฉ ๋ณธ๋ฌธ

๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ด๐Ÿ’ป/[๐๐ฒ๐ญ๐ก๐จ๐ง] ๐€๐ฅ๐ ๐จ๐ซ๐ข๐ญ๐ก๐ฆ

[Python] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ˆซ์ž์˜ ํ‘œํ˜„ & ๋ฐฑ์ค€ 6593 ์ƒ๋ฒ”๋นŒ๋”ฉ

๐ŸคRyusun๐Ÿค 2023. 9. 18. 15:42

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ˆซ์ž์˜ ํ‘œํ˜„

# ํˆฌํฌ์ธํŠธ ์•Œ๊ณ ๋ฆฌ์ฆ˜

def solution(n):
    n_list = []
    for i in range(1, n+1):
        n_list.append(i)

    cnt = 0
    start, end = 0, 0
    n_sum = n_list[0]
    while True:
        if n_sum < n:
            end += 1
            if end >= n:
                break
            n_sum += n_list[end]
        elif n_sum == n:
            cnt+= 1
            n_sum -= n_list[start]
            start += 1
        else:
            n_sum -= n_list[start]
            start += 1

    return cnt

print(solution(15))

# ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด
def solution2(num):
    answer = 0
    for i in range(1, num+1):
        n_sum = 0
        while n_sum < num:
            n_sum += i
            i += 1
        if n_sum == num:
            answer += 1
    return answer

print(solution2(15))

 

๋ฐฑ์ค€ 6593 ์ƒ๋ฒ”๋นŒ๋”ฉ 

# ๋ฐฑ์ค€ 6593 ์ƒ๋ฒ”๋นŒ๋”ฉ

from collections import deque
import sys
q = deque()

input = sys.stdin.readline
dx = [1, -1, 0, 0, 0, 0]
dy = [0, 0, 1, -1, 0, 0]
dz = [0, 0, 0, 0, 1, -1]

def bfs(z, x, y):
    q.append([z, x, y])
    visited[z][x][y] = 1
    while q:
        z, x, y = q.popleft()
        for i in range(6):
            nz = z + dz[i]
            nx = x + dx[i]
            ny = y + dy[i]
            if 0 <= nz < l and 0 <= nx < r and 0 <= ny < c:
                if graph[nz][nx][ny] == 'E':
                    print("Escaped in {0} minute(s).".format(visited[z][x][y]))
                    return
                elif graph[nz][nx][ny] == '.' and visited[nz][nx][ny] == 0:
                    visited[nz][nx][ny] = visited[z][x][y] + 1
                    q.append([nz, nx, ny])

    print("Trapped!")


while True:
    l, r, c = map(int, input().split())
    if l == r == c == 0:
        break
    graph = []
    visited = [[[0] * c for _ in range(r)] for _ in range(l)]
    for _ in range(l):
        graph.append([list(input().strip()) for _ in range(r)])
        temp = input()

    for z in range(l):
        for i in range(r):
            for j in range(c):
                if graph[z][i][j] == 'S':
                    bfs(z, i, j)

3์ฐจ์› ๋ฌธ์ œ๋ฅผ ์‹œ๋„ํ•ด ๋ณผ ์ˆ˜ ์žˆ์—ˆ๋˜ ์ข‹์€ ๋ฌธ์ œ์˜€๋‹ค.