-
Notifications
You must be signed in to change notification settings - Fork 3
/
q1568.py
101 lines (90 loc) · 3.17 KB
/
q1568.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
#!/usr/bin/python3
from typing import List
from copy import deepcopy
class Solution:
def fillGridWithNumber(self, grid: List[List[int]], x: int, y: int, maxX: int, maxY: int):
if x < 0 or y < 0 or x >= maxX or y >= maxY or grid[x][y] == 0:
return
grid[x][y] = 0
self.fillGridWithNumber(grid, x + 1, y, maxX, maxY)
self.fillGridWithNumber(grid, x - 1, y, maxX, maxY)
self.fillGridWithNumber(grid, x, y + 1, maxX, maxY)
self.fillGridWithNumber(grid, x, y - 1, maxX, maxY)
def checkGrid(self, grid: List[List[int]]) -> bool:
tempGrid = deepcopy(grid)
maxX = len(tempGrid)
maxY = len(tempGrid[0])
foundIsland = False
for x in range(maxX):
for y in range(maxY):
if tempGrid[x][y] > 0:
self.fillGridWithNumber(tempGrid, x, y, maxX, maxY)
foundIsland = True
break
if foundIsland:
break
for x in range(maxX):
for y in range(maxY):
if tempGrid[x][y] > 0:
return True
return not foundIsland
def tryMapRow(self, grid: List[List[int]], number: int, lastX:int) -> bool:
if number == 0:
return self.checkGrid(grid)
maxX = len(grid)
maxY = len(grid[0])
for x in range(lastX, maxX):
for y in range(maxY):
if grid[x][y] > 0:
grid[x][y] = 0
if self.tryMapRow(grid, number - 1, x + 1):
return True
grid[x][y] = 1
return False
def tryMapCol(self, grid: List[List[int]], number: int, lastY:int) -> bool:
if number == 0:
return self.checkGrid(grid)
maxX = len(grid)
maxY = len(grid[0])
for y in range(lastY, maxY):
for x in range(maxX):
if grid[x][y] > 0:
grid[x][y] = 0
if self.tryMapRow(grid, number - 1, maxY + 1):
return True
grid[x][y] = 1
return False
def checkSpecialTwo(self, grid:[List]) -> bool:
maxX = len(grid)
maxY = len(grid[0])
count = 0
indexX = 0
indexY = 0
for i in range(maxX):
for j in range(maxY):
if grid[i][j] > 0:
count += 1
if count > 2:
return False
indexX = i
indexY = j
if count == 2:
if indexX > 0 and grid[indexX - 1][indexY] > 0:
return True
if indexY > 0 and grid[indexX][indexY - 1] > 0:
return True
return False
def minDays(self, grid: List[List[int]]) -> int:
result = 0
if self.checkSpecialTwo(grid):
return 2
while True:
if self.tryMapRow(grid, result, 0):
return result
if result > 1 and self.tryMapCol(grid, result, 0):
return result
result += 1
return -1
grid = [[1,1]]
result = Solution().minDays(grid)
print(result)