Skip to content

Commit 3ffd5a8

Browse files
committed
Fix up breadth first search
1 parent dd6470f commit 3ffd5a8

File tree

2 files changed

+40
-32
lines changed

2 files changed

+40
-32
lines changed

src/algorithms/pathfinding/BreadthFirstSearch.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import { Cell } from "../utils/PathfindingUtils";
22

33
// Define a function to perform breadth-first search on the maze
4-
function BreadthFirstSearch(maze: Cell[][], startRow: number, startCol: number, endRow: number, endCol: number): Cell[][] | null {
4+
export function BreadthFirstSearch(maze: Cell[][], startCell: Cell, endCell: Cell): boolean[][] | null {
55
const queue: Cell[] = [];
6-
const visited: Cell[][] = [];
6+
const visited: boolean[][] = [];
77

88
// Initialize the visited array
99
for (let row = 0; row < maze.length; row++) {
1010
visited[row] = [];
1111
for (let col = 0; col < maze[row].length; col++) {
12-
visited[row][col] = new Cell(row, col);
13-
visited[row][col].wall = maze[row][col].wall;
12+
visited[row][col] = true;
1413
}
1514
}
1615

1716
// Add the start cell to the queue
18-
queue.push(maze[startRow][startCol]);
19-
visited[startRow][startCol].visited = true;
17+
queue.push(startCell);
18+
visited[startCell.row][startCell.col] = true;
2019

2120
// Perform breadth-first search
2221
while (queue.length > 0) {
@@ -27,7 +26,7 @@ function BreadthFirstSearch(maze: Cell[][], startRow: number, startCol: number,
2726
}
2827

2928
// Check if we have reached the end cell
30-
if (currentCell.row === endRow && currentCell.col === endCol) {
29+
if (currentCell.row === endCell.row && currentCell.col === endCell.col) {
3130
return visited;
3231
}
3332

@@ -36,9 +35,9 @@ function BreadthFirstSearch(maze: Cell[][], startRow: number, startCol: number,
3635

3736
// Visit each neighbor
3837
for (const neighbor of neighbors) {
39-
if (!visited[neighbor.row][neighbor.col].visited) {
38+
if (!visited[neighbor.row][neighbor.col]) {
4039
queue.push(neighbor);
41-
visited[neighbor.row][neighbor.col].visited = true;
40+
visited[neighbor.row][neighbor.col] = true;
4241
}
4342
}
4443
}

src/pages/PathfindingVisualiser.tsx

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import useWindowDimensions from '../utils/useWindowDimensions';
33
import '../styles/pathfinding.css';
44
import { HiChevronRight } from "react-icons/hi";
55
import { Cell } from '../algorithms/utils/PathfindingUtils';
6+
import { BreadthFirstSearch } from '../algorithms/pathfinding/BreadthFirstSearch.ts';
67

78
const PathfindingVisualiser: React.FC = () => {
89
const { width = 0, height = 0 } = useWindowDimensions();
@@ -109,30 +110,38 @@ const PathfindingVisualiser: React.FC = () => {
109110
});
110111
};
111112

113+
function triggerBFS(){
114+
let visited = BreadthFirstSearch(maze, maze[startNode[0]][startNode[1]], maze[endNode[0]][endNode[1]]);
115+
console.log(visited);
116+
}
117+
112118
return (
113-
<div onMouseUp={handleMouseUp}>
114-
{maze.map((row, rowIndex) => (
115-
<div key={rowIndex} className="pathfinding-row" id={'row-' + rowIndex}>
116-
{row.map((cell, colIndex) => (
117-
<div
118-
key={colIndex}
119-
onMouseDown={() => handleMouseDown(rowIndex, colIndex)}
120-
onMouseEnter={() => handleMouseEnter(rowIndex, colIndex)}
121-
style={{
122-
width: '26px',
123-
height: '26px',
124-
backgroundColor: cell.start ? 'lightblue' : ( cell.end ? 'lightgreen' : (cell.wall ? 'black' : 'white')),
125-
border: '.5px solid lightblue',
126-
display: 'flex',
127-
alignItems: 'center',
128-
justifyContent: 'center'
129-
}}>
130-
{cell.start ? <HiChevronRight style={{color: 'black', fontSize: '26px'}} /> : ''}
131-
{cell.end ? <HiChevronRight style={{color: 'black', fontSize: '26px', transform: 'rotate(180deg)'}} /> : ''}
132-
</div>
133-
))}
134-
</div>
135-
))}
119+
<div>
120+
<div onMouseUp={handleMouseUp}>
121+
{maze.map((row, rowIndex) => (
122+
<div key={rowIndex} className="pathfinding-row" id={'row-' + rowIndex}>
123+
{row.map((cell, colIndex) => (
124+
<div
125+
key={colIndex}
126+
onMouseDown={() => handleMouseDown(rowIndex, colIndex)}
127+
onMouseEnter={() => handleMouseEnter(rowIndex, colIndex)}
128+
style={{
129+
width: '26px',
130+
height: '26px',
131+
backgroundColor: cell.start ? 'lightblue' : ( cell.end ? 'lightgreen' : ( cell.visited ? 'orange' : (cell.wall ? 'black' : 'white'))),
132+
border: '.5px solid lightblue',
133+
display: 'flex',
134+
alignItems: 'center',
135+
justifyContent: 'center'
136+
}}>
137+
{cell.start ? <HiChevronRight style={{color: 'black', fontSize: '26px'}} /> : ''}
138+
{cell.end ? <HiChevronRight style={{color: 'black', fontSize: '26px', transform: 'rotate(180deg)'}} /> : ''}
139+
</div>
140+
))}
141+
</div>
142+
))}
143+
</div>
144+
<button onClick={() => triggerBFS()}></button>
136145
</div>
137146
);
138147
};

0 commit comments

Comments
 (0)