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 ed624f2

Browse files
committed
update 980.unique-paths-iii.0.java
1 parent 7a281dc commit ed624f2

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

‎980.unique-paths-iii.0.java‎

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* @lc app=leetcode id=980 lang=java
3+
*
4+
* [980] Unique Paths III
5+
*
6+
* https://leetcode.com/problems/unique-paths-iii/description/
7+
*
8+
* algorithms
9+
* Hard (72.57%)
10+
* Likes: 453
11+
* Dislikes: 55
12+
* Total Accepted: 27.3K
13+
* Total Submissions: 37.7K
14+
* Testcase Example: '[[1,0,0,0],[0,0,0,0],[0,0,2,-1]]'
15+
*
16+
* On a 2-dimensional grid, there are 4 types of squares:
17+
*
18+
*
19+
* 1 represents the starting square. There is exactly one starting square.
20+
* 2 represents the ending square. There is exactly one ending square.
21+
* 0 represents empty squares we can walk over.
22+
* -1 represents obstacles that we cannot walk over.
23+
*
24+
*
25+
* Return the number of 4-directional walks from the starting square to the
26+
* ending square, that walk over every non-obstacle square exactly once.
27+
*
28+
*
29+
*
30+
*
31+
* Example 1:
32+
*
33+
*
34+
* Input: [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
35+
* Output: 2
36+
* Explanation: We have the following two paths:
37+
* 1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
38+
* 2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
39+
*
40+
*
41+
* Example 2:
42+
*
43+
*
44+
* Input: [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
45+
* Output: 4
46+
* Explanation: We have the following four paths:
47+
* 1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
48+
* 2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
49+
* 3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
50+
* 4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
51+
*
52+
*
53+
* Example 3:
54+
*
55+
*
56+
* Input: [[0,1],[2,0]]
57+
* Output: 0
58+
* Explanation:
59+
* There is no path that walks over every empty square exactly once.
60+
* Note that the starting and ending square can be anywhere in the
61+
* grid.
62+
*
63+
*
64+
*
65+
*
66+
*
67+
*
68+
*
69+
* Note:
70+
*
71+
*
72+
* 1 <= grid.length * grid[0].length <= 20
73+
*
74+
*/
75+
76+
// @lc code=start
77+
class Solution {
78+
int n, m;
79+
HashMap<Integer, Integer> dp;
80+
81+
int p(int x, int y){ return x*m+y; }
82+
83+
int callme(int x, int y, int[][] grid, int cur){
84+
if(x<0 || y<0 || x>=grid.length || y>=grid[0].length || grid[x][y] == -1)
85+
return 0;
86+
if(grid[x][y] == 2)
87+
return cur == 0 ? 1 : 0;
88+
if((cur&(1<<p(x, y))) == 0)
89+
return 0;
90+
91+
if( dp.containsKey(cur*n*m + p(x, y)) )
92+
return dp.get(cur*n*m + p(x, y));
93+
94+
int ret = 0;
95+
cur ^= (1<<p(x, y));
96+
97+
//left
98+
ret += callme(x, y-1, grid, cur);
99+
//right
100+
ret += callme(x, y+1, grid, cur);
101+
//up
102+
ret += callme(x-1, y, grid, cur);
103+
//down
104+
ret += callme(x+1, y, grid, cur);
105+
106+
cur ^= (1<<p(x, y));
107+
108+
dp.put(cur*n*m + p(x, y), ret);
109+
return ret;
110+
}
111+
112+
public int uniquePathsIII(int[][] grid) {
113+
int cur = 0, x = 0, y = 0; n = grid.length; m = grid[0].length;
114+
dp = new HashMap<Integer, Integer>();
115+
116+
for(int i=0; i<n; i++)
117+
for(int j=0; j<m; j++){
118+
if(grid[i][j] == 0) //free
119+
cur ^= (1<<p(i, j));
120+
else if(grid[i][j] == 1){
121+
x = i;
122+
y = j;
123+
}
124+
}
125+
126+
grid[x][y] = -1;
127+
return callme(x+1, y, grid, cur)+callme(x-1, y, grid, cur)+callme(x, y-1, grid, cur)+callme(x, y+1, grid, cur);
128+
129+
}
130+
}
131+
// @lc code=end

0 commit comments

Comments
(0)

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