2
int[] dx = new int[] {0, 0, 1, -1};3
int[] dy = new int[] {-1, 1, 0, 0};8
public boolean canMouseWin(String[] grid, int catJump, int mouseJump) {11
int mouseI = 0, mouseJ = 0;12
int catI = 0, catJ = 0;13
for (int i = 0; i < m; i++) {14
for (int j = 0; j < n; j++) {15
switch (grid[i].charAt(j)) {36
new Boolean[TIME_MAX + 1][m][n][m][n]);48
Boolean[][][][][] memo) {49
if (grid[mI].charAt(mJ) == 'F') { // mouse got the food -> mouse won52
if (grid[cI].charAt(cJ) == 'F' || cI == mI && cJ == mJ || time > TIME_MAX) {53
return false; // cat got the food / draw / cat got the mouse -> cat won.55
if (memo[time][mI][mJ][cI][cJ] != null) {56
return memo[time][mI][mJ][cI][cJ];58
boolean mT = time % 2 == 0;59
int jump = mT ? mJump : cJump;62
if (!mT && !dfs(time + 1, mI, mJ, cI, cJ, cJump, mJump, grid, memo)) {63
return memo[time][mI][mJ][cI][cJ] = false; // cat's turn and cat stays still and cat won65
for (int i = 0; i < 4; i++) {66
for (int j = 1; j <= jump; j++) {67
int nx = x + dx[i] * j;68
int ny = y + dy[i] * j;69
if (nx < 0 || ny < 0 || nx == m || ny == n || grid[nx].charAt(ny) == '#') {70
break; // note that cat & mouse can't jump over #. I got a WA thinking they can jump over84
if (mT && res) { // if it is mouse's turn and it can find ONE way to win, then mouse win.85
return memo[time][mI][mJ][cI][cJ] = true;88
&& !res) { // if it is cat's turn and it can find ONE way to a draw or cat win, then cat90
return memo[time][mI][mJ][cI][cJ] = false;94
return memo[time][mI][mJ][cI][cJ] = !mT; // otherwise, return !mT