Skip to content

Commit

Permalink
Create 1778.Shortest-Path-in-a-Hidden-Grid.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Mar 7, 2021
1 parent d2af4be commit a94fcae
Showing 1 changed file with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* // This is the GridMaster's API interface.
* // You should not implement it, or speculate about its implementation
* class GridMaster {
* public:
* bool canMove(char direction);
* void move(char direction);
* boolean isTarget();
* };
*/

typedef pair<int,int> PII;

class Solution {
int grid[1000][1000];
int visited[1000][1000];
int visited2[1000][1000];
public:
int findShortestPath(GridMaster &master)
{
visited[500][500] = 1;
dfs(500, 500, master);

auto dir = vector<PII>({{-1,0},{1,0},{0,-1},{0,1}});

queue<PII>q;
q.push({500,500});
visited2[500][500]=1;

int step = 0;
while (!q.empty())
{
int len = q.size();
step++;
while (len--)
{
auto [i,j] = q.front();
q.pop();

for (int k=0; k<4; k++)
{
int x = i + dir[k].first;
int y = j + dir[k].second;
if (grid[x][y]==2) return step;
if (grid[x][y]==0) continue;
if (visited2[x][y]==1) continue;

visited2[x][y] = 1;
q.push({x,y});
}
}
}
return -1;

}

void dfs(int i, int j, GridMaster &master)
{
auto dir = vector<PII>({{-1,0},{1,0},{0,-1},{0,1}});
vector<char> move({'U','D','L','R'});

grid[i][j] = 1;

if (master.isTarget())
{
grid[i][j] = 2;
return;
}

for (int k=0; k<4; k++)
{
int x = i+dir[k].first;
int y = j+dir[k].second;
if (visited[x][y]) continue;

if (master.canMove(move[k])==false)
grid[x][y] = 0;
else
{
visited[x][y] = 1;
master.move(move[k]);

dfs(x,y,master);

int kk;
if (k==0) kk=1;
else if (k==1) kk=0;
else if (k==2) kk=3;
else if (k==3) kk=2;
master.move(move[kk]);
}
}
}
};

0 comments on commit a94fcae

Please sign in to comment.