📄 eightqueensdynamic.txt
字号:
#include<math.h> //调用数学函数库
#include<stdio.h> //调用标准输入输出函数库
#include<conio.h>
#include<graphics.h> //调用图形函数库
#include<stdlib.h>
#define MAX 8 //定义棋盘格数(特别是横轴方向上)
int board[MAX]; //纵轴方向格数
void Drow() //画棋盘
{
int i;
int Driver=VGA,Mode=VGAHI; //初始化显示器
initgraph(&Driver,&Mode,"d:\\tc\\bgi");
setfillstyle(SOLID_FILL,BLUE); //棋盘底色模式
bar(5,15,600,500); //棋盘外框(二维)
setcolor(YELLOW); //棋盘边框颜色
for(i=0;i<=9;i++)
{
line(10,20+50*i,460,20+50*i); //画行
}
for(i=0;i<=9;i++)
{
line(10+50*i,20,10+50*i,470); //画列
}
line(460,20,560,20);
line(10,470,560,470);
line(560,470,560,20);
setcolor(RED);
outtextxy(465,30,"WuShangjie"); //版权注释
outtextxy(465,45,"Eight Queue");
outtextxy(465,60,"CopyRight2.0");
outtextxy(415,440,"Start"); //屏幕输出文本 坐标标记
outtextxy(385,440,"0");
outtextxy(335,440,"1");
outtextxy(285,440,"2");
outtextxy(235,440,"3");
outtextxy(185,440,"4");
outtextxy(135,440,"5");
outtextxy(85,440,"6");
outtextxy(35,440,"7");
outtextxy(415,455,"Point");
outtextxy(435,390,"0");
outtextxy(435,340,"1");
outtextxy(435,290,"2");
outtextxy(435,240,"3");
outtextxy(435,190,"4");
outtextxy(435,140,"5");
outtextxy(435,90,"6");
outtextxy(435,40,"7");
}
void DrowCircle(int i,int j) //话棋子
{
char text[80];
setfillstyle(SOLID_FILL,YELLOW);
setcolor(YELLOW);
circle(385-50*i,395-50*j,15);
floodfill(385-50*i,395-50*j,YELLOW); //圆内颜色填充
setcolor(RED);
sprintf(text,"%d,%d",i,j); //棋子上坐标标记
outtextxy(385-50*i-14,395-50*j,text);
}
void GiveDrowIn(int i,int j) //过去显示棋子清除
{
setfillstyle(SOLID_FILL,BLUE); //将填充颜色设置与底色相同
setcolor(BLUE); //将画笔颜色设置与底色相同
circle(385-50*i,395-50*j,15);
floodfill(385-50*i,395-50*j,BLUE); //一定区域内填充
outtextxy(385-50*i-14,395-50*j," ");
}
void show_result() //调度相关算法求出所有结果
{
int j=0;
while(j<MAX)
{
DrowCircle(j,board[j]);
getch();
j++;
}
getch();
for(j=0;j<MAX;j++)
{
GiveDrowIn(j,board[j]); //释放上回显示棋子区域
}
getch();
}
int check_cross(int n) //判断棋子是否冲突函数
{
int k;
for(k=0;k<n;k++){
if(board[k]==board[n]||(n-k)==abs(board[k]-board[n])) //是否同行,是否同对角线
return 1;
}
return 0;
}
void put_chess(int n) //八皇后摆列方式逐个显示函数
{
for(int l=0;l<MAX;l++){
board[n]=l;
if(!check_cross(n)){
if(n==MAX-1) {
getch();
show_result();
}
else put_chess(n+1);
}
}
}
void main() //主函数
{
Drow();
put_chess(0);
//to the end of the program
getch();
exit(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -