Skip to content

Instantly share code, notes, and snippets.

@cocodrips
Created July 20, 2014 11:46
Show Gist options
  • Save cocodrips/3154a84ade57b5da5752 to your computer and use it in GitHub Desktop.
Save cocodrips/3154a84ade57b5da5752 to your computer and use it in GitHub Desktop.
二次元累積和を使う
# board[n][n] があるとする
table = [[0 for _ in xrange(N)] for _ in xrange(N)]
for c in xrange(N):
for r in xrange(N):
total = 0
if c != 0:
total += table[c - 1][r]
if r != 0:
total += table[c][r - 1]
if c != 0 and r != 0:
total -= table[c - 1][r - 1]
table[c][r] = total + board[c][r]
maximum = 0
for t in xrange(N):
for b in xrange(t + 1):
for r in xrange(N):
for l in xrange(r + 1):
cost = table[t][r]
if b != 0:
cost -= table[b - 1][r]
if l != 0:
cost -= table[t][l - 1]
if b != 0 and l != 0:
cost += table[b - 1][l - 1]
maximum = max(maximum, cost)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment