求 N 皇后问题回溯算法
来源:互联网 发布:金钱永不眠软件 编辑:程序博客网 时间:2024/04/28 03:27
/*
* File: queen.c
* Description: 求 N 皇后问题回溯算法
*/
#include <stdio.h>
<script type="text/javascript"><!--google_ad_client = "pub-3555979289815451";google_ad_slot = "0437120238";google_ad_width = 468;google_ad_height = 60;//--></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
#define DelayTime 20000 /* 显示棋局时间 */
#define TopX 10 /* 棋盘左上角 x 坐标 */
#define TopY 5 /* 棋盘左上角 y 坐标 */
int N; /* 皇后数量 */
int a[8], b[15], c[15];
/*
* a[col-1] 记录第 col 列有无皇后, 1 表示有。
* b[row+col-2] 记录从左上数第 row+col-1 条斜率为 1 的线上有无皇后。
* c[row-col+7] 记录从右上数第 row-col+8 条斜率为 -1 的线上有无皇后。
*/
int Num = 0;
int row;
void BackTrack (int row)
{
int col; /* 局部变量 */
for (col=1; col<=N; col++)
{
if (a[col-1] + b[row+col-2] + c[row-col+N-1] == 0)
{
a[col-1] = 1; /* 更改数据 */
b[row+col-2] = 1;
c[row-col+N-1] = 1;
gotoxy(col*2 + TopX, row + TopY); /* 画皇后 */
putch('Q');
if (row < N)
{
BackTrack (row + 1);
}
else
{
Num++; /* 递归终点 */
gotoxy(40, 9);
printf("Num: %d ", Num);
delay(DelayTime);
}
a[col-1] = 0; /* 清空数据 */
b[row+col-2] = 0;
c[row-col+N-1] = 0;
gotoxy(col*2 + TopX, row + TopY); /* 清除图象 */
putch('.');
}/* end if */
}/* end for */
}/* end function BackTrack */
void main()
{
int i, j;
clrscr();
gotoxy(1, 10); /* 要求用户输入皇后数量 */
printf("Input the number of queen: ");
while(N <= 0 || N > 14)
{
scanf("%d", &N);
if(N > 14) printf("Two huge number, input again:");
if(N <= 0) printf("Can's smaller than 1, again:");
}
clrscr();
for(i=1; i<=N; i++) /* 画棋盘(Chessboard) */
{
for(j=1; j<=N; j++)
{
gotoxy(i*2 + TopX, j + TopY);
putch('.');
}
}
BackTrack(1); /* 开始回溯算法 */
gotoxy(12, 17); /* 显示最后结果 */
printf ("There are %d kinds of solution./n", Num);
getch();
}
* File: queen.c
* Description: 求 N 皇后问题回溯算法
*/
#include <stdio.h>
<script type="text/javascript"><!--google_ad_client = "pub-3555979289815451";google_ad_slot = "0437120238";google_ad_width = 468;google_ad_height = 60;//--></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
#define DelayTime 20000 /* 显示棋局时间 */
#define TopX 10 /* 棋盘左上角 x 坐标 */
#define TopY 5 /* 棋盘左上角 y 坐标 */
int N; /* 皇后数量 */
int a[8], b[15], c[15];
/*
* a[col-1] 记录第 col 列有无皇后, 1 表示有。
* b[row+col-2] 记录从左上数第 row+col-1 条斜率为 1 的线上有无皇后。
* c[row-col+7] 记录从右上数第 row-col+8 条斜率为 -1 的线上有无皇后。
*/
int Num = 0;
int row;
void BackTrack (int row)
{
int col; /* 局部变量 */
for (col=1; col<=N; col++)
{
if (a[col-1] + b[row+col-2] + c[row-col+N-1] == 0)
{
a[col-1] = 1; /* 更改数据 */
b[row+col-2] = 1;
c[row-col+N-1] = 1;
gotoxy(col*2 + TopX, row + TopY); /* 画皇后 */
putch('Q');
if (row < N)
{
BackTrack (row + 1);
}
else
{
Num++; /* 递归终点 */
gotoxy(40, 9);
printf("Num: %d ", Num);
delay(DelayTime);
}
a[col-1] = 0; /* 清空数据 */
b[row+col-2] = 0;
c[row-col+N-1] = 0;
gotoxy(col*2 + TopX, row + TopY); /* 清除图象 */
putch('.');
}/* end if */
}/* end for */
}/* end function BackTrack */
void main()
{
int i, j;
clrscr();
gotoxy(1, 10); /* 要求用户输入皇后数量 */
printf("Input the number of queen: ");
while(N <= 0 || N > 14)
{
scanf("%d", &N);
if(N > 14) printf("Two huge number, input again:");
if(N <= 0) printf("Can's smaller than 1, again:");
}
clrscr();
for(i=1; i<=N; i++) /* 画棋盘(Chessboard) */
{
for(j=1; j<=N; j++)
{
gotoxy(i*2 + TopX, j + TopY);
putch('.');
}
}
BackTrack(1); /* 开始回溯算法 */
gotoxy(12, 17); /* 显示最后结果 */
printf ("There are %d kinds of solution./n", Num);
getch();
}
- 求 N 皇后问题回溯算法
- 求 N 皇后问题回溯算法
- 回溯算法n皇后问题
- 回溯法-求n皇后问题
- 回溯法求N皇后问题
- 回溯法求N皇后问题
- n皇后问题——回溯算法
- 回溯算法之 N皇后问题
- 回溯算法之N皇后问题
- 回溯算法—n皇后问题
- N皇后问题——回溯算法
- php回溯算法解决n皇后问题
- 回溯算法与N皇后问题
- 回溯算法之n皇后问题
- 回溯算法—n皇后问题
- 经典回溯算法之N皇后问题
- 回溯算法之N皇后问题
- 算法-回溯法初探-n皇后问题
- SQL Server数据导入导出工具BCP详解
- 二叉树
- ASP wap开发Request.ServerVariables("Http_User_Agent")获取为空的原因
- 顺序栈
- 哈夫曼算法
- 求 N 皇后问题回溯算法
- 配置tomcat + mysql +eclipse
- Oracle 语法之 OVER (PARTITION BY ..)
- 单词倒转
- 诉衷情.08年十一游恭王府小憩于什刹海西海畔
- 使用VS自带的混淆器(dotfuscator)防止你的程序被反编译
- 大家好
- 100行代码去除QQ广告
- 继承时候遍历虚函数