leetcode

Leetcode submissions
git clone git://git.laack.co/leetcode.git
Log | Files | Refs | README

n-queens.dart (2498B)


      1 //Given a number n return all possible configurations of queens
      2 //on a chess board such that none can take another assuming they are all on their
      3 //own teams where the chess board is size n*n. To do this I used 
      4 //1's to denote places that are reserved by other queens that have been previously placed.
      5 //Given this I iterated through the current row to find all possible spaces that are not being taken
      6 //by another queen. If they are safe to place a piece in I did and continued on iterating through the rest 
      7 //of the rows until either reaching the end or running out of safe spaces to place queens.
      8 //Then I returned all possible configurations back to my function called nQueenControl. 
      9 //The purpose of this function was to replace all of the ones with "."'s and return the final strings
     10 //to the solveNQueens function.
     11 //Time: 312ms Beats: 25.71%
     12 //Memory: 151.1MB Beats: 7.14%
     13 
     14 class Solution {
     15   List<List<String>> solveNQueens(int n) {
     16       List<String> init_mat = [];
     17       for(int i = 0 ; i < n ; ++i){
     18           init_mat.add("."*n);
     19       }
     20       List<List<String>> ret_list = nQueenControl(init_mat , 0);
     21       return ret_list;
     22   }
     23   List<List<String>> nQueenControl(List<String> matrix, int row){
     24       List<List<String>> ret_list = nQueens(matrix , row);
     25       for(int i = 0 ; i < ret_list.length ; ++i){
     26         for(int x = 0 ; x < ret_list[i].length ; ++x){
     27           ret_list[i][x] = ret_list[i][x].replaceAll('1' , '.');
     28         }
     29       }
     30       return ret_list;
     31   }
     32   List<List<String>> nQueens(List<String> matrix , int row){
     33     if(row == matrix.length){
     34       return [matrix];
     35     }
     36     List<List<String>> return_list = [];
     37     for(int i = 0 ; i < matrix[row].length ; ++i){
     38       if(matrix[row][i] == '1'){
     39         continue;
     40       }
     41       List<String> new_matrix = List.from(matrix);
     42       List<String> current_row = matrix[row].split('');
     43       current_row[i] = 'Q';
     44       new_matrix[row] = current_row.join('');
     45       int itr = 1;
     46       for(int x = row + 1 ; x < matrix.length ; ++x){
     47         List<String> next_row = matrix[x].split('');
     48         next_row[i] = '1';
     49         if(i - itr >= 0){
     50           next_row[i - itr] = '1';
     51         }
     52         if(i + itr < matrix.length){
     53           next_row[i + itr] = '1';
     54         }
     55         itr += 1;
     56         new_matrix[x] = next_row.join('');
     57       }
     58       List<List<String>> nQu = nQueens(new_matrix , row + 1);
     59       for(int i = 0 ; i < nQu.length ; ++i){
     60         return_list.add(nQu[i]);
     61       }
     62     }
     63     return return_list;
     64   }
     65 }