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 1f146c9

Browse files
committed
update 980.unique-paths-iii.java
1 parent 4456a26 commit 1f146c9

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

‎980.unique-paths-iii.java‎

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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 zeros = 0;
79+
private int callme(int x, int y, int[][] grid){
80+
if(x<0 || y<0 || x>=grid.length || y>=grid[0].length)
81+
return 0;
82+
if(grid[x][y] == -1 || grid[x][y] == 3)
83+
return 0;
84+
if(grid[x][y] == 2){
85+
if(zeros == 0)
86+
return 1;
87+
else
88+
return 0;
89+
}
90+
91+
zeros--;
92+
grid[x][y] = 3;
93+
int ret = 0;
94+
95+
//left
96+
ret += callme(x, y-1, grid);
97+
//right
98+
ret += callme(x, y+1, grid);
99+
//up
100+
ret += callme(x-1, y, grid);
101+
//down
102+
ret += callme(x+1, y, grid);
103+
104+
zeros++;
105+
grid[x][y] = 0;
106+
return ret;
107+
}
108+
109+
public int uniquePathsIII(int[][] grid) {
110+
111+
for(int i=0; i<grid.length; i++)
112+
for(int j=0; j<grid[0].length; j++)
113+
if(grid[i][j] == 0)
114+
zeros++;
115+
for(int i=0; i<grid.length; i++)
116+
for(int j=0; j<grid[0].length; j++)
117+
if(grid[i][j] == 1){
118+
grid[i][j] = -1;
119+
return callme(i+1, j, grid)+callme(i-1, j, grid)+callme(i, j-1, grid)+callme(i, j+1, grid);
120+
}
121+
122+
return 0;
123+
}
124+
}
125+
// @lc code=end

0 commit comments

Comments
(0)

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