Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 327bf56

Browse files
authored
feat: add python solution to lc problem: No.1728 (#1848)
No.1728.Cat and Mouse II
1 parent cf7308b commit 327bf56

File tree

3 files changed

+210
-0
lines changed

3 files changed

+210
-0
lines changed

‎solution/1700-1799/1728.Cat and Mouse II/README.md‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,76 @@
105105
<!-- 这里可写当前语言的特殊实现逻辑 -->
106106

107107
```python
108+
class Solution:
109+
def canMouseWin(self, grid: List[str], catJump: int, mouseJump: int) -> bool:
110+
dirs = [0, 1, 0, -1, 0]
111+
m = len(grid)
112+
n = len(grid[0])
113+
nFloors = 0
114+
cat = 0 # cat's position
115+
mouse = 0 # mouse's position
116+
117+
def hash(i: int, j: int) -> int:
118+
return i * n + j
119+
120+
for i in range(m):
121+
for j in range(n):
122+
if grid[i][j] != "#":
123+
nFloors += 1
124+
if grid[i][j] == "C":
125+
cat = hash(i, j)
126+
elif grid[i][j] == "M":
127+
mouse = hash(i, j)
128+
129+
# dp(i, j, k) := True if mouse can win w//
130+
# Cat on (i // 8, i % 8), mouse on (j // 8, j % 8), and turns = k
131+
@functools.lru_cache(None)
132+
def dp(cat: int, mouse: int, turn: int) -> bool:
133+
# We already search whole touchable grid
134+
if turn == nFloors * 2:
135+
return False
136+
137+
if turn % 2 == 0:
138+
# mouse's turn
139+
i = mouse // n
140+
j = mouse % n
141+
for k in range(4):
142+
for jump in range(mouseJump + 1):
143+
x = i + dirs[k] * jump
144+
y = j + dirs[k + 1] * jump
145+
if x < 0 or x == m or y < 0 or y == n:
146+
break
147+
if grid[x][y] == "#":
148+
break
149+
if grid[x][y] == "F": # Mouse eats the food, so mouse win
150+
return True
151+
if dp(cat, hash(x, y), turn + 1):
152+
return True
153+
# Mouse can't win, so mouse lose
154+
return False
155+
else:
156+
# cat's turn
157+
i = cat // n
158+
j = cat % n
159+
for k in range(4):
160+
for jump in range(catJump + 1):
161+
x = i + dirs[k] * jump
162+
y = j + dirs[k + 1] * jump
163+
if x < 0 or x == m or y < 0 or y == n:
164+
break
165+
if grid[x][y] == "#":
166+
break
167+
if grid[x][y] == "F": # Cat eats the food, so mouse lose
168+
return False
169+
nextCat = hash(x, y)
170+
if nextCat == mouse: # Cat catches mouse, so mouse lose
171+
return False
172+
if not dp(nextCat, mouse, turn + 1):
173+
return False
174+
# Cat can't win, so mouse win
175+
return True
176+
177+
return dp(cat, mouse, 0)
108178

109179
```
110180

‎solution/1700-1799/1728.Cat and Mouse II/README_EN.md‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,76 @@
7979
### **Python3**
8080

8181
```python
82+
class Solution:
83+
def canMouseWin(self, grid: List[str], catJump: int, mouseJump: int) -> bool:
84+
dirs = [0, 1, 0, -1, 0]
85+
m = len(grid)
86+
n = len(grid[0])
87+
nFloors = 0
88+
cat = 0 # cat's position
89+
mouse = 0 # mouse's position
90+
91+
def hash(i: int, j: int) -> int:
92+
return i * n + j
93+
94+
for i in range(m):
95+
for j in range(n):
96+
if grid[i][j] != "#":
97+
nFloors += 1
98+
if grid[i][j] == "C":
99+
cat = hash(i, j)
100+
elif grid[i][j] == "M":
101+
mouse = hash(i, j)
102+
103+
# dp(i, j, k) := True if mouse can win w//
104+
# Cat on (i // 8, i % 8), mouse on (j // 8, j % 8), and turns = k
105+
@functools.lru_cache(None)
106+
def dp(cat: int, mouse: int, turn: int) -> bool:
107+
# We already search whole touchable grid
108+
if turn == nFloors * 2:
109+
return False
110+
111+
if turn % 2 == 0:
112+
# mouse's turn
113+
i = mouse // n
114+
j = mouse % n
115+
for k in range(4):
116+
for jump in range(mouseJump + 1):
117+
x = i + dirs[k] * jump
118+
y = j + dirs[k + 1] * jump
119+
if x < 0 or x == m or y < 0 or y == n:
120+
break
121+
if grid[x][y] == "#":
122+
break
123+
if grid[x][y] == "F": # Mouse eats the food, so mouse win
124+
return True
125+
if dp(cat, hash(x, y), turn + 1):
126+
return True
127+
# Mouse can't win, so mouse lose
128+
return False
129+
else:
130+
# cat's turn
131+
i = cat // n
132+
j = cat % n
133+
for k in range(4):
134+
for jump in range(catJump + 1):
135+
x = i + dirs[k] * jump
136+
y = j + dirs[k + 1] * jump
137+
if x < 0 or x == m or y < 0 or y == n:
138+
break
139+
if grid[x][y] == "#":
140+
break
141+
if grid[x][y] == "F": # Cat eats the food, so mouse lose
142+
return False
143+
nextCat = hash(x, y)
144+
if nextCat == mouse: # Cat catches mouse, so mouse lose
145+
return False
146+
if not dp(nextCat, mouse, turn + 1):
147+
return False
148+
# Cat can't win, so mouse win
149+
return True
150+
151+
return dp(cat, mouse, 0)
82152

83153
```
84154

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
class Solution:
2+
def canMouseWin(self, grid: List[str], catJump: int, mouseJump: int) -> bool:
3+
dirs = [0, 1, 0, -1, 0]
4+
m = len(grid)
5+
n = len(grid[0])
6+
nFloors = 0
7+
cat = 0 # cat's position
8+
mouse = 0 # mouse's position
9+
10+
def hash(i: int, j: int) -> int:
11+
return i * n + j
12+
13+
for i in range(m):
14+
for j in range(n):
15+
if grid[i][j] != "#":
16+
nFloors += 1
17+
if grid[i][j] == "C":
18+
cat = hash(i, j)
19+
elif grid[i][j] == "M":
20+
mouse = hash(i, j)
21+
22+
# dp(i, j, k) := True if mouse can win w//
23+
# Cat on (i // 8, i % 8), mouse on (j // 8, j % 8), and turns = k
24+
@functools.lru_cache(None)
25+
def dp(cat: int, mouse: int, turn: int) -> bool:
26+
# We already search whole touchable grid
27+
if turn == nFloors * 2:
28+
return False
29+
30+
if turn % 2 == 0:
31+
# mouse's turn
32+
i = mouse // n
33+
j = mouse % n
34+
for k in range(4):
35+
for jump in range(mouseJump + 1):
36+
x = i + dirs[k] * jump
37+
y = j + dirs[k + 1] * jump
38+
if x < 0 or x == m or y < 0 or y == n:
39+
break
40+
if grid[x][y] == "#":
41+
break
42+
if grid[x][y] == "F": # Mouse eats the food, so mouse win
43+
return True
44+
if dp(cat, hash(x, y), turn + 1):
45+
return True
46+
# Mouse can't win, so mouse lose
47+
return False
48+
else:
49+
# cat's turn
50+
i = cat // n
51+
j = cat % n
52+
for k in range(4):
53+
for jump in range(catJump + 1):
54+
x = i + dirs[k] * jump
55+
y = j + dirs[k + 1] * jump
56+
if x < 0 or x == m or y < 0 or y == n:
57+
break
58+
if grid[x][y] == "#":
59+
break
60+
if grid[x][y] == "F": # Cat eats the food, so mouse lose
61+
return False
62+
nextCat = hash(x, y)
63+
if nextCat == mouse: # Cat catches mouse, so mouse lose
64+
return False
65+
if not dp(nextCat, mouse, turn + 1):
66+
return False
67+
# Cat can't win, so mouse win
68+
return True
69+
70+
return dp(cat, mouse, 0)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /