13.4 n-퀸 문제¶
문제
체스 규칙에서 퀸은 같은 행, 열 또는 대각선에 있는 말을 공격할 수 있다. 퀸 \(n\) 개와 \(n \times n\) 크기의 체스판이 주어졌을 때, 퀸들이 서로 공격할 수 없도록 배치하는 방법을 찾아라.
아래 그림(13-15)과 같이 \(n = 4\)이면 해는 두 개다. 백트래킹 관점에서 \(n \times n\) 체스판의 \(n^2\) 개 칸은 모든 선택지 choices를 제공한다. 퀸을 하나씩 놓는 동안 체스판은 계속 바뀌며, 각 시점의 체스판이 상태 state다.

그림 13-15 4-퀸 문제의 해
아래 그림(13-16)은 세 가지 제약 조건을 보여 준다. 여러 퀸이 같은 행, 같은 열, 같은 대각선에 있으면 안 된다. 대각선은 주대각선 방향 \과 부대각선 방향 /의 두 종류로 나뉜다.

그림 13-16 n-퀸 문제의 제약 조건
1. 한 행씩 배치하는 전략¶
퀸 수와 체스판의 행 수가 모두 \(n\)이므로 각 행에는 퀸을 정확히 하나씩 놓아야 한다는 사실을 쉽게 도출할 수 있다.
따라서 첫 행부터 마지막 행까지 매 행에 퀸 하나씩 놓는 전략을 사용할 수 있다.
아래 그림(13-17)은 4-퀸 문제에서 한 행씩 배치하는 과정이다. 공간 제약 때문에 첫 행의 탐색 가지 중 하나만 펼쳤으며, 열과 대각선 제약을 만족하지 않는 배치는 모두 가지치기했다.

그림 13-17 한 행씩 배치하는 전략
본질적으로 행별 배치 전략 자체가 가지치기 역할을 한다. 같은 행에 여러 퀸이 놓이는 모든 탐색 가지를 피하기 때문이다.
2. 열과 대각선 가지치기¶
열 제약을 만족하기 위해 길이 \(n\)의 불리언 배열 cols에 각 열의 퀸 존재 여부를 기록할 수 있다. 배치를 결정하기 전에 cols로 이미 퀸이 있는 열을 가지치기하고, 백트래킹 중 cols의 상태를 동적으로 갱신한다.
참고
행렬의 시작점은 왼쪽 위이며, 행 인덱스는 아래로 갈수록, 열 인덱스는 오른쪽으로 갈수록 커진다.
대각선 제약은 어떻게 처리할까? 한 칸의 행과 열 인덱스를 \((row, col)\)이라고 하자. 주대각선 방향의 대각선 하나를 고르면, 그 위의 모든 칸에서 행 인덱스와 열 인덱스의 차가 같다. 즉, 같은 주대각선 방향 대각선에서는 \(row - col\)이 일정하다.
따라서 두 칸이 \(row_1 - col_1 = row_2 - col_2\)를 만족하면 반드시 같은 주대각선 방향 대각선에 있다. 이 규칙을 이용하여 아래 그림(13-18)의 배열 diags1에 각 대각선의 퀸 존재 여부를 기록할 수 있다.
마찬가지로 같은 부대각선 방향 대각선의 모든 칸에서는 \(row + col\)이 일정하다. 배열 diags2를 이용하여 이 방향의 제약도 처리할 수 있다.

