[LeetCode][JavaScript]N-Queens

來源:互聯網
上載者:User

標籤:

N-Queens

The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.

Given an integer n, return all distinct solutions to the n-queens puzzle.

Each solution contains a distinct board configuration of the n-queens‘ placement, where ‘Q‘ and ‘.‘ both indicate a queen and an empty space respectively.

For example,
There exist two distinct solutions to the 4-queens puzzle:

[ [".Q..",  // Solution 1  "...Q",  "Q...",  "..Q."], ["..Q.",  // Solution 2  "Q...",  "...Q",  ".Q.."]]

https://leetcode.com/problems/n-queens/

 

 

 

 

傳說中的N皇后問題,據說AC了這題就能掌握圖論搜尋,進而稱霸演算法,然後迎娶白富美,走上人生巔峰。

不就是dfs嘛。

一行行地做dfs。每一輪先找這一行有哪些地方可以放Queen,把這些點置成Q,迴圈遞迴這些點,最後不要忘記把預設值‘.‘寫回去。

如果到了最後一行,並且可以放得下Queen,這就是一個正確的解。

因為每一行只能放一個,如果遇到放不下的情況,說明已經錯了。

找行上的候選人的時候,第一眼看上去要找8個方向。

但首先,行上就不用找了,列方向上和2個斜的方向上只需要找上半部分,因為下面都沒開始放Q,肯定不衝突的。

因為js沒有二維數組,寫了2個方法構造數組,所以看上去代碼很長...

 1 /** 2  * @param {number} n 3  * @return {string[][]} 4  */ 5 var solveNQueens = function(n) { 6     var map = []; 7     var result = [];    8     buildMap(); 9     dfs(0);10     return result;11 12     function dfs(row){13         var i = 0, j = 0;14         var candidates = [];15         for(i = 0; i < n; i++){16             if(canPlaceQueen(row, i)){17                 candidates.push(i);18             }19         }20         if(row === n -1 && candidates.length !== 0){ //found21             map[row][candidates[0]] = ‘Q‘;22             result.push(formatMap(map));23             map[row][candidates[0]] = ‘.‘;24             return;25         }26         for(i = 0; i < candidates.length; i++){27             map[row][candidates[i]] = ‘Q‘;28             dfs(row + 1);29             map[row][candidates[i]] = ‘.‘;30         }31     }32     function canPlaceQueen(x, y){33         var cell = "";34         var i = 0, j = 0;35         for(i = 0; i < n; i++){36             cell = map[i][y];37             if(cell === ‘Q‘){38                 return false;39             } 40         }41         for(i = x, j = y; i >=0 && j >= 0; i--, j--){42             cell = map[i][j];43             if(cell === ‘Q‘){44                 return false;45             } 46         }47         for(i = x, j = y; i >=0 && j < n; i--, j++){48             cell = map[i][j];49             if(cell === ‘Q‘){50                 return false;51             } 52         }53         return true;54     }55     function buildMap(){56         var i = 0, j = 0;57         for(i = 0; i < n; i++){58             map[i] = [];59         }60         for(i = 0; i < n; i++){61             for(j = 0; j < n; j++){62                 map[i][j] = ‘.‘;63             }64         }65     }66     function formatMap(map){67         var res = [];68         var i = 0, j = 0;69         for(i = 0; i < n; i++){70             var tmp = "";71             for(j = 0; j < n; j++){72                 tmp += map[i][j];73             }74             res.push(tmp);75         }76         return res;77     }78 };

 



[LeetCode][JavaScript]N-Queens

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.