[LeetCode] N-Queens II

來源:互聯網
上載者:User

標籤:style   blog   http   color   width   os   

N-Queens II

Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.

solution:

#include <iostream>#include <iomanip>#include <list>using namespace std; //board is 0 for avaliable positons, 1 for the otherint board[100][100];int queenPos[100];int N;int solutionNum = 0; void dfs(int k){if(k == N){//the last queen has been put properlysolutionNum++;return;} for(int i = 0;i < N;i++){//search for a postion for the current queenbool flag = true; if(board[i][k] == 0){list<int> lastX, lastY;//this is a good postion for the k th column queen.queenPos[k] = i;//in the ith row, kth column//change the board state to mark its attark region.for(int j = 0;j < N;j++){if(board[i][j] == 0){board[i][j] = 1;lastX.push_back(i);lastY.push_back(j);//        cout << i << " " << j << endl;}if(board[j][k] == 0){board[j][k] = 1;lastX.push_back(j);lastY.push_back(k);//cout << j << " " << k << endl;}//if(i - j >= 0 && k - j >= 0 && board[i - j][k - j] == 0){//left uplastX.push_back(i - j);lastY.push_back(k - j);board[i - j][k - j] = 1; //cout << i -j  << " " << k - j << endl;}if(i - j >= 0 && k + j < N && board[i - j][k + j] == 0){lastX.push_back(i - j);lastY.push_back(k + j);board[i - j][k + j] = 1;//cout << i -j  << " " << k + j << endl;}if(i + j < N && k - j >= 0 && board[i + j][k - j] == 0){lastX.push_back(i + j);lastY.push_back(k - j);board[i + j][k - j] = 1;//        cout << i + j  << " " << k - j << endl;}if(i + j < N && k + j < N && board[i + j][k + j] == 0){lastX.push_back(i + j);lastY.push_back(k + j);board[i + j][k + j] = 1;//        cout << i + j  << " " << k + j << endl;}} //cout << "put the " << k << " queen at " << i << " size = " << lastX.size() << endl; dfs(k + 1); //cout << "size = " << lastX.size() << endl;//back to the previous state.int num = lastX.size();for(int t = 0;t < num;t++){int x = lastX.front();lastX.pop_front();int y = lastY.front();lastY.pop_front();board[x][y] = 0;//                cout << x << " " << y << endl;}        }elsecontinue;} } //return the total number of distinct solutions for N-Queens problem.int totalNQueens(int n) {N = n;solutionNum = 0;for(int i = 0;i < 100;i++)for(int j = 0;j < 100;j++)board[i][j] = 0; dfs(0);return solutionNum;        } int main(){for(int i = 0;i < 100;i++)memset(board[i], 0, sizeof(int) * 100);memset(queenPos, 0, sizeof(int) * 100);for(int i = 1;i < 14;i++){totalNQueens(i);cout << "solutionNum = " << solutionNum << endl << endl;}return 0;}
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.