Skip to content

Solution for 1728. #1848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions solution/1700-1799/1728.Cat and Mouse II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,76 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```python
class Solution:
def canMouseWin(self, grid: List[str], catJump: int, mouseJump: int) -> bool:
dirs = [0, 1, 0, -1, 0]
m = len(grid)
n = len(grid[0])
nFloors = 0
cat = 0 # cat's position
mouse = 0 # mouse's position

def hash(i: int, j: int) -> int:
return i * n + j

for i in range(m):
for j in range(n):
if grid[i][j] != "#":
nFloors += 1
if grid[i][j] == "C":
cat = hash(i, j)
elif grid[i][j] == "M":
mouse = hash(i, j)

# dp(i, j, k) := True if mouse can win w//
# Cat on (i // 8, i % 8), mouse on (j // 8, j % 8), and turns = k
@functools.lru_cache(None)
def dp(cat: int, mouse: int, turn: int) -> bool:
# We already search whole touchable grid
if turn == nFloors * 2:
return False

if turn % 2 == 0:
# mouse's turn
i = mouse // n
j = mouse % n
for k in range(4):
for jump in range(mouseJump + 1):
x = i + dirs[k] * jump
y = j + dirs[k + 1] * jump
if x < 0 or x == m or y < 0 or y == n:
break
if grid[x][y] == "#":
break
if grid[x][y] == "F": # Mouse eats the food, so mouse win
return True
if dp(cat, hash(x, y), turn + 1):
return True
# Mouse can't win, so mouse lose
return False
else:
# cat's turn
i = cat // n
j = cat % n
for k in range(4):
for jump in range(catJump + 1):
x = i + dirs[k] * jump
y = j + dirs[k + 1] * jump
if x < 0 or x == m or y < 0 or y == n:
break
if grid[x][y] == "#":
break
if grid[x][y] == "F": # Cat eats the food, so mouse lose
return False
nextCat = hash(x, y)
if nextCat == mouse: # Cat catches mouse, so mouse lose
return False
if not dp(nextCat, mouse, turn + 1):
return False
# Cat can't win, so mouse win
return True

return dp(cat, mouse, 0)

```

Expand Down
70 changes: 70 additions & 0 deletions solution/1700-1799/1728.Cat and Mouse II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,76 @@
### **Python3**

```python
class Solution:
def canMouseWin(self, grid: List[str], catJump: int, mouseJump: int) -> bool:
dirs = [0, 1, 0, -1, 0]
m = len(grid)
n = len(grid[0])
nFloors = 0
cat = 0 # cat's position
mouse = 0 # mouse's position

def hash(i: int, j: int) -> int:
return i * n + j

for i in range(m):
for j in range(n):
if grid[i][j] != "#":
nFloors += 1
if grid[i][j] == "C":
cat = hash(i, j)
elif grid[i][j] == "M":
mouse = hash(i, j)

# dp(i, j, k) := True if mouse can win w//
# Cat on (i // 8, i % 8), mouse on (j // 8, j % 8), and turns = k
@functools.lru_cache(None)
def dp(cat: int, mouse: int, turn: int) -> bool:
# We already search whole touchable grid
if turn == nFloors * 2:
return False

if turn % 2 == 0:
# mouse's turn
i = mouse // n
j = mouse % n
for k in range(4):
for jump in range(mouseJump + 1):
x = i + dirs[k] * jump
y = j + dirs[k + 1] * jump
if x < 0 or x == m or y < 0 or y == n:
break
if grid[x][y] == "#":
break
if grid[x][y] == "F": # Mouse eats the food, so mouse win
return True
if dp(cat, hash(x, y), turn + 1):
return True
# Mouse can't win, so mouse lose
return False
else:
# cat's turn
i = cat // n
j = cat % n
for k in range(4):
for jump in range(catJump + 1):
x = i + dirs[k] * jump
y = j + dirs[k + 1] * jump
if x < 0 or x == m or y < 0 or y == n:
break
if grid[x][y] == "#":
break
if grid[x][y] == "F": # Cat eats the food, so mouse lose
return False
nextCat = hash(x, y)
if nextCat == mouse: # Cat catches mouse, so mouse lose
return False
if not dp(nextCat, mouse, turn + 1):
return False
# Cat can't win, so mouse win
return True

return dp(cat, mouse, 0)

```

Expand Down
70 changes: 70 additions & 0 deletions solution/1700-1799/1728.Cat and Mouse II/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
class Solution:
def canMouseWin(self, grid: List[str], catJump: int, mouseJump: int) -> bool:
dirs = [0, 1, 0, -1, 0]
m = len(grid)
n = len(grid[0])
nFloors = 0
cat = 0 # cat's position
mouse = 0 # mouse's position

def hash(i: int, j: int) -> int:
return i * n + j

for i in range(m):
for j in range(n):
if grid[i][j] != "#":
nFloors += 1
if grid[i][j] == "C":
cat = hash(i, j)
elif grid[i][j] == "M":
mouse = hash(i, j)

# dp(i, j, k) := True if mouse can win w//
# Cat on (i // 8, i % 8), mouse on (j // 8, j % 8), and turns = k
@functools.lru_cache(None)
def dp(cat: int, mouse: int, turn: int) -> bool:
# We already search whole touchable grid
if turn == nFloors * 2:
return False

if turn % 2 == 0:
# mouse's turn
i = mouse // n
j = mouse % n
for k in range(4):
for jump in range(mouseJump + 1):
x = i + dirs[k] * jump
y = j + dirs[k + 1] * jump
if x < 0 or x == m or y < 0 or y == n:
break
if grid[x][y] == "#":
break
if grid[x][y] == "F": # Mouse eats the food, so mouse win
return True
if dp(cat, hash(x, y), turn + 1):
return True
# Mouse can't win, so mouse lose
return False
else:
# cat's turn
i = cat // n
j = cat % n
for k in range(4):
for jump in range(catJump + 1):
x = i + dirs[k] * jump
y = j + dirs[k + 1] * jump
if x < 0 or x == m or y < 0 or y == n:
break
if grid[x][y] == "#":
break
if grid[x][y] == "F": # Cat eats the food, so mouse lose
return False
nextCat = hash(x, y)
if nextCat == mouse: # Cat catches mouse, so mouse lose
return False
if not dp(nextCat, mouse, turn + 1):
return False
# Cat can't win, so mouse win
return True

return dp(cat, mouse, 0)