|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Collections; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using System.Windows.Forms; |
| 8 | + |
| 9 | +namespace PathAlgorithms |
| 10 | +{ |
| 11 | + |
| 12 | + |
| 13 | + class BFS |
| 14 | + { |
| 15 | + |
| 16 | + int[] d1 = new int[] { 1, 1, 1, -1, -1, -1, 0, 0 }; // movements |
| 17 | + int[] d2 = new int[] { 1, -1, 0, 0, -1, 1, 1, -1 }; |
| 18 | + public BFS(int start_x, int start_y, int end_x, int end_y, int height_, int width_, List<int> wallx, List<int> wally) |
| 19 | + { |
| 20 | + startX = start_x; |
| 21 | + startY = start_y; |
| 22 | + endX = end_x; |
| 23 | + endY = end_y; |
| 24 | + height = height_; |
| 25 | + width = width_; |
| 26 | + wallX = wallx; |
| 27 | + wallY = wally; |
| 28 | + |
| 29 | + initialize(); |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + #region attributes |
| 34 | + int startX, startY; |
| 35 | + int endX, endY; |
| 36 | + static int height, width; // dimensions |
| 37 | + |
| 38 | + List<int> wallX = new List<int>(); //walls |
| 39 | + List<int> wallY = new List<int>(); |
| 40 | + |
| 41 | + int[,] table = new int[50, 50]; // table |
| 42 | + #endregion |
| 43 | + |
| 44 | + |
| 45 | + #region methods |
| 46 | + void initialize() |
| 47 | + { |
| 48 | + for (int i = 0; i <= width; i++) |
| 49 | + { |
| 50 | + for (int j = 0; j <= height; j++) |
| 51 | + { |
| 52 | + table[i, j] = 0;// all cells to 0 |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + for (int i = 0; i < wallX.Count; i++) |
| 57 | + { |
| 58 | + table[wallX[i], wallY[i]] = 1; // build the wall |
| 59 | + } |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + public List<Tuple<int,int>> performBFS() |
| 64 | + { |
| 65 | + List<Tuple<int, int>> toReturn= new List<Tuple<int,int>>(); |
| 66 | + var queue = new Queue<Tuple<int, int, int>>(); // generic queue declaration |
| 67 | + |
| 68 | + queue.Enqueue(Tuple.Create(startX, startY, 0)); // insert the starting point |
| 69 | + |
| 70 | + while (queue.Count != 0) // while still elements in queue |
| 71 | + { |
| 72 | + var curr = queue.Dequeue(); // get the next element and pop it out the queue |
| 73 | + |
| 74 | + toReturn.Add(Tuple.Create(curr.Item1, curr.Item2)); |
| 75 | + |
| 76 | + if (curr.Item1 == endX && curr.Item2 == endY) // if the target found break |
| 77 | + { |
| 78 | + MessageBox.Show(curr.Item3.ToString()+" steps"); |
| 79 | + break; |
| 80 | + } |
| 81 | + |
| 82 | + for (int i = 0; i < 8; i++) // for each neighbour |
| 83 | + { |
| 84 | + // update the coordinates |
| 85 | + int nextX = curr.Item1 + d1[i]; |
| 86 | + int nextY = curr.Item2 + d2[i]; |
| 87 | + |
| 88 | + // check if inside the bounds |
| 89 | + if (nextX >= 0 && nextY >= 0 && nextX < width && nextY < height) |
| 90 | + { |
| 91 | + // check if visited before |
| 92 | + if (table[nextX, nextY] == 0) |
| 93 | + { |
| 94 | + table[nextX, nextY] = 1; |
| 95 | + // Visited[Tuple.Create(nextX, nextY)] = true; // mark as visited |
| 96 | + queue.Enqueue(Tuple.Create(nextX, nextY, curr.Item3+1)); // insert into queue |
| 97 | + toReturn.Add(Tuple.Create(nextX, nextY)); |
| 98 | + |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + #endregion |
| 104 | + |
| 105 | + |
| 106 | + return toReturn; |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + } |
| 111 | +} |
0 commit comments