diff --git a/.idea/workspace.xml b/.idea/workspace.xml index d2f51d5..f0fbb95 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,7 @@ - + @@ -339,20 +342,30 @@ - + - - + + + + + + + + + - + - + - - + + + + + @@ -367,19 +380,12 @@ - - - - - - - - + - - + + - + diff --git a/eight_queens/lib/queens.dart b/eight_queens/lib/queens.dart index 8eac5a1..682f8e8 100644 --- a/eight_queens/lib/queens.dart +++ b/eight_queens/lib/queens.dart @@ -1,7 +1,8 @@ -/** - * Queen Resolver class - * Solved using algorithm found in: https://www.geeksforgeeks.org/n-queen-problem-using-branch-and-bound/ - */ +/// +///Queen Resolver class +///Solved using algorithm found in: https://www.geeksforgeeks.org/n-queen-problem-backtracking-3/ +///Adapted for use with Dart and Flutter +/// import 'package:shared_preferences/shared_preferences.dart'; @@ -9,6 +10,10 @@ class QueenResolver { int count = 0; + /// + /// Checks if a queen can be placed on a [board] in a [row] and [col] + /// Returns a [bool] if it's safe to place it [true] or not [false] + /// Future isSafe(board, int row, int col, int n) async { // Check row on the left side for (var i = 0; i < col; i++) { @@ -40,8 +45,15 @@ class QueenResolver { return true; } + /// + /// Main eight queens algorithm + /// Receives a single [board] and [col] positions, as well as [n] queens. + /// Will return [true] when solved. + /// Future solveQueens(board, int col, int n) async { + /* Solve for the base case : All queens are placed + * Then, store it into local storage */ if (col >= n) { count++; storeSolution(board).then((value) { @@ -50,6 +62,7 @@ class QueenResolver { } var res = false; + /** Consider the [col] and try placing it on the [board] **/ for(int i = 0; i < n; i++) { if(await isSafe(board, i, col, n)){ board[i][col] = 1; @@ -59,7 +72,10 @@ class QueenResolver { } return res; } - + + /// Stores a complete [board] into [SharedPreferences] + /// Because the [board] is not completely initialized, it requires to convert all [null] values to [0]. + /// Stores also the current [count] into [SharedPreferences] Future storeSolution(List> board) async { for(int i = 0; i < board.length; i++) { for(int x = 0; x < board[i].length ; x++) { @@ -73,6 +89,10 @@ class QueenResolver { await prefs.setInt('count', count); } + /// + /// Function entryway to solve for [n] queens. + /// Will return [count] for the app's purposes + /// Future solve(int n) async { // Create the initial queens board List> board = new List.generate(n, (_) => new List(n));