Unique Paths III Solutions in C++
Number 980
Difficulty Hard
Acceptance 73.4%
Link LeetCode
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/unique-paths-iii/// Author : Hao Chen// Date : 2019-02-03class Solution {public:int uniquePathsIII(vector<vector<int>>& grid) {int path = 0;int startX, startY;if (!findStartPoint( grid, startX, startY)) return 0;uniquePathsHelper(grid, startX, startY, path);return path;}bool findStartPoint(vector<vector<int>> &grid, int& x, int& y) {for(int i=0; i<grid.size(); i++) {for(int j=0; j<grid[0].size(); j++) {if (grid[i][j] == 1) {x = i; y =j;return true;}}}return false;}bool check(vector<vector<int>> &grid ) {for(int i=0; i<grid.size(); i++) {for(int j=0; j<grid[0].size(); j++) {if (grid[i][j] == 0 ) return false;}}return true;}void uniquePathsHelper(vector<vector<int>> &grid, int x, int y, int& path ) {if (x < 0 || y < 0 || x>= grid.size() || y>=grid[0].size()) return;if ( grid[x][y] < 0) return;if ( grid[x][y] == 2) {if (check(grid)) path++;return;}//back tracing - mark -2 means already passed.grid[x][y] = -2;uniquePathsHelper(grid, x, y-1, path); // upuniquePathsHelper(grid, x, y+1, path); // downuniquePathsHelper(grid, x+1, y, path); // rightuniquePathsHelper(grid, x-1, y, path); // leftgrid[x][y] = 0;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/unique-paths-iii//// Author : liuyubobobo/// Time : 2019-01-21#include <iostream>#include <vector>using namespace std;/// Backtrack/// Time Complexity: O(4 ^ (m * n))/// Space Complexity: O(m * n)class Solution {private:int m, n, end;const int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};public:int uniquePathsIII(vector<vector<int>>& grid) {m = grid.size();n = grid[0].size();int start, todo = 0;for(int i = 0; i < m; i ++)for(int j = 0; j < n; j ++){int pos = i * n + j;if(grid[i][j] == 1)start = pos;else if(grid[i][j] == 2)end = pos, grid[i][j] = 0, todo ++;else if(grid[i][j] == 0)todo ++;}return dfs(grid, start, todo);}private:int dfs(vector<vector<int>>& grid, int pos, int todo){if(pos == end)return !todo;int x = pos / n, y = pos % n, res = 0;for(int i = 0; i < 4; i ++){int nextx = x + d[i][0], nexty = y + d[i][1];if(nextx >= 0 && nextx < m && nexty >= 0 && nexty < n && grid[nextx][nexty] == 0){grid[nextx][nexty] = 1;res += dfs(grid, nextx * n + nexty, todo - 1);grid[nextx][nexty] = 0;}}return res;}};int main() {vector<vector<int>> g0 = {{1,0},{0,0},{0,2}};cout << Solution().uniquePathsIII(g0) << endl;// 1vector<vector<int>> g1 = {{1,0,0,0},{0,0,0,0},{0,0,2,-1}};cout << Solution().uniquePathsIII(g1) << endl;// 2vector<vector<int>> g2 = {{1,0,0,0},{0,0,0,0},{0,0,0,2}};cout << Solution().uniquePathsIII(g2) << endl;// 4return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/unique-paths-iii//// Author : liuyubobobo/// Time : 2020-04-28#include <iostream>#include <vector>using namespace std;/// State Compression + Memory Search/// Time Complexity: O(m * n * 2 ^ (m * n))/// Space Complexity: O(m * n * 2 ^ (m * n))class Solution {private:int m, n, end;const int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};public:int uniquePathsIII(vector<vector<int>>& grid) {m = grid.size(), n = grid[0].size();int start, state = 0;for(int i = 0; i < m; i ++)for(int j = 0; j < n; j ++){int pos = i * n + j;if(grid[i][j] == 1)start = pos;else if(grid[i][j] == 2)end = pos, grid[i][j] = 0;else if(grid[i][j] == -1)state |= (1 << pos);}vector<vector<int>> dp((m * n), vector<int>(1 << (m * n), -1));return dfs(grid, state | (1 << start), start, dp);}private:int dfs(vector<vector<int>>& grid, int state, int pos, vector<vector<int>>& dp){if(pos == end)return state == (1 << (m * n)) - 1 ? 1 : 0;if(dp[pos][state] != -1) return dp[pos][state];int x = pos / n, y = pos % n, res = 0;for(int i = 0; i < 4; i ++){int nextx = x + d[i][0], nexty = y + d[i][1];int next = nextx * n + nexty;if(in_area(nextx, nexty) && (state & (1 << next)) == 0){res += dfs(grid, state | (1 << next), next, dp);}}return dp[pos][state] = res;}bool in_area(int x, int y){return x >= 0 && x < m && y >= 0 && y < n;}};int main() {vector<vector<int>> g0 = {{1,0},{0,0},{0,2}};cout << Solution().uniquePathsIII(g0) << endl;// 1vector<vector<int>> g1 = {{1,0,0,0},{0,0,0,0},{0,0,2,-1}};cout << Solution().uniquePathsIII(g1) << endl;// 2vector<vector<int>> g2 = {{1,0,0,0},{0,0,0,0},{0,0,0,2}};cout << Solution().uniquePathsIII(g2) << endl;// 4vector<vector<int>> g3 = {{0,0,0,0,0},{0,2,1,0,0},{0,0,0,0,0},{0,0,0,0,0}};cout << Solution().uniquePathsIII(g3) << endl;// 8return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/unique-paths-iii//// Author : liuyubobobo/// Time : 2020-04-28#include <iostream>#include <vector>#include <map>using namespace std;/// State Compression + Dynamic Programming/// Time Complexity: O(m * n * 2 ^ (m * n))/// Space Complexity: O(m * n * 2 ^ (m * n))class Solution {private:int m, n;const int dirs[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};public:int uniquePathsIII(vector<vector<int>>& grid) {m = grid.size(), n = grid[0].size();int start, end, k = 0;map<int, int> posToIndex, indexToPos;for(int i = 0; i < m; i ++)for(int j = 0; j < n; j ++){int pos = i * n + j;if(grid[i][j] == 1)start = pos, grid[i][j] = 0;else if(grid[i][j] == 2)end = pos, grid[i][j] = 0;if(grid[i][j] == -1) continue;posToIndex[pos] = k;indexToPos[k] = pos;k ++;}vector<vector<int>> dp(k, vector<int>(1 << k, 0));dp[posToIndex[start]][1 << posToIndex[start]] = 1;for(int state = 1; state < (1 << k); state ++)for(int i = 0; i < k; i ++)if((state & (1 << i)) && dp[i][state]){int pos = indexToPos[i], x = pos / n, y = pos %n;for(int d = 0; d < 4; d ++) {int nextx = x + dirs[d][0], nexty = y + dirs[d][1], next = nextx * n + nexty;if(in_area(nextx, nexty) && grid[nextx][nexty] != -1 && (state & (1 << next)) == 0)dp[posToIndex[next]][state | (1 << posToIndex[next])] += dp[i][state];}}return dp[posToIndex[end]][(1 << k) - 1];}private:bool in_area(int x, int y){return x >= 0 && x < m && y >= 0 && y < n;}};int main() {vector<vector<int>> g0 = {{1,0},{0,0},{0,2}};cout << Solution().uniquePathsIII(g0) << endl;// 1vector<vector<int>> g1 = {{1,0,0,0},{0,0,0,0},{0,0,2,-1}};cout << Solution().uniquePathsIII(g1) << endl;// 2vector<vector<int>> g2 = {{1,0,0,0},{0,0,0,0},{0,0,0,2}};cout << Solution().uniquePathsIII(g2) << endl;// 4vector<vector<int>> g3 = {{0,0,0,0,0},{0,2,1,0,0},{0,0,0,0,0},{0,0,0,0,0}};cout << Solution().uniquePathsIII(g3) << endl;// 8return 0;}