解决方案:
1.编译,运行程序,在debug目录下扮镇生成厅做粗 test.exe文件(假设你的文件名是test.exe)
2.把test.exe copy到d:下
3.开始,运行,cmd
4.d: 回车
5.test.exe >result.txt 回车
6.在d:下result.txt文件中就有你的结果了
记得把test.exe换成你的程序名!
如胡饥果不行的话加我QQ:331012005
(1) 头文件eigqueprob.h
#include
#define N 8 /* N 表示皇后的个数 */
/* 用来定义答案的结构体 */
typedef struct
{
int line/* 答案的行号 */
int row/* 答案的列号 */
}ANSWER_TYPE
/* 用来定义某个位置是否被占用 */
typedef enum
{
notoccued = 0, /* 没被占用滚碰 */
occued = 1 /* 被占用 */
}IFOCCUED
/* 该列是否已经有其他皇后占用 */
IFOCCUED rowoccu[N]
/* 左上-右下对角位置已经有其他皇后占用 */
IFOCCUED LeftTop_RightDown[2*N-1]
/* 右上-左下对角位置已经有其他皇后占用*/
IFOCCUED RightTop_LefttDown[2*N-1]
/* 最后的答案记录 */
ANSWER_TYPE answer[N]
(2)主程序文件
#include "eigqueprob.h"
/* 寻找下一行占用的位置 */
void nextline(int LineIndex)
{
static asnnum = 0/* 统计答案的个数州盯 */
int RowIndex = 0/* 列索引 */
int PrintIndex = 0
/* 按列开始遍历 */
for (RowIndex=0RowIndex {
/* 如果列和两个对角线上都没有被占用的话,则占用该位置 */
if ( ( notoccued == rowoccu[RowIndex] )\
&&( notoccued == LeftTop_RightDown[LineIndex-RowIndex+N-1] )\
&&( notoccued == RightTop_LefttDown[LineIndex+RowIndex] ) )
{
/* 标记已占用 */
rowoccu[RowIndex] = occued
LeftTop_RightDown[LineIndex-RowIndex+N-1] = occued
RightTop_LefttDown[LineIndex+RowIndex] = occued
/* 标记被占用的行、列号 */
answer[LineIndex].line = LineIndex
answer[LineIndex].row = RowIndex
/* 如果不是最后一行,继续找下一行可以占用的册备和位置 */
if ( (N-1) >LineIndex )
{
nextline(LineIndex+1)
}
/* 如果已经到了最后一行,输出结果 */
else
{
asnnum++
printf("\nThe %dth answer is :",asnnum)
for (PrintIndex=0PrintIndex {
printf("(%d,%d) ",answer[PrintIndex].line+1,answer[PrintIndex].row+1)
}
/* 每10个答案一组,与其他组隔两行 */
if ((asnnum % 10) == 0)
printf("\n\n")
}
/* 清空占用标志,寻找下一组解 */
rowoccu[RowIndex] = notoccued
LeftTop_RightDown[LineIndex-RowIndex+N-1] = notoccued
RightTop_LefttDown[LineIndex+RowIndex] = notoccued
}
}
}
main()
{
int i = 0
/* 调用求解函数*/
nextline(i)
/* 保持屏幕结果*/
getchar()
}
class Queen8{static final int QueenMax = 8
static int oktimes = 0
static int chess[] = new int[QueenMax]//每一个Queen的放州岁置位置
public static void main(String args[]){
for (int i=0i<QueenMaxi++)chess[i]=-1
placequeen(0)
System.out.println("\段迹哪n\n\n八皇后共有"+oktimes+"个解法made by yifi 2003")
}
public static void placequeen(int num){ //num 为现在要放置的行数握码
int i=0
boolean qsave[] = new boolean[QueenMax]
for(i<QueenMaxi++) qsave[i]=true
//下面先把安全位数组完成
i=0//i 是现在要检查的数组值
while (i<num){
qsave[chess[i]]=false
int k=num-i
if ( (chess[i]+k >= 0) &&(chess[i]+k <QueenMax) ) qsave[chess[i]+k]=false
if ( (chess[i]-k >= 0) &&(chess[i]-k <QueenMax) ) qsave[chess[i]-k]=false
i++
}
//下面历遍安全位
for(i=0i<QueenMaxi++){
if (qsave[i]==false)continue
if (num<QueenMax-1){
chess[num]=i
placequeen(num+1)
}
else{ //num is last one
chess[num]=i
oktimes++
System.out.println("这是第"+oktimes+"个解法 如下:")
System.out.println("第n行: 1 2 3 4 5 6 7 8")
for (i=0i<QueenMaxi++){
String row="第"+(i+1)+"行: "
if (chess[i]==0)
else
for(int j=0j<chess[i]j++) row+="--"
row+="++"
int j = chess[i]
while(j<QueenMax-1){row+="--"j++}
System.out.println(row)
}
}
}
//历遍完成就停止
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)