使用java语言、用回溯法实现N皇后问题,皇后的个数和最后的输出结果都保存在文件中,其中皇后的个数保存在QueensCount.txt文件中,输出结果保存在QueensResult.txt文件中。目录结构如下:
运行结果如下:
源码:
package gavin2;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
/**
* 回溯法--N皇后问题
* @author Gavin
*
*/
public class Queen {
static int sum; // 解数量
private static String QueensCount = "QueensCount.txt";// 皇后数量文件
private static String QueensResult = "QueensResult.txt";// 结果文件
public static void main(String[] args) {
int n = fileRead(QueensCount);
File file = null;
try {
// 将结果文件清空
file = new File(QueensResult);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileWriter fileWriter = new FileWriter(file, false);
fileWriter.write("");
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
int[] x = new int[n + 1];
long timeBefore = System.currentTimeMillis();
NQueens(n, x);
long timeAfter = System.currentTimeMillis();
try {
BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file, true)));
bf.write("如上所示,当皇后数量为"+n+"时,总共有"+sum+"种结果");
bf.newLine();
bf.write("完成算法总共用时:"+(timeAfter-timeBefore)/1000+"s");
bf.flush();
bf.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(sum);
}
/**
* 判断位置是否合法
*
* @param i
* @return
*/
public static boolean place(int x[], int i, int n) {
if (i > n) {
return false;
}
// x[]是列,i,j是行
for (int j = 1; j < i; j++) {
if (x[i] == x[j])
return false;
if ((Math.abs(j - i) == Math.abs(x[i] - x[j])))
return false;
}
return true;
}
/**
* 回溯法解N皇后问题
*
* @param n
* @param x
*/
private static void NQueens(int n, int x[]) {
int k = 1;// 当前行
x[1] = 0;// 当前列
while (k > 0) {// 对所有的行执行以下语句
x[k] = x[k] + 1;// 移动到下一列
while (!place(x, k, n) && x[k] <= n) {
x[k] = x[k] + 1;// 找到位置
}
if (x[k] <= n) {
if (k == n) {
fileWrite(QueensResult, x);
sum++;
} else {
k = k + 1;
x[k] = 0;
}
} else {
k = k - 1;
}
}
}
/**
* 读文件
*
* @param fileName
* @return
*/
public static int fileRead(String fileName) {
int count = 0;
try {
BufferedReader bf = new BufferedReader(new FileReader(fileName));
String str = bf.readLine();
bf.close();
count = Integer.parseInt(str);
return count;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return count;
}
/**
* 写文件
*
* @param fileName
* @param a
*/
public static void fileWrite(String fileName, int a[]) {
File file = new File(fileName);
BufferedWriter bw;
String str = "";
String s = "";
try {
bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file, true)));
for (int i = 1; i < a.length; i++) {
str = "x[" + i + "]=" + a[i] + " ";
s = s + str;
}
try {
System.out.println(s);
bw.append(s);
bw.newLine();
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}