-
Notifications
You must be signed in to change notification settings - Fork 1
/
RoutePlanner.cs
51 lines (43 loc) · 1.38 KB
/
RoutePlanner.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
public class RoutePlanner
{
public static bool RouteExists(int fromRow, int fromColumn, int toRow, int toColumn, bool[,] mapMatrix)
{
bool[,] visited = new bool[mapMatrix.GetLength(0), mapMatrix.GetLength(1)];
return DFS(fromRow, fromColumn, toRow, toColumn, mapMatrix, visited);
}
private static bool DFS(int row, int col, int toRow, int toCol, bool[,] mapMatrix, bool[,] visited)
{
if (row < 0 ||
col < 0 ||
row >= mapMatrix.GetLength(0) ||
col >= mapMatrix.GetLength(1) ||
!mapMatrix[row, col] ||
visited[row, col])
{
return false;
}
if (row == toRow && col == toCol)
{
return true;
}
visited[row, col] = true;
if (DFS(row + 1, col, toRow, toCol, mapMatrix, visited) ||
DFS(row - 1, col, toRow, toCol, mapMatrix, visited) ||
DFS(row, col + 1, toRow, toCol, mapMatrix, visited) ||
DFS(row, col - 1, toRow, toCol, mapMatrix, visited))
{
return true;
}
return false;
}
public static void Main(string[] args)
{
bool[,] mapMatrix = {
{ true, false, false },
{ true, true, false },
{ false, true, true }
};
Console.WriteLine(RouteExists(0, 0, 2, 2, mapMatrix));
}
}