Last active
June 14, 2021 16:50
-
-
Save joshkautz/902fdb028232595f60740dd7b8616d60 to your computer and use it in GitHub Desktop.
Shortest path between two cells in a matrix
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
import 'dart:collection'; | |
class Point { | |
int x; | |
int y; | |
Point({required this.y,required this.x}); | |
} | |
void main() { | |
print('Finding a viable solution for the shortest path...'); | |
var grid = [ | |
['๐ฟ','๐ฟ','๐ฟ','๐ฟ','๐ฟ'], | |
['๐ฟ','๐ฟ','๐ฟ','๐ฟ','๐ฟ'], | |
['๐','๐ฟ','๐','๐ฟ','๐ฟ'], // Character is at position grid[2][1] | |
['๐ฟ','๐ฟ','๐','๐ฟ','๐ฟ'], // Destination is position grid[2][3] | |
['๐ฟ','๐ฟ','๐ฟ','๐ฟ','๐ฟ'], | |
]; | |
var characterPosition = Point(y: 2, x: 1); | |
var characterDestination = Point(y: 2, x: 3); | |
PathFinder pathFinder = PathFinder(grid, characterPosition, characterDestination); | |
if (pathFinder.solutionExists) { | |
print('...Solution exists!'); | |
for (Point point in pathFinder.solution) { | |
print('(${point.y},${point.x})'); | |
} | |
} | |
else { | |
print('...No solution exists.'); | |
} | |
} | |
class PathFinder { | |
late Queue<Point> solution; | |
late bool solutionExists; | |
PathFinder(grid, characterPosition, characterDestination) { | |
// Applying BFS on matrix cells. | |
Queue<Point> queue = Queue<Point>(); | |
queue.add(new Point(y: characterPosition.y, x: characterPosition.x)); | |
List<List<bool>> visited = List.generate( | |
grid.length, | |
(index) => List.generate( | |
grid[0].length, | |
(index) => false, | |
growable: false, | |
), | |
growable: false, | |
); | |
visited[characterPosition.y][characterPosition.x] = | |
true; | |
while (queue.isNotEmpty) { | |
Point point = queue.removeLast(); | |
// Destination found | |
if (point.x == characterDestination.x && | |
point.y == characterDestination.y) { | |
this.solutionExists = true; | |
this.solution = queue; | |
return; | |
} | |
// moving up | |
if (_isValid(x: point.x, y: point.y - 1, grid: grid, visited: visited)) { | |
queue.add(new Point(y: point.y - 1, x: point.x)); | |
visited[point.y - 1][point.x] = true; | |
} | |
// moving down | |
if (_isValid(x: point.x, y: point.y + 1, grid: grid, visited: visited)) { | |
queue.add(new Point(y: point.y + 1, x: point.x)); | |
visited[point.y + 1][point.x] = true; | |
} | |
// moving left | |
if (_isValid(x: point.x - 1, y: point.y, grid: grid, visited: visited)) { | |
queue.add(new Point(y: point.y, x: point.x - 1)); | |
visited[point.y][point.x - 1] = true; | |
} | |
// moving right | |
if (_isValid(x: point.x + 1, y: point.y, grid: grid, visited: visited)) { | |
queue.add(new Point(y: point.y, x: point.x + 1)); | |
visited[point.y][point.x + 1] = true; | |
} | |
} | |
this.solutionExists = false; | |
this.solution = queue; | |
return; | |
} | |
bool _isValid({required int x, required int y, required List<List<String>> grid, required List<List<bool>> visited}) { | |
if (x >= 0 && | |
y >= 0 && | |
x < grid[0].length && | |
y < grid.length && | |
grid[y][x] != '๐' && | |
visited[y][x] == false) { | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment