-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheProb364.py
144 lines (106 loc) · 3.53 KB
/
eProb364.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
135
136
137
138
139
140
141
142
143
144
'''
Created on 2011-12-26
@author: Guigui
NOTE: THIS IS MESSED UP. DO NOT USE.
This solution I believe is technically correct (the best kind of correct).
However, it would take several years to compute the solution this way.
So yeah, this is not an efficient solution. Sorry!
'''
import time
# prob 364
def projfunc():
""" benchmarking
lastTime = 0
for i in range(2,14):
t1 = time.time()
getNumberOfWays(i)
t2 = time.time()
newTime = t2-t1
if lastTime != 0.0:
print(str(i) + ' ' + str(newTime))
lastTime = newTime
"""
rowWidth = 9
for i in range(0, rowWidth):
print('Col' + str(i) + ': ' + str(getNumberOfWays(rowWidth, i)))
return ""
def getNumberOfWays(seats, cond = -1):
row = []
for i in range(0,seats):
row.append(0)
totalNow = 0
for i in getValidSeats(row):
totalNow += getNumberOfSeatGrabs(i, row, 0, i, cond)
return totalNow
def getNumberOfSeatGrabs(startingPos, oldRow, totalYet, initialStart, ex = -1):
row = list(oldRow)
row[startingPos] = 1
totalNow = 0
if isRowFull(row) != True:
for i in getValidSeats(row):
totalNow += getNumberOfSeatGrabs(i, row, totalYet, initialStart, ex)
else:
if ex == -1:
return 1
# pos
else:
if initialStart == ex:
return 1
else:
return 0
return totalNow
'''
If there is any seat whose adjacent seat(s) are not occupied take such a seat.
If there is no such seat and there is any seat for which only one adjacent seat is occupied take such a seat.
Otherwise take one of the remaining available seats.
'''
def getValidSeats(row):
if isRowFull(row):
return []
listOfSeats = []
''' If there is any seat whose adjacent seat(s) are not occupied take such a seat.'''
for i in range(0,len(row)):
if row[i] == 0:
''' Obviously, taken seat is invalid '''
if i == 0:
'''Edge case left '''
if row[i + 1] == 0:
listOfSeats.append(i)
elif i == len(row) -1:
''' Edge case right '''
if row[i - 1] == 0:
listOfSeats.append(i)
else:
''' Not an edge case '''
if row[i - 1] == 0 and row[i + 1] == 0:
listOfSeats.append(i)
if len(listOfSeats) > 0:
return listOfSeats
''' If there is no such seat and there is any seat for which only one adjacent seat is occupied take such a seat. '''
for i in range(0,len(row)):
if row[i] == 0:
''' Obviously, taken seat is invalid '''
if i == 0:
'''Edge case left, left is always open '''
listOfSeats.append(i)
elif i == len(row) -1:
''' Edge case right, right is always open '''
listOfSeats.append(i)
else:
''' Not an edge case '''
if row[i - 1] == 0 or row[i + 1] == 0:
listOfSeats.append(i)
if len(listOfSeats) > 0:
return listOfSeats
for i in range(0,len(row)):
if row[i] == 0:
listOfSeats.append(i)
return listOfSeats
def isRowFull(row):
for i in row:
if i == 0:
return False
return True
if __name__ == '__main__':
print(projfunc())
print('')