-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathalphabet-cake.py
51 lines (47 loc) · 1.33 KB
/
alphabet-cake.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Copyright (c) 2017 kamyu. All rights reserved.
#
# Google Code Jam 2017 Round 1A - Problem A. Alphabet Cake
# https://code.google.com/codejam/contest/5304486/dashboard#s=p0
#
# Time: O(R * C)
# Space: O(1)
#
def paint_non_empty_row(cake, r, c):
for j in reversed(xrange(0, c)):
if cake[r][j] == '?':
cake[r][j] = cake[r][j+1]
else:
break
for j in xrange(c+1, len(cake[r])):
if cake[r][j] == '?':
cake[r][j] = cake[r][j-1]
else:
break
def paint_empty_row(cake, r, c):
for i in reversed(xrange(0, r)):
if cake[i][c] == '?':
cake[i] = cake[i+1]
else:
break
for i in xrange(r+1, len(cake)):
if cake[i][c] == '?':
cake[i] = cake[i-1]
else:
break
def alphabet_cake():
R, C = map(int, raw_input().strip().split())
cake, initials = [], []
for i in xrange(R):
cake.append(list(raw_input().strip()))
for j in xrange(C):
if cake[i][j] != '?':
initials.append((i, j))
for r, c in initials:
paint_non_empty_row(cake, r, c)
for r, c in initials:
paint_empty_row(cake, r, c)
return cake
for case in xrange(input()):
print 'Case #%d:' % (case+1)
for row in alphabet_cake():
print "".join(row)