Problem:


問題 B  蝉

問題文

N 君は毎日学校に通っているが，その道のりはとても長い．さらに今は暑い夏である．道中，蝉が五月蝿い．出来れば避けたいものである．ところで情報化が発達した現代社会においては，家に居ながらにしてどこに何匹の蝉がいるか調べることが出来る．その情報を使い，途中で出会う蝉の数が最も少ない道で学校に行くことに決めた．

家と学校が描かれた地図は長方形状になっており，H × W 個のブロックに分割される．一番左上のブロックに N 君の家があり，一番右下に学校がある．彼は学校へまっすぐ登校したいので家の方に逆戻りすることはない (地図において，右か下のブロックにしか移動しない)．各ブロックにおいて，そこにいる蝉の数だけ不快になる．どのような道を選べば，不快な度合いを最小化，つまり出会う蝉の数を最小に出来るだろうか．

入力形式

最初の行には二つの数字 H，W がこの順で与えられる．H は地図の縦方向のブロック数を示し，W は横方向のブロック数を示す．

次の H 行に蝉の情報が書かれた地図が与えられる．各行は W 文字からなり，各文字は対応するブロック中の蝉の数を示す．ブロック中の蝉の数は 9 を超えることはない．

出力形式

出会う蝉の数の最小値を 1 行で与えよ．

制約

 2 ≤ H ≤ 50 

 2 ≤ W ≤ 50 

N 君の家と学校には蝉はいない．

ブロック中の蝉の数は 0 以上 9 以下である．

入出力例

入力例 1

3 3
023
321
120

出力例 1

5

入力例 2

5 6
000000
923450
000000
054329
000000

出力例 2

4

N 君は右か下のブロックにしか移動しないことに注意せよ．

入力例 3

6 9
089712364
781263576
937526351
736589623
217835653
837479570

出力例 3

46



Sample input:
3 3
023
321
120


Sample output:
5

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
INF = 10 ** 20
dp = [[INF] * (w + 1)] + [[INF] + [0] * w for _ in range(h)]
dp[0][1] = 0
for y in range(1, h + 1):
  line = list(map(int, list(input())))
  for x in range(1, w + 1):
    dp[y][x] = min(dp[y - 1][x], dp[y][x - 1]) + line[x - 1]
print(dp[h][w])

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
m = [list(map(int, list(input()))) for _ in range(H)]

dp = [[-1 for j in range(W)] for i in range(H)]

for i in range(H):
    for j in range(W):
        if i == 0 and j == 0:
            dp[i][j] = 0
        elif i == 0:
            dp[i][j] = dp[i][j-1] + m[i][j]
        elif j == 0:
            dp[i][j] = dp[i-1][j] + m[i][j]
        else:
            dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + m[i][j]

print(dp[H-1][W-1])

Submission status: Accepted

Solution code:
M_lis = []  
H,W = map(int,input().split())
DP = [[0 for j in range(W)] for i in range(H)]
for i in range(H):
    M_lis.append(list(map(int,list(str(input())))))
for j in range(1,W):
    DP[0][j] += M_lis[0][j] + DP[0][j - 1]
for k in range(1,H):
    DP[k][0] += M_lis[k][0] + DP[k - 1][0]
for i in range(1,H):
    for j in range(1,W):
        DP[i][j] = min(DP[i - 1][j],DP[i][j - 1]) + M_lis[i][j]

print(DP[H - 1][W - 1])

Submission status: Accepted

Solution code:
[H,W] = list(map(int,input().split()))
ls = []
for i in range(H):
  ls.append(input())
dp = [[0 for i in range(W)] for j in range(H)]
for i in range(H):
  for j in range(W):
    if i == 0 and j == 0:
      dp[i][j] = 0
    elif i == 0:
      dp[i][j] = dp[i][j-1] + int(ls[i][j])
    elif j == 0:
      dp[i][j] = dp[i-1][j] + int(ls[i][j])
    else:
      dp[i][j] = min(dp[i-1][j],dp[i][j-1]) + int(ls[i][j])
print(dp[H-1][W-1])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2272

"""
import sys
from sys import stdin
input = stdin.readline


def main(args):
    H, W = map(int, input().split(' '))
    data = []
    for i in range(H):
        data.append([int(x) for x in list(input().strip('\n'))])


    dp = [[0] * W for _ in range(H)]

    for i in range(1, H):
        dp[i][0] = dp[i-1][0] + data[i][0]
    for j in range(1, W):
        dp[0][j] = dp[0][j-1] + data[0][j]

    for i in range(1, H):
        for j in range(1, W):
            dp[i][j] = data[i][j] + min(dp[i-1][j], dp[i][j-1])

    print(dp[-1][-1])




if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
def solve():
    import sys
    file_input = sys.stdin
    H, W = map(int, file_input.readline().split())
    
    from itertools import accumulate
    from operator import add
    
    first_line = file_input.readline().rstrip()
    memo_1 = accumulate(map(int, first_line), add)
    
    for line in file_input:
        line = map(int, line.rstrip())
        ans = next(line) + next(memo_1)
        memo_2 = [ans]
        for n, m in zip(line, memo_1):
            ans = min(ans + n, m + n)
            memo_2.append(ans)
        memo_1 = iter(memo_2)
    print(ans)

solve()

Submission status: Accepted

Solution code:
INF = 1 << 30
H, W = map(int, raw_input().split())
G = [map(int, list(raw_input())) for _ in xrange(H)]
dp = [[0] * W + [INF] for _ in xrange(H)] + [[0] + [INF] * (W)]
for y in xrange(H):
    for x in xrange(W):
            dp[y][x] = min(dp[y-1][x], dp[y][x-1]) + G[y][x]
print dp[H-1][W-1]

Submission status: Accepted

Solution code:
INF = 1 << 30
H, W = map(int, raw_input().split())
G = [map(int, list(raw_input())) for _ in xrange(H)]
dp = [[0] * W + [INF] for _ in xrange(H)] + [[0] + [INF] * W]
for y in xrange(H):
    for x in xrange(W):
            dp[y][x] = min(dp[y-1][x], dp[y][x-1]) + G[y][x]
print dp[H-1][W-1]

Submission status: Accepted

Solution code:
H,W = map(int,raw_input().split())
M = [map(int,list(raw_input())) for i in range(H)]
for h in range(H):
	for w in range(W):
		if 0 < w  and 0 < h:
			M[h][w] += min(M[h-1][w],M[h][w-1])
		elif w == 0 and 0 < h:
			M[h][w] += M[h-1][w]
		elif 0 < w and h == 0:
			M[h][w] += M[h][w-1]
print M[H-1][W-1]

Submission status: Accepted

Solution code:
H,W = map(int,raw_input().split())
M = [map(int,list(raw_input())) for i in range(H)]
M.insert(0,[0]+[500]*(W-1))
for h in range(1,H+1):
	M[h][0] += M[h-1][0]
	for w in range(1,W):
		M[h][w] += min(M[h-1][w],M[h][w-1])
print M[H][W-1]

Submission status: Accepted

Solution code:
H,W = map(int,raw_input().split())
M = [[500]*(W+1)]+[[500]+map(int,list(raw_input())) for i in range(H)]
M[0][1] = 0
for h in range(1,H+1):
	for w in range(1,W+1):
		M[h][w] += min(M[h-1][w],M[h][w-1])
print M[H][W]

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
dp=[[float("inf") for i in range(w+1)] for j in range(h+1)]
dp[0][1]=0
town=[["0"]*(w+1)]
for i in range(h):
    town.append(list("0"+input()))
for i in range(1,w+1):
    for j in range(1,h+1):
        dp[j][i]=min(dp[j-1][i],dp[j][i-1])+int(town[j][i])
print(dp[-1][-1])