-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
18862b1
commit ddf80b5
Showing
12 changed files
with
311 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,43 @@ | ||
import 'package:path_finding/algorithm/algorithm_path.dart'; | ||
import 'package:path_finding/models/algorithm_result.dart'; | ||
import 'package:path_finding/models/block_state.dart'; | ||
|
||
import 'node/path_node.dart'; | ||
|
||
abstract class Algorithm { | ||
Algorithm(); | ||
String name; | ||
Algorithm({required this.name}); | ||
|
||
AlgorithmResult execute(List<List<BlockState>> matrix); | ||
|
||
@override | ||
bool operator ==(Object other) { | ||
if (identical(this, other)) return true; | ||
return other is Algorithm && other.name == name; | ||
} | ||
|
||
@override | ||
int get hashCode => name.hashCode; | ||
} | ||
|
||
AlgorithmPath constructPath(PathNode? endNode) { | ||
if (endNode == null) { | ||
throw Exception("End node not found in path"); | ||
} | ||
|
||
final List<int> rows = []; | ||
final List<int> columns = []; | ||
var currentNode = endNode; | ||
|
||
while (currentNode.cameFrom != null) { | ||
rows.insert(0, currentNode.row); | ||
columns.insert(0, currentNode.column); | ||
currentNode = currentNode.cameFrom!; | ||
} | ||
|
||
// Insert the start node's coordinates | ||
rows.insert(0, currentNode.row); | ||
columns.insert(0, currentNode.column); | ||
|
||
return AlgorithmPath(rows, columns); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import 'package:path_finding/algorithm/algorithm.dart'; | ||
import 'package:path_finding/algorithm/node/path_node.dart'; | ||
import 'package:path_finding/models/models.dart'; | ||
|
||
class DijkstraAlgorithm implements Algorithm { | ||
DijkstraAlgorithm() : super(); | ||
|
||
@override | ||
AlgorithmResult execute(List<List<BlockState>> matrix) { | ||
final rows = matrix.length; | ||
final columns = matrix[0].length; | ||
final changes = <Change>[]; | ||
|
||
final grid = List.generate( | ||
rows, | ||
(row) => List.generate( | ||
columns, | ||
(col) => DijkstraNode(row, col, matrix[row][col]), | ||
), | ||
); | ||
|
||
DijkstraNode? startNode; | ||
DijkstraNode? endNode; | ||
for (var row = 0; row < rows; row++) { | ||
for (var col = 0; col < columns; col++) { | ||
final blockState = matrix[row][col]; | ||
if (blockState == BlockState.start) { | ||
startNode = grid[row][col]; | ||
} else if (blockState == BlockState.end) { | ||
endNode = grid[row][col]; | ||
} | ||
} | ||
} | ||
|
||
if (startNode == null || endNode == null) { | ||
throw Exception("Start and End nodes need to be set"); | ||
} | ||
startNode.distance = 0; | ||
|
||
final openSet = [startNode]; | ||
final closedSet = <DijkstraNode>[]; | ||
|
||
while (openSet.isNotEmpty) { | ||
final currentNode = | ||
openSet.reduce((a, b) => a.distance < b.distance ? a : b); | ||
|
||
if (currentNode == endNode) { | ||
final path = constructPath(currentNode); | ||
return AlgorithmResult(changes, path); | ||
} | ||
|
||
openSet.remove(currentNode); | ||
closedSet.add(currentNode); | ||
changes | ||
.add(Change(currentNode.row, currentNode.column, BlockState.visited)); | ||
|
||
for (final neighbor in currentNode.getNeighbors(grid, rows, columns)) { | ||
if (closedSet.contains(neighbor) || | ||
neighbor.blockState == BlockState.wall) { | ||
continue; | ||
} | ||
|
||
final newDistance = currentNode.distance + 1; | ||
if (newDistance < neighbor.distance) { | ||
neighbor.cameFrom = currentNode; | ||
neighbor.distance = newDistance; | ||
} | ||
|
||
if (!openSet.contains(neighbor)) { | ||
openSet.add(neighbor); | ||
changes | ||
.add(Change(neighbor.row, neighbor.column, BlockState.visited)); | ||
} | ||
} | ||
} | ||
|
||
return AlgorithmResult(changes, null); | ||
} | ||
|
||
@override | ||
String name = "Dijkstra"; | ||
|
||
@override | ||
bool operator ==(Object other) { | ||
if (identical(this, other)) return true; | ||
return other is Algorithm && other.name == name; | ||
} | ||
|
||
@override | ||
int get hashCode => name.hashCode; | ||
} | ||
|
||
class DijkstraNode extends PathNode { | ||
int distance; | ||
BlockState blockState; | ||
|
||
DijkstraNode(int row, int column, this.blockState) | ||
: distance = 2147483647, // Maximum value for 32-bit integer | ||
super(row, column); | ||
|
||
List<DijkstraNode> getNeighbors( | ||
List<List<DijkstraNode>> grid, int rows, int columns) { | ||
final neighbors = <DijkstraNode>[]; | ||
if (row > 0) neighbors.add(grid[row - 1][column]); | ||
if (row < rows - 1) neighbors.add(grid[row + 1][column]); | ||
if (column > 0) neighbors.add(grid[row][column - 1]); | ||
if (column < columns - 1) neighbors.add(grid[row][column + 1]); | ||
|
||
// Filter out wall nodes if necessary | ||
return neighbors | ||
.where((node) => node.blockState != BlockState.wall) | ||
.toList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
abstract class PathNode { | ||
final int row; | ||
final int column; | ||
PathNode? cameFrom; | ||
|
||
PathNode(this.row, this.column); | ||
|
||
// You can add other common methods or properties here if needed | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
PODS: | ||
- FlutterMacOS (1.0.0) | ||
|
||
DEPENDENCIES: | ||
- FlutterMacOS (from `Flutter/ephemeral`) | ||
|
||
EXTERNAL SOURCES: | ||
FlutterMacOS: | ||
:path: Flutter/ephemeral | ||
|
||
SPEC CHECKSUMS: | ||
FlutterMacOS: 8f6f14fa908a6fb3fba0cd85dbd81ec4b251fb24 | ||
|
||
PODFILE CHECKSUM: 236401fc2c932af29a9fcf0e97baeeb2d750d367 | ||
|
||
COCOAPODS: 1.12.1 |
Oops, something went wrong.