그림 13-18 열 제약과 대각선 제약 처리
3. 코드 구현¶
\(n\) 차 정방행렬에서 \(row - col\)의 범위는 \([-n + 1, n - 1]\), \(row + col\)의 범위는 \([0, 2n - 2]\)이다. 따라서 두 방향의 대각선 수는 각각 \(2n - 1\) 개이며, 배열 diags1과 diags2의 길이도 모두 \(2n - 1\)이다.
def backtrack(
row: int,
n: int,
state: list[list[str]],
res: list[list[list[str]]],
cols: list[bool],
diags1: list[bool],
diags2: list[bool],
):
"""백트래킹 알고리즘: n-퀸"""
# 모든 행에 배치했으면 해를 기록한다
if row == n:
res.append([list(row) for row in state])
return
# 모든 열을 순회한다
for col in range(n):
# 해당 칸이 속한 주대각선과 부대각선을 계산한다
diag1 = row - col + n - 1
diag2 = row + col
# 가지치기: 해당 칸이 속한 열, 주대각선, 부대각선에 퀸이 있으면 안 된다
if not cols[col] and not diags1[diag1] and not diags2[diag2]:
# 시도: 해당 칸에 퀸을 놓는다
state[row][col] = "Q"
cols[col] = diags1[diag1] = diags2[diag2] = True
# 다음 행에 배치한다
backtrack(row + 1, n, state, res, cols, diags1, diags2)
# 되돌리기: 해당 칸을 빈칸으로 되돌린다
state[row][col] = "#"
cols[col] = diags1[diag1] = diags2[diag2] = False
def n_queens(n: int) -> list[list[list[str]]]:
"""n-퀸 문제 풀기"""
# n*n 크기의 체스판을 초기화한다. 'Q'는 퀸, '#'은 빈칸을 나타낸다
state = [["#" for _ in range(n)] for _ in range(n)]
cols = [False] * n # 열에 퀸이 있는지 기록한다
diags1 = [False] * (2 * n - 1) # 주대각선에 퀸이 있는지 기록한다
diags2 = [False] * (2 * n - 1) # 부대각선에 퀸이 있는지 기록한다
res = []
backtrack(0, n, state, res, cols, diags1, diags2)
return res
/* 백트래킹 알고리즘: n-퀸 */
void backtrack(int row, int n, vector<vector<string>> &state, vector<vector<vector<string>>> &res, vector<bool> &cols,
vector<bool> &diags1, vector<bool> &diags2) {
// 모든 행에 배치했으면 해를 기록한다
if (row == n) {
res.push_back(state);
return;
}
// 모든 열을 순회한다
for (int col = 0; col < n; col++) {
// 해당 칸이 속한 주대각선과 부대각선을 계산한다
int diag1 = row - col + n - 1;
int diag2 = row + col;
// 가지치기: 해당 칸이 속한 열, 주대각선, 부대각선에 퀸이 있으면 안 된다
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// 시도: 해당 칸에 퀸을 놓는다
state[row][col] = "Q";
cols[col] = diags1[diag1] = diags2[diag2] = true;
// 다음 행에 배치한다
backtrack(row + 1, n, state, res, cols, diags1, diags2);
// 되돌리기: 해당 칸을 빈칸으로 되돌린다
state[row][col] = "#";
cols[col] = diags1[diag1] = diags2[diag2] = false;
}
}
}
/* n-퀸 문제 풀기 */
vector<vector<vector<string>>> nQueens(int n) {
// n*n 크기의 체스판을 초기화한다. 'Q'는 퀸, '#'은 빈칸을 나타낸다
vector<vector<string>> state(n, vector<string>(n, "#"));
vector<bool> cols(n, false); // 열에 퀸이 있는지 기록한다
vector<bool> diags1(2 * n - 1, false); // 주대각선에 퀸이 있는지 기록한다
vector<bool> diags2(2 * n - 1, false); // 부대각선에 퀸이 있는지 기록한다
vector<vector<vector<string>>> res;
backtrack(0, n, state, res, cols, diags1, diags2);
return res;
}
/* 백트래킹 알고리즘: n-퀸 */
void backtrack(int row, int n, List<List<String>> state, List<List<List<String>>> res,
boolean[] cols, boolean[] diags1, boolean[] diags2) {
// 모든 행에 배치했으면 해를 기록한다
if (row == n) {
List<List<String>> copyState = new ArrayList<>();
for (List<String> sRow : state) {
copyState.add(new ArrayList<>(sRow));
}
res.add(copyState);
return;
}
// 모든 열을 순회한다
for (int col = 0; col < n; col++) {
// 해당 칸이 속한 주대각선과 부대각선을 계산한다
int diag1 = row - col + n - 1;
int diag2 = row + col;
// 가지치기: 해당 칸이 속한 열, 주대각선, 부대각선에 퀸이 있으면 안 된다
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// 시도: 해당 칸에 퀸을 놓는다
state.get(row).set(col, "Q");
cols[col] = diags1[diag1] = diags2[diag2] = true;
// 다음 행에 배치한다
backtrack(row + 1, n, state, res, cols, diags1, diags2);
// 되돌리기: 해당 칸을 빈칸으로 되돌린다
state.get(row).set(col, "#");
cols[col] = diags1[diag1] = diags2[diag2] = false;
}
}
}
/* n-퀸 문제 풀기 */
List<List<List<String>>> nQueens(int n) {
// n*n 크기의 체스판을 초기화한다. 'Q'는 퀸, '#'은 빈칸을 나타낸다
List<List<String>> state = new ArrayList<>();
for (int i = 0; i < n; i++) {
List<String> row = new ArrayList<>();
for (int j = 0; j < n; j++) {
row.add("#");
}
state.add(row);
}
boolean[] cols = new boolean[n]; // 열에 퀸이 있는지 기록한다
boolean[] diags1 = new boolean[2 * n - 1]; // 주대각선에 퀸이 있는지 기록한다
boolean[] diags2 = new boolean[2 * n - 1]; // 부대각선에 퀸이 있는지 기록한다
List<List<List<String>>> res = new ArrayList<>();
backtrack(0, n, state, res, cols, diags1, diags2);
return res;
}
/* 백트래킹 알고리즘: n-퀸 */
void Backtrack(int row, int n, List<List<string>> state, List<List<List<string>>> res,
bool[] cols, bool[] diags1, bool[] diags2) {
// 모든 행에 배치했으면 해를 기록한다
if (row == n) {
List<List<string>> copyState = [];
foreach (List<string> sRow in state) {
copyState.Add(new List<string>(sRow));
}
res.Add(copyState);
return;
}
// 모든 열을 순회한다
for (int col = 0; col < n; col++) {
// 해당 칸이 속한 주대각선과 부대각선을 계산한다
int diag1 = row - col + n - 1;
int diag2 = row + col;
// 가지치기: 해당 칸이 속한 열, 주대각선, 부대각선에 퀸이 있으면 안 된다
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// 시도: 해당 칸에 퀸을 놓는다
state[row][col] = "Q";
cols[col] = diags1[diag1] = diags2[diag2] = true;
// 다음 행에 배치한다
Backtrack(row + 1, n, state, res, cols, diags1, diags2);
// 되돌리기: 해당 칸을 빈칸으로 되돌린다
state[row][col] = "#";
cols[col] = diags1[diag1] = diags2[diag2] = false;
}
}
}
/* n-퀸 문제 풀기 */
List<List<List<string>>> NQueens(int n) {
// n*n 크기의 체스판을 초기화한다. 'Q'는 퀸, '#'은 빈칸을 나타낸다
List<List<string>> state = [];
for (int i = 0; i < n; i++) {
List<string> row = [];
for (int j = 0; j < n; j++) {
row.Add("#");
}
state.Add(row);
}
bool[] cols = new bool[n]; // 열에 퀸이 있는지 기록한다
bool[] diags1 = new bool[2 * n - 1]; // 주대각선에 퀸이 있는지 기록한다
bool[] diags2 = new bool[2 * n - 1]; // 부대각선에 퀸이 있는지 기록한다
List<List<List<string>>> res = [];
Backtrack(0, n, state, res, cols, diags1, diags2);
return res;
}
/* 백트래킹 알고리즘: n-퀸 */
func backtrack(row, n int, state *[][]string, res *[][][]string, cols, diags1, diags2 *[]bool) {
// 모든 행에 배치했으면 해를 기록한다
if row == n {
newState := make([][]string, len(*state))
for i, _ := range newState {
newState[i] = make([]string, len((*state)[0]))
copy(newState[i], (*state)[i])
}
*res = append(*res, newState)
return
}
// 모든 열을 순회한다
for col := 0; col < n; col++ {
// 해당 칸이 속한 주대각선과 부대각선을 계산한다
diag1 := row - col + n - 1
diag2 := row + col
// 가지치기: 해당 칸이 속한 열, 주대각선, 부대각선에 퀸이 있으면 안 된다
if !(*cols)[col] && !(*diags1)[diag1] && !(*diags2)[diag2] {
// 시도: 해당 칸에 퀸을 놓는다
(*state)[row][col] = "Q"
(*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = true, true, true
// 다음 행에 배치한다
backtrack(row+1, n, state, res, cols, diags1, diags2)
// 되돌리기: 해당 칸을 빈칸으로 되돌린다
(*state)[row][col] = "#"
(*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = false, false, false
}
}
}
/* n-퀸 문제 풀기 */
func nQueens(n int) [][][]string {
// n*n 크기의 체스판을 초기화한다. 'Q'는 퀸, '#'은 빈칸을 나타낸다
state := make([][]string, n)
for i := 0; i < n; i++ {
row := make([]string, n)
for i := 0; i < n; i++ {
row[i] = "#"
}
state[i] = row
}
// 열에 퀸이 있는지 기록한다
cols := make([]bool, n)
diags1 := make([]bool, 2*n-1)
diags2 := make([]bool, 2*n-1)
res := make([][][]string, 0)
backtrack(0, n, &state, &res, &cols, &diags1, &diags2)
return res
}
/* 백트래킹 알고리즘: n-퀸 */
func backtrack(row: Int, n: Int, state: inout [[String]], res: inout [[[String]]], cols: inout [Bool], diags1: inout [Bool], diags2: inout [Bool]) {
// 모든 행에 배치했으면 해를 기록한다
if row == n {
res.append(state)
return
}
// 모든 열을 순회한다
for col in 0 ..< n {
// 해당 칸이 속한 주대각선과 부대각선을 계산한다
let diag1 = row - col + n - 1
let diag2 = row + col
// 가지치기: 해당 칸이 속한 열, 주대각선, 부대각선에 퀸이 있으면 안 된다
if !cols[col] && !diags1[diag1] && !diags2[diag2] {
// 시도: 해당 칸에 퀸을 놓는다
state[row][col] = "Q"
cols[col] = true
diags1[diag1] = true
diags2[diag2] = true
// 다음 행에 배치한다
backtrack(row: row + 1, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)
// 되돌리기: 해당 칸을 빈칸으로 되돌린다
state[row][col] = "#"
cols[col] = false
diags1[diag1] = false
diags2[diag2] = false
}
}
}
/* n-퀸 문제 풀기 */
func nQueens(n: Int) -> [[[String]]] {
// n*n 크기의 체스판을 초기화한다. 'Q'는 퀸, '#'은 빈칸을 나타낸다
var state = Array(repeating: Array(repeating: "#", count: n), count: n)
var cols = Array(repeating: false, count: n) // 열에 퀸이 있는지 기록한다
var diags1 = Array(repeating: false, count: 2 * n - 1) // 주대각선에 퀸이 있는지 기록한다
var diags2 = Array(repeating: false, count: 2 * n - 1) // 부대각선에 퀸이 있는지 기록한다
var res: [[[String]]] = []
backtrack(row: 0, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)
return res
}
/* 백트래킹 알고리즘: n-퀸 */
function backtrack(row, n, state, res, cols, diags1, diags2) {
// 모든 행에 배치했으면 해를 기록한다
if (row === n) {
res.push(state.map((row) => row.slice()));
return;
}
// 모든 열을 순회한다
for (let col = 0; col < n; col++) {
// 해당 칸이 속한 주대각선과 부대각선을 계산한다
const diag1 = row - col + n - 1;
const diag2 = row + col;
// 가지치기: 해당 칸이 속한 열, 주대각선, 부대각선에 퀸이 있으면 안 된다
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// 시도: 해당 칸에 퀸을 놓는다
state[row][col] = 'Q';
cols[col] = diags1[diag1] = diags2[diag2] = true;
// 다음 행에 배치한다
backtrack(row + 1, n, state, res, cols, diags1, diags2);
// 되돌리기: 해당 칸을 빈칸으로 되돌린다
state[row][col] = '#';
cols[col] = diags1[diag1] = diags2[diag2] = false;
}
}
}
/* n-퀸 문제 풀기 */
function nQueens(n) {
// n*n 크기의 체스판을 초기화한다. 'Q'는 퀸, '#'은 빈칸을 나타낸다
const state = Array.from({ length: n }, () => Array(n).fill('#'));
const cols = Array(n).fill(false); // 열에 퀸이 있는지 기록한다
const diags1 = Array(2 * n - 1).fill(false); // 주대각선에 퀸이 있는지 기록한다
const diags2 = Array(2 * n - 1).fill(false); // 부대각선에 퀸이 있는지 기록한다
const res = [];
backtrack(0, n, state, res, cols, diags1, diags2);
return res;
}
/* 백트래킹 알고리즘: n-퀸 */
function backtrack(
row: number,
n: number,
state: string[][],
res: string[][][],
cols: boolean[],
diags1: boolean[],
diags2: boolean[]
): void {
// 모든 행에 배치했으면 해를 기록한다
if (row === n) {
res.push(state.map((row) => row.slice()));
return;
}
// 모든 열을 순회한다
for (let col = 0; col < n; col++) {
// 해당 칸이 속한 주대각선과 부대각선을 계산한다
const diag1 = row - col + n - 1;
const diag2 = row + col;
// 가지치기: 해당 칸이 속한 열, 주대각선, 부대각선에 퀸이 있으면 안 된다
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// 시도: 해당 칸에 퀸을 놓는다
state[row][col] = 'Q';
cols[col] = diags1[diag1] = diags2[diag2] = true;
// 다음 행에 배치한다
backtrack(row + 1, n, state, res, cols, diags1, diags2);
// 되돌리기: 해당 칸을 빈칸으로 되돌린다
state[row][col] = '#';
cols[col] = diags1[diag1] = diags2[diag2] = false;
}
}
}
/* n-퀸 문제 풀기 */
function nQueens(n: number): string[][][] {
// n*n 크기의 체스판을 초기화한다. 'Q'는 퀸, '#'은 빈칸을 나타낸다
const state = Array.from({ length: n }, () => Array(n).fill('#'));
const cols = Array(n).fill(false); // 열에 퀸이 있는지 기록한다
const diags1 = Array(2 * n - 1).fill(false); // 주대각선에 퀸이 있는지 기록한다
const diags2 = Array(2 * n - 1).fill(false); // 부대각선에 퀸이 있는지 기록한다
const res: string[][][] = [];
backtrack(0, n, state, res, cols, diags1, diags2);
return res;
}
/* 백트래킹 알고리즘: n-퀸 */
void backtrack(
int row,
int n,
List<List<String>> state,
List<List<List<String>>> res,
List<bool> cols,
List<bool> diags1,
List<bool> diags2,
) {
// 모든 행에 배치했으면 해를 기록한다
if (row == n) {
List<List<String>> copyState = [];
for (List<String> sRow in state) {
copyState.add(List.from(sRow));
}
res.add(copyState);
return;
}
// 모든 열을 순회한다
for (int col = 0; col < n; col++) {
// 해당 칸이 속한 주대각선과 부대각선을 계산한다
int diag1 = row - col + n - 1;
int diag2 = row + col;
// 가지치기: 해당 칸이 속한 열, 주대각선, 부대각선에 퀸이 있으면 안 된다
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// 시도: 해당 칸에 퀸을 놓는다
state[row][col] = "Q";
cols[col] = true;
diags1[diag1] = true;
diags2[diag2] = true;
// 다음 행에 배치한다
backtrack(row + 1, n, state, res, cols, diags1, diags2);
// 되돌리기: 해당 칸을 빈칸으로 되돌린다
state[row][col] = "#";
cols[col] = false;
diags1[diag1] = false;
diags2[diag2] = false;
}
}
}
/* n-퀸 문제 풀기 */
List<List<List<String>>> nQueens(int n) {
// n*n 크기의 체스판을 초기화한다. 'Q'는 퀸, '#'은 빈칸을 나타낸다
List<List<String>> state = List.generate(n, (index) => List.filled(n, "#"));
List<bool> cols = List.filled(n, false); // 열에 퀸이 있는지 기록한다
List<bool> diags1 = List.filled(2 * n - 1, false); // 주대각선에 퀸이 있는지 기록한다
List<bool> diags2 = List.filled(2 * n - 1, false); // 부대각선에 퀸이 있는지 기록한다
List<List<List<String>>> res = [];
backtrack(0, n, state, res, cols, diags1, diags2);
return res;
}
/* 백트래킹 알고리즘: n-퀸 */
fn backtrack(
row: usize,
n: usize,
state: &mut Vec<Vec<String>>,
res: &mut Vec<Vec<Vec<String>>>,
cols: &mut [bool],
diags1: &mut [bool],
diags2: &mut [bool],
) {
// 모든 행에 배치했으면 해를 기록한다
if row == n {
res.push(state.clone());
return;
}
// 모든 열을 순회한다
for col in 0..n {
// 해당 칸이 속한 주대각선과 부대각선을 계산한다
let diag1 = row + n - 1 - col;
let diag2 = row + col;
// 가지치기: 해당 칸이 속한 열, 주대각선, 부대각선에 퀸이 있으면 안 된다
if !cols[col] && !diags1[diag1] && !diags2[diag2] {
// 시도: 해당 칸에 퀸을 놓는다
state[row][col] = "Q".into();
(cols[col], diags1[diag1], diags2[diag2]) = (true, true, true);
// 다음 행에 배치한다
backtrack(row + 1, n, state, res, cols, diags1, diags2);
// 되돌리기: 해당 칸을 빈칸으로 되돌린다
state[row][col] = "#".into();
(cols[col], diags1[diag1], diags2[diag2]) = (false, false, false);
}
}
}
/* n-퀸 문제 풀기 */
fn n_queens(n: usize) -> Vec<Vec<Vec<String>>> {
// n*n 크기의 체스판을 초기화한다. 'Q'는 퀸, '#'은 빈칸을 나타낸다
let mut state: Vec<Vec<String>> = vec![vec!["#".to_string(); n]; n];
let mut cols = vec![false; n]; // 열에 퀸이 있는지 기록한다
let mut diags1 = vec![false; 2 * n - 1]; // 주대각선에 퀸이 있는지 기록한다
let mut diags2 = vec![false; 2 * n - 1]; // 부대각선에 퀸이 있는지 기록한다
let mut res: Vec<Vec<Vec<String>>> = Vec::new();
backtrack(
0,
n,
&mut state,
&mut res,
&mut cols,
&mut diags1,
&mut diags2,
);
res
}
/* 백트래킹 알고리즘: n-퀸 */
void backtrack(int row, int n, char state[MAX_SIZE][MAX_SIZE], char ***res, int *resSize, bool cols[MAX_SIZE],
bool diags1[2 * MAX_SIZE - 1], bool diags2[2 * MAX_SIZE - 1]) {
// 모든 행에 배치했으면 해를 기록한다
if (row == n) {
res[*resSize] = (char **)malloc(sizeof(char *) * n);
for (int i = 0; i < n; ++i) {
res[*resSize][i] = (char *)malloc(sizeof(char) * (n + 1));
strcpy(res[*resSize][i], state[i]);
}
(*resSize)++;
return;
}
// 모든 열을 순회한다
for (int col = 0; col < n; col++) {
// 해당 칸이 속한 주대각선과 부대각선을 계산한다
int diag1 = row - col + n - 1;
int diag2 = row + col;
// 가지치기: 해당 칸이 속한 열, 주대각선, 부대각선에 퀸이 있으면 안 된다
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// 시도: 해당 칸에 퀸을 놓는다
state[row][col] = 'Q';
cols[col] = diags1[diag1] = diags2[diag2] = true;
// 다음 행에 배치한다
backtrack(row + 1, n, state, res, resSize, cols, diags1, diags2);
// 되돌리기: 해당 칸을 빈칸으로 되돌린다
state[row][col] = '#';
cols[col] = diags1[diag1] = diags2[diag2] = false;
}
}
}
/* n-퀸 문제 풀기 */
char ***nQueens(int n, int *returnSize) {
char state[MAX_SIZE][MAX_SIZE];
// n*n 크기의 체스판을 초기화한다. 'Q'는 퀸, '#'은 빈칸을 나타낸다
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
state[i][j] = '#';
}
state[i][n] = '\0';
}
bool cols[MAX_SIZE] = {false}; // 열에 퀸이 있는지 기록한다
bool diags1[2 * MAX_SIZE - 1] = {false}; // 주대각선에 퀸이 있는지 기록한다
bool diags2[2 * MAX_SIZE - 1] = {false}; // 부대각선에 퀸이 있는지 기록한다
char ***res = (char ***)malloc(sizeof(char **) * MAX_SIZE);
*returnSize = 0;
backtrack(0, n, state, res, returnSize, cols, diags1, diags2);
return res;
}
/* 백트래킹 알고리즘: n-퀸 */
fun backtrack(
row: Int,
n: Int,
state: MutableList<MutableList<String>>,
res: MutableList<MutableList<MutableList<String>>?>,
cols: BooleanArray,
diags1: BooleanArray,
diags2: BooleanArray
) {
// 모든 행에 배치했으면 해를 기록한다
if (row == n) {
val copyState = mutableListOf<MutableList<String>>()
for (sRow in state) {
copyState.add(sRow.toMutableList())
}
res.add(copyState)
return
}
// 모든 열을 순회한다
for (col in 0..<n) {
// 해당 칸이 속한 주대각선과 부대각선을 계산한다
val diag1 = row - col + n - 1
val diag2 = row + col
// 가지치기: 해당 칸이 속한 열, 주대각선, 부대각선에 퀸이 있으면 안 된다
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// 시도: 해당 칸에 퀸을 놓는다
state[row][col] = "Q"
diags2[diag2] = true
diags1[diag1] = diags2[diag2]
cols[col] = diags1[diag1]
// 다음 행에 배치한다
backtrack(row + 1, n, state, res, cols, diags1, diags2)
// 되돌리기: 해당 칸을 빈칸으로 되돌린다
state[row][col] = "#"
diags2[diag2] = false
diags1[diag1] = diags2[diag2]
cols[col] = diags1[diag1]
}
}
}
/* n-퀸 문제 풀기 */
fun nQueens(n: Int): MutableList<MutableList<MutableList<String>>?> {
// n*n 크기의 체스판을 초기화한다. 'Q'는 퀸, '#'은 빈칸을 나타낸다
val state = mutableListOf<MutableList<String>>()
for (i in 0..<n) {
val row = mutableListOf<String>()
for (j in 0..<n) {
row.add("#")
}
state.add(row)
}
val cols = BooleanArray(n) // 열에 퀸이 있는지 기록한다
val diags1 = BooleanArray(2 * n - 1) // 주대각선에 퀸이 있는지 기록한다
val diags2 = BooleanArray(2 * n - 1) // 부대각선에 퀸이 있는지 기록한다
val res = mutableListOf<MutableList<MutableList<String>>?>()
backtrack(0, n, state, res, cols, diags1, diags2)
return res
}
### 백트래킹 알고리즘: n-퀸 ###
def backtrack(row, n, state, res, cols, diags1, diags2)
# 모든 행에 배치했으면 해를 기록한다
if row == n
res << state.map { |row| row.dup }
return
end
# 모든 열을 순회한다
for col in 0...n
# 해당 칸이 속한 주대각선과 부대각선을 계산한다
diag1 = row - col + n - 1
diag2 = row + col
# 가지치기: 해당 칸이 속한 열, 주대각선, 부대각선에 퀸이 있으면 안 된다
if !cols[col] && !diags1[diag1] && !diags2[diag2]
# 시도: 해당 칸에 퀸을 놓는다
state[row][col] = "Q"
cols[col] = diags1[diag1] = diags2[diag2] = true
# 다음 행에 배치한다
backtrack(row + 1, n, state, res, cols, diags1, diags2)
# 되돌리기: 해당 칸을 빈칸으로 되돌린다
state[row][col] = "#"
cols[col] = diags1[diag1] = diags2[diag2] = false
end
end
end
### n-퀸 문제 풀기 ###
def n_queens(n)
# n*n 크기의 체스판을 초기화한다. 'Q'는 퀸, '#'은 빈칸을 나타낸다
state = Array.new(n) { Array.new(n, "#") }
cols = Array.new(n, false) # 열에 퀸이 있는지 기록한다
diags1 = Array.new(2 * n - 1, false) # 주대각선에 퀸이 있는지 기록한다
diags2 = Array.new(2 * n - 1, false) # 부대각선에 퀸이 있는지 기록한다
res = []
backtrack(0, n, state, res, cols, diags1, diags2)
res
end
코드 시각화
행별로 \(n\) 번 배치할 때 열 제약을 고려하면 첫 행부터 마지막 행까지 선택지 수가 \(n\), \(n-1\), \(\dots\), \(2\), \(1\)이므로 \(O(n!)\) 시간이 걸린다. 해를 기록할 때 행렬 state를 복사하여 res에 추가해야 하며, 복사에는 \(O(n^2)\) 시간이 든다. 따라서 전체 시간 복잡도는 \(O(n! \cdot n^2)\)이다. 실제로는 대각선 제약에 따른 가지치기도 탐색 공간을 크게 줄이므로 위 복잡도보다 탐색 효율이 좋은 경우가 많다.
배열 state는 \(O(n^2)\) 공간을, cols, diags1, diags2는 각각 \(O(n)\) 공간을 사용한다. 최대 재귀 깊이가 \(n\)이므로 스택 프레임 공간은 \(O(n)\)이다. 따라서 공간 복잡도는 \(O(n^2)\)이다.