Skip to content

Instantly share code, notes, and snippets.

@todoa2c
Created April 28, 2014 09:13
Show Gist options
  • Save todoa2c/11366323 to your computer and use it in GitHub Desktop.
Save todoa2c/11366323 to your computer and use it in GitHub Desktop.
Paiza vol.2 (他の方のコードを読んでPythonで書いた)
import sys
if sys.version_info[0] < 3:
input = raw_input
h, w = [int(e) for e in input().split()]
home = []
for _ in range(h):
home.append([0 if c == '1' else 1 for c in input().rstrip()])
table = [[[c] + [0] * (h - 1) for c in line] for line in home]
accum = [[0] * (w + 1) for _ in range(h)]
for i in range(h):
accum[0][table[i][0][0]] += 1
for j in range(1, w):
if table[i][j][0] > 0:
table[i][j][0] += table[i][j - 1][0]
accum[0][table[i][j][0]] += 1
for i in range(1, h):
for j in range(w):
for k in range(1, h):
table[i][j][k] = min(table[i-1][j][k-1], table[i][j][k - 1])
accum[k][table[i][j][k]] += 1
for i in range(h):
for j in reversed(range(w)):
accum[i][j] += accum[i][j + 1]
for _ in range(int(input())):
height, width = [int(e) for e in input().split()]
if height <= h and width <= w:
print(accum[height - 1][width])
else:
print(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment