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?

ABC387をPythonで(A~D)

Posted at

AtCoder Beginner Contest 387(Promotion of AtCoderJobs)の解答等の速報的まとめ

A問題

問題文の通りに計算する

A
print(sum(map(int, input().split()))**2)

B問題

$1 \times 1$から$9 \times 9$まで全部調べる

B
x = int(input())

ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        if i * j != x:
            ans += i * j

print(ans)

C問題

calc(x)でx以下の蛇数をもとめられるようにしたらcalc(R) - calc(L - 1)で出来ると考えた。
ただ、calcがうまくできなかった。

D問題

bfsをもとに上下移動で来たかを判定できるようにする

D
from collections import deque

h, w = map(int, input().split())
s = [input() for _ in range(h)]

sx, sy, gx, gy = 0, 0, 0, 0
for i in range(h):
    for j in range(w):
        if s[i][j] == "S":
            sx, sy = i, j
        if s[i][j] == "G":
            gx, gy = i, j

INF = float("inf")
dist = [[[INF] * 2 for _ in range(w)] for _ in range(h)]
q = deque()
q.append((sx, sy, 0, -1))

while q:
    x, y, d_i, arr = q.popleft()

    if arr != 0:
        for i, j in [(x + 1, y), (x - 1, y)]:
            if 0 <= i < h and 0 <= j < w and s[i][j] != "#" and dist[i][j][0] > d_i + 1:
                dist[i][j][0] = d_i + 1
                q.append([i, j, d_i + 1, 0])
    if arr != 1:
        for i, j in [(x, y + 1), (x, y - 1)]:
            if 0 <= i < h and 0 <= j < w and s[i][j] != "#" and dist[i][j][1] > d_i + 1:
                dist[i][j][1] = d_i + 1
                q.append([i, j, d_i + 1, 1])

print(-1 if min(dist[gx][gy]) >= INF else min(dist[gx][gy]))
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?