-
Notifications
You must be signed in to change notification settings - Fork 15
/
pylons.py
134 lines (115 loc) · 4.36 KB
/
pylons.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Copyright (c) 2019 kamyu. All rights reserved.
#
# Google Code Jam 2019 Round 1A - Problem A. Pylons
# https://codingcompetitions.withgoogle.com/codejam/round/0000000000051635/0000000000104e03
#
# Time: O(R * C)
# Space: O(1)
#
def begin_at_i_seq(C, i):
for c in xrange(i, C+1):
yield c
for c in xrange(1, i):
yield c
def pylons2():
R, C = map(int, raw_input().strip().split())
swapped = False
if R > C:
R, C = C, R
swapped = True
result = []
r = 0
if C >= 4:
while (R-r) >= 3 and (R-r) != 4: # case 3 rows
iter3 = begin_at_i_seq(C, 1)
iter2 = begin_at_i_seq(C, 3)
iter1 = begin_at_i_seq(C, 1)
for c in xrange(C):
result.append((r+3, next(iter3)))
result.append((r+2, next(iter2)))
result.append((r+1, next(iter1)))
r += 3
if (R-r) == 4: # case 4 rows
iter4 = begin_at_i_seq(C, 3)
iter3 = begin_at_i_seq(C, 1)
iter2 = begin_at_i_seq(C, 3)
iter1 = begin_at_i_seq(C, 1)
for c in xrange(C):
result.append((r+4, next(iter4)))
result.append((r+3, next(iter3)))
result.append((r+2, next(iter2)))
result.append((r+1, next(iter1)))
if len(result) >= 5 and abs(result[-5][0]-result[-4][0]) == abs(result[-5][1]-result[-4][1]):
result[-4], result[-2] = result[-2], result[-4]
r += 4
elif (R-r) == 2 and C >= 5: # case 2 rows
iter2 = begin_at_i_seq(C, 3)
iter1 = begin_at_i_seq(C, 1)
for _ in xrange(C):
result.append((r+2, next(iter2)))
result.append((r+1, next(iter1)))
r += 2
if r != R:
return "IMPOSSIBLE"
if swapped:
swapped = False
for i in xrange(len(result)):
result[i] = (result[i][1], result[i][0])
R, C = C, R
assert(R*C == len(result))
for i in xrange(1, len(result)):
assert((abs(result[i][0]-result[i-1][0]) != abs(result[i][1]-result[i-1][1]) and
result[i][0]-result[i-1][0] != 0 and result[i][1]-result[i-1][1] != 0))
return "POSSIBLE\n{}".format("\n".join(map(lambda x: " ".join(map(str, x)), result)))
def pylons():
R, C = map(int, raw_input().strip().split())
swapped = False
if R > C:
R, C = C, R
swapped = True
result = []
r = 0
if C >= 4:
while (R-r) >= 4 and (R-r) != 5: # case 4 rows
iter4 = begin_at_i_seq(C, 3)
iter3 = begin_at_i_seq(C, 1)
iter2 = begin_at_i_seq(C, 3)
iter1 = begin_at_i_seq(C, 1)
for c in xrange(C):
result.append((r+4, next(iter4)))
result.append((r+3, next(iter3)))
result.append((r+2, next(iter2)))
result.append((r+1, next(iter1)))
if len(result) >= 5 and abs(result[-5][0]-result[-4][0]) == abs(result[-5][1]-result[-4][1]):
result[-4], result[-2] = result[-2], result[-4]
r += 4
while (R-r) >= 3: # case 3 rows
iter3 = begin_at_i_seq(C, 1)
iter2 = begin_at_i_seq(C, 3)
iter1 = begin_at_i_seq(C, 1)
for c in xrange(C):
result.append((r+3, next(iter3)))
result.append((r+2, next(iter2)))
result.append((r+1, next(iter1)))
r += 3
if (R-r) == 2 and C >= 5: # case 2 rows
iter2 = begin_at_i_seq(C, 3)
iter1 = begin_at_i_seq(C, 1)
for _ in xrange(C):
result.append((r+2, next(iter2)))
result.append((r+1, next(iter1)))
r += 2
if r != R:
return "IMPOSSIBLE"
if swapped:
swapped = False
for i in xrange(len(result)):
result[i] = (result[i][1], result[i][0])
R, C = C, R
assert(R*C == len(result))
for i in xrange(1, len(result)):
assert((abs(result[i][0]-result[i-1][0]) != abs(result[i][1]-result[i-1][1]) and
result[i][0]-result[i-1][0] != 0 and result[i][1]-result[i-1][1] != 0))
return "POSSIBLE\n{}".format("\n".join(map(lambda x: " ".join(map(str, x)), result)))
for case in xrange(input()):
print 'Case #%d: %s' % (case+1, pylons())