0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AtCoder Beginner Contest 176をPythonで解く

0
Posted at

AtCoder Beginner Contest 176の問題をPythonで解いてみました。

A - Takoyaki

割り算の商をA // Bで、剰余をA % Bで計算します。
切り上げ処理を(n + x - 1) // xとすると簡潔になります。

main.py (A - Takoyaki)
n, x, t = map(int, input().split())
count = (n + x - 1) // x
print(count * t)

B - Multiple of 9

各文字をint型に変換し、合計を求めて$9$で割った剰余を調べます。

main.py (B - Multiple of 9)
if sum(map(int, input())) % 9 == 0:
    print("Yes")
else:
    print("No")

Python 3のint型には上限がありませんので、文字列全体をint型に変換してもOKです。

main.py (B - Multiple of 9)
if int(input()) % 9 == 0:
    print("Yes")
else:
    print("No")

C - Sum of product of pairs

前の人の高さ(踏み台含)より自分の身長が低いか否かを判断し、低ければ必要な高さの踏み台を用意していきます。

main.py (C - Sum of product of pairs)
# 入力
n = int(input())
a = list(map(int, input().split()))

# 直前のインデックスで必要だった踏み台の高さ
prev_step_height = 0
# 全体で追加した踏み台の合計高さ
total_step_height = 0

for i in range(1, n):
    # 現在位置で必要な追加高さを計算
    required = a[i - 1] + prev_step_height - a[i]
    if required > 0:
        # 必要な高さの踏み台を追加し、その高さを次に引き継ぐ
        prev_step_height = required
        total_step_height += required
    else:
        # 必要がなければstepは0にリセット
        prev_step_height = 0

# 出力
print(total_step_height)

D - Wizard in Maze

幅優先探索(breadth first search; BFS)と呼ばれる方法を用います。

次を繰り返すことで、移動できるマス全てにおける移動「コスト」(移動Bをしなければならない回数)が分かります。

  1. スタート地点から移動Aで移動できるマスを調べ上げる。
  2. 1.のマスから移動Bで移動できるマスを調べ上げる。
  3. 2.のマスから移動Aで移動できるマスを調べ上げる。
  4. 3.のマスから移動Bで移動できるマスを調べ上げる。
main.py (D - Wizard in Maze)
from collections import deque

h, w = map(int, input().split())
c_h, c_w = map(int, input().split())
d_h, d_w = map(int, input().split())

# 迷路情報の入力(今後、これに移動"コスト"が入力されていく)
# 周囲に2マス分の壁を追加
maze = (
    [["#"] * (w + 4) for _ in range(2)]
    + [list("##" + input().rstrip() + "##") for _ in range(h)]
    + [["#"] * (w + 4) for _ in range(2)]
)

# 迷路情報に合わせてCとDの値を調整
# 2マス分の壁が追加されているが、迷路情報のIndexは0からなので1だけ足す
c_h += 1
c_w += 1
d_h += 1
d_w += 1

# スタート地点はコスト0
maze[c_h][c_w] = 0

# 移動先の地点を調査するべき地点リスト
# まずはじめにスタート地点から移動Aを調べる
dq_A = deque([(c_h, c_w)])
dq_B = deque()

# 移動Aと移動Bで移動できる先
delta_A = ((0, 1), (0, -1), (1, 0), (-1, 0))
delta_B = tuple(
    (dh, dw)
    for dh in range(-2, 3)
    for dw in range(-2, 3)
    if (abs(dh) + abs(dw)) > 1
)

# 移動先の地点を調査するべき地点リストがなくなるまでループ
while dq_A or dq_B:
    while dq_A:
        # 移動元=current、座標とコストを確認
        current_h, current_w = dq_A.popleft()
        current_cost = maze[current_h][current_w]

        # この後移動Bに関しても調べる
        dq_B.append((current_h, current_w))

        # 移動Aを全て試す
        for dh, dw in delta_A:
            next_h = current_h + dh
            next_w = current_w + dw
            # 移動先が移動可能かどうか
            if maze[next_h][next_w] == ".":
                # コストは追加ではかからない
                maze[next_h][next_w] = current_cost
                # 再度移動Aを試す(タプルで保持)
                dq_A.append((next_h, next_w))

    while dq_B:
        # 移動元=current、座標とコストを確認
        current_h, current_w = dq_B.popleft()
        current_cost = maze[current_h][current_w]

        # 移動Bを全て試す
        for dh, dw in delta_B:
            next_h = current_h + dh
            next_w = current_w + dw
            # 移動先が移動可能かどうか
            if maze[next_h][next_w] == ".":
                # コストは追加でかかる
                maze[next_h][next_w] = current_cost + 1
                # 再度移動Aを試す
                dq_A.append((next_h, next_w))

# この時点でコストが入力されていないなら、それは移動不可能ということ
if maze[d_h][d_w] == ".":
    print(-1)
# そうでなければ、移動コストを出力
else:
    print(maze[d_h][d_w])

参考にしたサイト等

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?