检查方向矩阵中给定单元格之间是否存在有效路径
给定一个N x M矩阵。矩阵的每个单元格都有一个数值,指向它通向的下一个单元格。 matrix[i][j] 的值可以是:
- 1表示转到右侧的单元格。 (即从矩阵[i][j] 到矩阵[i][j + 1])
- 2这意味着去左边的单元格。 (即从 matrix[i][j] 到 matrix[i][j – 1])
- 3这意味着去下层单元格。 (即从矩阵[i][j] 到矩阵[i + 1][j])
- 4这意味着去上层单元格。 (即从 matrix[i][j] 到 matrix[i – 1][j])
给出了源单元格 (x1, y1) 和目标单元格 (x2, y2)。任务是查找给定源单元格和目标单元格之间是否存在路径。
例子:
Input: matrix[][] = {{3, 2, 2, 2}, {3, 4, 2, 3}, {1, 3, 4, 4}, {2, 1, 1, 2}, {4, 4, 3, 3}}, source cell = {0, 3}, destination cell = {3, 1}
Output: Yes
3 | 2 | 2 | 2 |
3 | 4 | 2 | 3 |
1 | 3 | 4 | 4 |
2 | 1 | 1 | 2 |
4 | 4 | 3 | 3 |
Explanation : Starting from cell (0, 3), follow the path based on the direction rule. Traverse the cells in the following order: (0, 3)->(0, 2)->(0, 1)->(0, 0)->(1, 0)->(2, 0)->(2, 1)->(3, 1) which is the destination.
Input: matrix[][]={{1, 4, 3}, {2, 3, 2}, {4, 1, 4}}, source cell = {1, 1}, destination cell = {0, 3}
Output: No1 4 3 2 3 2 4 1 4
Explanation: Starting from cell (1, 1), follow the path based on the direction rule. Traverse the cells as: (1, 1)->(2, 1)->(2, 2)->(1, 2)->(1, 1), here source cell is visited again and thus in any case destination cell is not visited.
方法:该任务可以通过使用 DFS 或 BFS 来解决。
- 可以观察到,要遵循的路径是固定的,我们需要根据指定的方向规则遍历矩阵。
- 从源单元开始 DFS 或 BFS,并根据上述方向规则移动。
- 如果到达目标单元格,则找到有效路径。
- 如果再次访问已访问的单元格或当前单元格的索引超出范围,则无法通过该路径到达目标单元格,否则继续。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check whether a valid path
// exists or not
bool uniquepath(vector >& matrix, int curr_x,
int curr_y, int dest_x, int dest_y,
vector >& visited)
{
// Check if destination cell is reached
if (curr_x == dest_x && curr_y == dest_y)
return true;
// Base Cases
if (!(curr_x >= 0 && curr_x < matrix.size()
&& curr_y >= 0 && curr_y < matrix[0].size()))
return false;
// Check whether a visited cell is
// re-visited again
if (visited[curr_x][curr_y] == true)
return false;
// Mark current cell as visited
visited[curr_x][curr_y] = true;
// Moving based on direction rule
if (matrix[curr_x][curr_y] == 1)
return uniquepath(matrix, curr_x, curr_y + 1,
dest_x, dest_y, visited);
else if (matrix[curr_x][curr_y] == 2)
return uniquepath(matrix, curr_x, curr_y - 1,
dest_x, dest_y, visited);
else if (matrix[curr_x][curr_y] == 3)
return uniquepath(matrix, curr_x + 1, curr_y,
dest_x, dest_y, visited);
else if (matrix[curr_x][curr_y] == 4)
return uniquepath(matrix, curr_x - 1, curr_y,
dest_x, dest_y, visited);
}
// Driver code
int main()
{
vector > matrix = { { 3, 2, 2, 2 },
{ 3, 4, 2, 3 },
{ 1, 3, 4, 4 },
{ 2, 1, 1, 2 },
{ 4, 4, 1, 2 } };
int start_x = 0, start_y = 3;
int dest_x = 3, dest_y = 1;
int n = matrix.size();
int m = matrix[0].size();
vector > visited(
n,
vector(m, false));
if (uniquepath(matrix, start_x, start_y,
dest_x, dest_y,
visited))
cout << "Yes";
else
cout << "No";
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to check whether a valid path
// exists or not
static boolean uniquepath(int[][]matrix, int curr_x,
int curr_y, int dest_x,
int dest_y, boolean[][] visited)
{
// Check if destination cell is reached
if (curr_x == dest_x && curr_y == dest_y)
return true;
// Base Cases
if (!(curr_x >= 0 && curr_x < matrix.length &&
curr_y >= 0 && curr_y < matrix[0].length))
return false;
// Check whether a visited cell is
// re-visited again
if (visited[curr_x][curr_y] == true)
return false;
// Mark current cell as visited
visited[curr_x][curr_y] = true;
// Moving based on direction rule
if (matrix[curr_x][curr_y] == 1)
return uniquepath(matrix, curr_x, curr_y + 1,
dest_x, dest_y, visited);
else if (matrix[curr_x][curr_y] == 2)
return uniquepath(matrix, curr_x, curr_y - 1,
dest_x, dest_y, visited);
else if (matrix[curr_x][curr_y] == 3)
return uniquepath(matrix, curr_x + 1, curr_y,
dest_x, dest_y, visited);
else if (matrix[curr_x][curr_y] == 4)
return uniquepath(matrix, curr_x - 1, curr_y,
dest_x, dest_y, visited);
return false;
}
// Driver code
public static void main(String[] args)
{
int[][] matrix = { { 3, 2, 2, 2 },
{ 3, 4, 2, 3 },
{ 1, 3, 4, 4 },
{ 2, 1, 1, 2 },
{ 4, 4, 1, 2 } };
int start_x = 0, start_y = 3;
int dest_x = 3, dest_y = 1;
int n = matrix.length;
int m = matrix[0].length;
boolean[][] visited = new boolean[n][m];
if (uniquepath(matrix, start_x, start_y,
dest_x, dest_y, visited))
System.out.print("Yes");
else
System.out.print("No");
}
}
// This code is contributed by 29AjayKumar
Python3
# python program for the above approach
# Function to check whether a valid path
# exists or not
def uniquepath(matrix, curr_x, curr_y, dest_x, dest_y, visited):
# Check if destination cell is reached
if (curr_x == dest_x and curr_y == dest_y):
return True
# Base Cases
if (not(curr_x >= 0 and curr_x < len(matrix) and curr_y >= 0 and curr_y < len(matrix[0]))):
return False
# Check whether a visited cell is
# re-visited again
if (visited[curr_x][curr_y] == True):
return False
# Mark current cell as visited
visited[curr_x][curr_y] = True
# Moving based on direction rule
if (matrix[curr_x][curr_y] == 1):
return uniquepath(matrix, curr_x, curr_y + 1, dest_x, dest_y, visited)
elif (matrix[curr_x][curr_y] == 2):
return uniquepath(matrix, curr_x, curr_y - 1, dest_x, dest_y, visited)
elif (matrix[curr_x][curr_y] == 3):
return uniquepath(matrix, curr_x + 1, curr_y, dest_x, dest_y, visited)
elif (matrix[curr_x][curr_y] == 4):
return uniquepath(matrix, curr_x - 1, curr_y, dest_x, dest_y, visited)
# Driver code
if __name__ == "__main__":
matrix = [[3, 2, 2, 2],
[3, 4, 2, 3],
[1, 3, 4, 4],
[2, 1, 1, 2],
[4, 4, 1, 2]
]
start_x = 0
start_y = 3
dest_x = 3
dest_y = 1
n = len(matrix)
m = len(matrix[0])
visited = [[False for _ in range(m)] for _ in range(n)]
if (uniquepath(matrix, start_x, start_y, dest_x, dest_y, visited)):
print("Yes")
else:
print("No")
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
public class GFG{
// Function to check whether a valid path
// exists or not
static bool uniquepath(int[,]matrix, int curr_x,
int curr_y, int dest_x,
int dest_y, bool[,] visited)
{
// Check if destination cell is reached
if (curr_x == dest_x && curr_y == dest_y)
return true;
// Base Cases
if (!(curr_x >= 0 && curr_x < matrix.GetLength(0) &&
curr_y >= 0 && curr_y < matrix.GetLength(1)))
return false;
// Check whether a visited cell is
// re-visited again
if (visited[curr_x,curr_y] == true)
return false;
// Mark current cell as visited
visited[curr_x,curr_y] = true;
// Moving based on direction rule
if (matrix[curr_x,curr_y] == 1)
return uniquepath(matrix, curr_x, curr_y + 1,
dest_x, dest_y, visited);
else if (matrix[curr_x,curr_y] == 2)
return uniquepath(matrix, curr_x, curr_y - 1,
dest_x, dest_y, visited);
else if (matrix[curr_x,curr_y] == 3)
return uniquepath(matrix, curr_x + 1, curr_y,
dest_x, dest_y, visited);
else if (matrix[curr_x,curr_y] == 4)
return uniquepath(matrix, curr_x - 1, curr_y,
dest_x, dest_y, visited);
return false;
}
// Driver code
public static void Main(String[] args)
{
int[,] matrix = { { 3, 2, 2, 2 },
{ 3, 4, 2, 3 },
{ 1, 3, 4, 4 },
{ 2, 1, 1, 2 },
{ 4, 4, 1, 2 } };
int start_x = 0, start_y = 3;
int dest_x = 3, dest_y = 1;
int n = matrix.GetLength(0);
int m = matrix.GetLength(1);
bool[,] visited = new bool[n,m];
if (uniquepath(matrix, start_x, start_y,
dest_x, dest_y, visited))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by 29AjayKumar
Javascript
Yes
时间复杂度:O(n*m)
辅助空间:O(n*m)