N皇后
leetcode 51
The n-queens puzzle is the problem of placing n queens on an n x 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.
Example 1
Input: n = 4
Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above
代码
class Solution {
public:
vector<vector<string>> res;
vector<bool> col;
vector<bool> dia1;
vector<bool> dia2;
vector<vector<string>> solveNQueens(int n) {
if (n <= 0) return res;
col = vector<bool> (n, false);
dia1 = vector<bool> (2*n-1, false);
dia2 = vector<bool> (2*n-1, false);
vector<int> rows;
queens(n, 0, rows);
return res;
}
//
void queens(int n, int index, vector<int> &rows) {
if (index >= n) {
vector<string> oneRes = parseRows(rows);
res.push_back(oneRes);
return;
}
for (int i = 0; i < n; i++) {
//判断是否有冲突
if (!col[i] && !dia1[i+index] && !dia2[index-i+n-1]) {
col[i] = true;
dia1[i+index] = true;
dia2[index-i+n-1] = true;
rows.push_back(i);
queens(n, index+1, rows);
rows.pop_back();
col[i] = false;
dia1[i+index] = false;
dia2[index-i+n-1] = false;
}
}
}
vector<string> parseRows(vector<int> &rows) {
int len = rows.size();
vector<string> oneRes;
string line = "";
for (int i = 0; i < len; i++) {
line = "";
for (int j = 0; j < len; j++) {
if (rows[i] == j) {
line = line + "Q";
} else {
line = line + ".";
}
}
oneRes.push_back(line);
}
return oneRes;
}
};
数独问题
leetcode 37
Write a program to solve a Sudoku puzzle by filling the empty cells.
A sudoku solution must satisfy all of the following rules:
Each of the digits 1-9 must occur exactly once in each row.
Each of the digits 1-9 must occur exactly once in each column.
Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.
The ‘.’ character indicates empty cells.
Example 1
Input: board = [["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]]
Output: [["5","3","4","6","7","8","9","1","2"],["6","7","2","1","9","5","3","4","8"],["1","9","8","3","4","2","5","6","7"],["8","5","9","7","6","1","4","2","3"],["4","2","6","8","5","3","7","9","1"],["7","1","3","9","2","4","8","5","6"],["9","6","1","5","3","7","2","8","4"],["2","8","7","4","1","9","6","3","5"],["3","4","5","2","8","6","1","7","9"]]
代码
class Solution {
public:
void solveSudoku(vector<vector<char>>& board) {
bool flag = _solveSudoku(board);
return;
}
//
bool _solveSudoku(vector<vector<char>> &board) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] == '.') {
for (char c = '1'; c <= '9'; c++) {
board[i][j]=c;
if(isValid(board,c,i,j) && _solveSudoku(board)) return true;
board[i][j]='.';
}
//递归结束
return false;
}
}
}
return true;
}
bool isValid(vector<vector<char>> &board, char c, int x, int y) {
for (int i = 0; i < 9; i++) if (i!=x && c == board[i][y]) return false;
for (int j = 0; j < 9; j++) if (j!=y && c == board[x][j]) return false;
int wid = 3*(x/3);
int hig = 3*(y/3);
for (int i = wid; i < wid+3; i++) {
for (int j = hig; j < hig+3; j++) {
if (i!=x && j!=y && c == board[i][j]) return false;
}
}
return true;
}
};