Created
October 1, 2012 17:40
-
-
Save mrjohannchang/3813238 to your computer and use it in GitHub Desktop.
Codeforces 229A
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n, m = map(int, raw_input().split()) | |
table = [map(int, list(raw_input())) for _ in xrange(n)] | |
weighted_table = [] | |
for r in table: | |
if sum(r) == 0: | |
print -1 | |
exit() | |
weighted_row = [] | |
for i in xrange(len(r)): | |
if r[i] == 1: | |
weighted_row.append(0) | |
continue | |
ii = i | |
ii += len(r) | |
r *= 3 | |
a = [abs(j - i) for j in xrange(len(r)) if r[j] == 1] | |
b = [abs(j - i) for j in xrange(i, -1, -1) if r[j] == 1] | |
weight = None | |
if len(a) == 0: | |
weight = min(b) | |
elif len(b) == 0: | |
weight = min(a) | |
else: | |
weight = min(min(a), min(b)) | |
weighted_row.append(weight) | |
weighted_table.append(weighted_row) | |
mini = None | |
for c in xrange(len(weighted_table[0])): | |
if mini == None: | |
mini = sum(r[c] for r in weighted_table) | |
mini = min(mini, sum(r[c] for r in weighted_table)) | |
print mini |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment