The first time I couldn't solve the C problem with ABC …….
Break through in 1 minute. Just write.
a, b = map(int, input().split())
c, d = map(int, input().split())
print(a * d - b * c)
Break through in 3 minutes. Just write.
N, X = map(int, input().split())
S = input()
result = X
for c in S:
if c == 'o':
result += 1
elif c == 'x':
if result != 0:
result -= 1
print(result)
I couldn't break through. My head turned white.
I couldn't break through. Even in the input / output example, TLE and NaN did not go well.
I started after giving up C and D, so I don't know how many minutes it took. It's certainly less than 56 minutes. There was nothing that seemed difficult to see, and I implemented it thinking that there was some trap, but nothing trapped. There wasn't (laughs). It seemed dangerous to try warp repeatedly, so I tried only once, but this is too visible and not a trap (laughs).
from collections import deque
INF = 10 ** 9
H, W = map(int, input().split())
a = [input() for _ in range(H)]
d = {}
for h in range(H):
for w in range(W):
c = a[h][w]
if c in 'SG':
d[c] = (h, w)
elif c in '.#':
continue
else:
if c in d:
d[c].append((h, w))
else:
d[c] = [(h, w)]
not_warped = {}
for c in 'abcdefghijklmnopqrstuvwxyz':
not_warped[c] = True
def move(h, w, p):
c = a[h][w]
if c == '#':
return
if t[h][w] > p:
t[h][w] = p
q.append((h, w))
t = [[INF] * W for _ in range(H)]
h, w = d['S']
t[h][w] = 0
q = deque([(h, w)])
while q:
h, w = q.popleft()
c = a[h][w]
p = t[h][w] + 1
if 'a' <= c <= 'z' and not_warped[c]:
for nh, nw in d[c]:
if t[nh][nw] > p:
t[nh][nw] = p
q.append((nh, nw))
not_warped[c] = False
if h != 0:
move(h - 1, w, p)
if h != H - 1:
move(h + 1, w, p)
if w != 0:
move(h, w - 1, p)
if w != W - 1:
move(h, w + 1, p)
h, w = d['G']
if t[h][w] == INF:
print(-1)
else:
print(t[h][w])
Recommended Posts