mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-04 04:02:48 +08:00
Retranslate the complete Japanese online edition from Chinese (#2008)
* Retranslate the complete Japanese online edition from Chinese * Fit Japanese homepage labels and localize heading permalinks * Point Japanese contribution instructions to the Japanese preview * Replace locale conditionals with multilingual UI tables
This commit is contained in:
@@ -9,10 +9,10 @@ package chapter_backtracking;
|
||||
import java.util.*;
|
||||
|
||||
public class n_queens {
|
||||
/* バックトラッキング:N クイーン */
|
||||
/* バックトラッキング:n クイーン */
|
||||
public static 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) {
|
||||
@@ -21,26 +21,26 @@ public class n_queens {
|
||||
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 クイーンを解く */
|
||||
/* n クイーン問題を解く */
|
||||
public static List<List<List<String>>> nQueens(int n) {
|
||||
// n*n の盤面を初期化する。'Q' はクイーン、'#' は空きマスを表す
|
||||
List<List<String>> state = new ArrayList<>();
|
||||
@@ -51,9 +51,9 @@ public class n_queens {
|
||||
}
|
||||
state.add(row);
|
||||
}
|
||||
boolean[] cols = new boolean[n]; // 列にクイーンがあるか記録
|
||||
boolean[] diags1 = new boolean[2 * n - 1]; // 主対角線にクイーンがあるかを記録
|
||||
boolean[] diags2 = new boolean[2 * n - 1]; // 副対角線にクイーンがあるかを記録
|
||||
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);
|
||||
@@ -65,7 +65,7 @@ public class n_queens {
|
||||
int n = 4;
|
||||
List<List<List<String>>> res = nQueens(n);
|
||||
|
||||
System.out.println("盤面の縦横サイズは " + n);
|
||||
System.out.println("入力した盤面の縦横の長さは " + n);
|
||||
System.out.println("クイーンの配置方法は全部で " + res.size() + " 通り");
|
||||
for (List<List<String>> state : res) {
|
||||
System.out.println("--------------------");
|
||||
|
||||
Reference in New Issue
Block a user