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]))