A game is played by a cat and a mouse named Cat and Mouse.
The environment is represented by a grid
of size rows x cols
, where each element is a wall, floor, player (Cat, Mouse), or food.
- Players are represented by the characters
'C'
(Cat),'M'
(Mouse). - Floors are represented by the character
'.'
and can be walked on. - Walls are represented by the character
'#'
and cannot be walked on. - Food is represented by the character
'F'
and can be walked on. - There is only one of each character
'C'
,'M'
, and'F'
ingrid
.
Mouse and Cat play according to the following rules:
- Mouse moves first, then they take turns to move.
- During each turn, Cat and Mouse can jump in one of the four directions (left, right, up, down). They cannot jump over the wall nor outside of the
grid
. catJump, mouseJump
are the maximum lengths Cat and Mouse can jump at a time, respectively. Cat and Mouse can jump less than the maximum length.- Staying in the same position is allowed.
- Mouse can jump over Cat.
The game can end in 4 ways:
- If Cat occupies the same position as Mouse, Cat wins.
- If Cat reaches the food first, Cat wins.
- If Mouse reaches the food first, Mouse wins.
- If Mouse cannot get to the food within 1000 turns, Cat wins.
Given a rows x cols
matrix grid
and two integers catJump
and mouseJump
, return true
if Mouse can win the game if both Cat and Mouse play optimally, otherwise return false
.
Example 1:
Input: grid = ["####F","#C...","M...."], catJump = 1, mouseJump = 2 Output: true Explanation: Cat cannot catch Mouse on its turn nor can it get the food before Mouse.
Example 2:
Input: grid = ["M.C...F"], catJump = 1, mouseJump = 4 Output: true
Example 3:
Input: grid = ["M.C...F"], catJump = 1, mouseJump = 3 Output: false
Example 4:
Input: grid = ["C...#","...#F","....#","M...."], catJump = 2, mouseJump = 5 Output: false
Example 5:
Input: grid = [".M...","..#..","#..#.","C#.#.","...#F"], catJump = 3, mouseJump = 1 Output: true
Constraints:
rows == grid.length
cols = grid[i].length
1 <= rows, cols <= 8
grid[i][j]
consist only of characters'C'
,'M'
,'F'
,'.'
, and'#'
.- There is only one of each character
'C'
,'M'
, and'F'
ingrid
. 1 <= catJump, mouseJump <= 8
Solution: MinMax + Memoization
Time complexity: O(m^3 * n^3 * max(n, m))
Space complexity: O(m^3 * n^3)
state: [mouse_pos, cat_pos, turn]
C++
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 |
// Author: Huahua class Solution { public: bool canMouseWin(vector<string>& grid, int catJump, int mouseJump) { const int m = grid.size(), n = grid[0].size(); int mouse, cat, food; for (int pos = 0; pos < m * n; ++pos) switch (grid[pos / n][ pos % n]) { case 'M': mouse = pos; break; case 'C': cat = pos; break; case 'F': food = pos; break; default: break; } const vector<pair<int, int>> dirs{{0, -1}, {0, 1}, {-1, 0}, {1, 0}}; const int limit = m * n * 2; vector<vector<vector<int>>> seen(m*n, vector<vector<int>>(m*n, vector<int>(limit + 1, -1))); function<bool(int, int, int)> dfs = [&](int mouse, int cat, int d) -> bool { const bool isCat = d & 1; if (cat == food || cat == mouse || d >= limit) return isCat; if (mouse == food) return !isCat; int& ans = seen[mouse][cat][d]; if (ans != -1) return ans; const int cur = isCat ? cat : mouse; const int jumps = isCat ? catJump : mouseJump; for (const auto& [dx, dy] : dirs) for (int j = 0; j <= jumps; ++j) { const int x = (cur % n) + dx * j; const int y = (cur / n) + dy * j; const int pos = y * n + x; if (x < 0 || x >= n || y < 0 || y >= m || grid[y][x] == '#') break; if (!dfs(isCat ? mouse : pos, isCat ? pos : cat, d + 1)) return ans = true; } return ans = false; }; return dfs(mouse, cat, 0); } }; |