1. 程式人生 > 其它 >Codeforces Round #695 (Div. 2)

Codeforces Round #695 (Div. 2)

技術標籤:Leetcodeleetcode演算法

n皇后

//n皇后
/*給出一個整數n,返回n皇后問題的所有擺放方案 */
#include<string>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;

class Solution {
public:
	//儘量不要首先考慮作太多轉換,會造成複雜度提高,可以的話儘量直接去設初值去處理
	bool isValid(vector<string>& cur, int&
n, int row, int col) { for (int i = 0; i < row; ++i) { if (cur[i][col] == 'Q') return false; } for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; --i, --j) { if (cur[i][j] == 'Q') return false; } for (int i = row - 1, j = col + 1; i >= 0 && j <
n; --i, ++j) { if (cur[i][j] == 'Q') return false; } return true; } void dfs(vector<vector<string>> &res, vector<string>& cur, int& n, int row) { if (row == n) { res.push_back(cur); return; } for (int i = 0; i < n; ++i) { if (isValid
(cur, n, row, i)) { cur[row][i] = 'Q'; dfs(res, cur, n, row + 1); cur[row][i] = '.'; } } } vector<vector<string> > solveNQueens(int n) { vector<vector<string>> res; if (n == 0) return res; vector<string> cur(n, string(n, '.')); dfs(res, cur, n, 0); return res; } };