-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFS.cs
100 lines (77 loc) · 2.73 KB
/
DFS.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PathAlgorithms
{
class DFS
{
int[] d1 = new int[] { 1, 1, 1, -1, -1, -1, 0, 0 }; // X movements
int[] d2 = new int[] { 1, -1, 0, 0, -1, 1, 1, -1 }; // Y movements
public DFS(int start_x, int start_y, int end_x, int end_y, int height_, int width_, List<int> wallx, List<int> wally)
{
startX = start_x;
startY = start_y;
endX = end_x;
endY = end_y;
height = height_;
width = width_;
wallX = wallx;
wallY = wally;
initialize();
}
#region attributes
int startX, startY;
int endX, endY;
static int height, width; // dimensions
List<int> wallX = new List<int>(); //walls
List<int> wallY = new List<int>();
int[,] table = new int[50, 50]; // table
List<Tuple<int, int>> toReturn = new List<Tuple<int, int>>();
#endregion
void initialize()
{
for (int i = 0; i <= width; i++)
{
for (int j = 0; j <= height; j++)
{
table[i, j] = 0;// all cells to 0
}
}
for (int i = 0; i < wallX.Count; i++)
{
table[wallX[i], wallY[i]] = 1; // build the wall
}
}
public void performDFS(Tuple<int,int,int> currentCell)
{
int currX, currY;
currX = currentCell.Item1;
currY = currentCell.Item2;
Tuple<int, int> toAppend = Tuple.Create(currX,currY);
if (currX < 0 || currY < 0 || currX >= width || currY >= height || table[currX,currY]==1) // check if cell available
return; // if not then stop
// otherwise continue...
table[currX, currY] = 1; // mark as visited
toReturn.Add(toAppend);
if (currX == endX && currY == endY)
{
//MessageBox.Show(currentCell.Item3.ToString() + " steps"); // number of steps
return;
}
for(int i = 0; i < 8; i++) // explore the neighbour cells
{
int nextX = currX + d1[i];
int nextY = currY + d2[i];
performDFS(Tuple.Create(nextX, nextY, currentCell.Item3 + 1)); // go to cell
}
}
public List<Tuple<int,int>> get_DFS_path()
{
performDFS(Tuple.Create(startX, startY, 0));
return toReturn;
}
}
}