You are given an M by N matrix consisting of booleans that represents a board. Each True boolean represents a wall. Each False boolean represents a tile you can walk on.
Given this matrix, a start coordinate, and an end coordinate, return the minimum number of steps required to reach the end coordinate from the start. If there is no possible path, then return null. You can move up, left, down, and right. You cannot move through walls. You cannot wrap around the edges of the board.
For example, given the following board:
[[f, f, f, f],
[t, t, f, t],
[f, f, f, f],
[f, f, f, f]]
and start = (3, 0)
(bottom left) and end = (0, 0)
(top left), the minimum number of steps required to reach the end is 7 since we would need to go through (1, 2)
because there is a wall everywhere else on the second row.
Example:
Input: board = {{1, 1, 1, 1}, {0, 0, 1, 0}, {1, 1, 1, 1}, {1, 1, 1, 1}}, srcX = 3, srcY = 0, destX = 0, destY = 0
Output: 7
Approach
C++
#include <bits/stdc++.h>using namespace std;bool isValid(vector<vector<bool>> &board,int M, int N,vector<vector<bool>> visited,int i, int j){return !(i < 0 || j < 0 || i >= M || j >= N ||!board[i][j] || visited[i][j]);}int ShortestPathInAMaze(vector<vector<bool>> &board,int srcX, int srcY,int destX, int destY){int M = board.size();int N = M > 0 ? board[0].size() : 0;vector<vector<bool>> visited(M, vector<bool>(N, false));queue<vector<int>> q;if (isValid(board, M, N, visited, srcX, srcY)){q.push({srcX, srcY, 0});visited[srcX][srcY] = true;}while (!q.empty()){vector<int> tempElement = q.front();q.pop();if (tempElement[0] == destX && tempElement[1] == destY){return tempElement[2];}// checking if the 4 elements are valid or//not and pushing in the queueif (isValid(board, M, N,visited, tempElement[0] + 1, tempElement[1])){q.push({tempElement[0] + 1, tempElement[1],tempElement[2] + 1});}if (isValid(board, M, N, visited,tempElement[0] - 1, tempElement[1])){q.push({tempElement[0] - 1, tempElement[1],tempElement[2] + 1});}if (isValid(board, M, N, visited,tempElement[0], tempElement[1] + 1)){q.push({tempElement[0], tempElement[1] + 1,tempElement[2] + 1});}if (isValid(board, M, N, visited,tempElement[0], tempElement[1] - 1)){q.push({tempElement[0], tempElement[1] - 1,tempElement[2] + 1});}}// -1 indicates no path exists.return -1;}int main(){vector<vector<bool>> board = {{1, 1, 1, 1},{0, 0, 1, 0},{1, 1, 1, 1},{1, 1, 1, 1}};int srcX = 3, srcY = 0;int destX = 0, destY = 0;cout << ShortestPathInAMaze(board, srcX, srcY, destX, destY);return 0;}
No comments:
Post a Comment