读书人

新手关于8皇后有关问题求教多谢了

发布时间: 2012-09-14 23:00:49 作者: rapoo

新手关于8皇后问题求教各位高手,谢谢了~
说明 :使用bc ++ 3.1 运行成功
问题 :结果是否正确?
能在优化些么?
程序还有哪里需要改进的?

C/C++ code
#include    <stdio.h>#include    <stdlib.h>#include    <graphics.h>#include    <dos.h>#include    <bios.h>void checker(void);void check(int (*)[],int,int);int PutQueen(int (*)[],int,int);int main(void){    int    graphdriver,graphmode;    int    array[8][8] = {0};    int    count,i,j,k,m;    detectgraph(&graphdriver,&graphmode);    initgraph(&graphdriver,&graphmode,"c:\\borlandc\\bgi");    for(i = 0 ; i < 8 ; i ++)                    //皇后放置的第一个位置    for(j = 0 ,count =0;j < 8 ; j ++)    {        setbkcolor(BLACK);                                     //重画棋盘        cleardevice();        setcolor(WHITE);        checker();        if(PutQueen(array,j,i) == 1)            count ++;        if(bioskey(0) == 0x11b)            exit(1);        for(m = 0 ; m < 8 ; m ++)                //清空棋盘准备测试下一个位置        for(k = 0 ; k < 8 ; k ++)            array[m][k] = 0;    }    closegraph();    printf("There are <%d> ways to put eight queen!\n",count);    return 0;}int PutQueen(int (*array)[8],int x,int y)                //在制定位置放置皇后并测试全盘能否放置8个皇后{    int    count = 0,flag;    for(flag = 0; flag < 8 ;y ++,flag ++)    {        y %= 8;        for(x = flag != 0 ? 0 : x;x < 8; x ++)        {            if(array[y][x] != -1)            {                setcolor(BLUE);                circle(30 + x * 60,30 +y * 60,20);                circle(30 + x * 60,30 +y * 60,18);                check(array,x,y);                count ++;                break;            }        }    }    return (count == 8 ? 1 : 0);}void CountCoor(int,int,int,int,int,int,int (*)[]);void check(int (*array)[8],int x,int y)                    //在皇后攻击范围内设置标识{    CountCoor(x,y,1,0,8,-1,array);        //x++;    CountCoor(x,y,-1,0,-1,-1,array);     //x--;    CountCoor(x,y,0,1,-1,8,array);        //y++;    CountCoor(x,y,0,-1,-1,-1,array);    //y--;    CountCoor(x,y,1,1,8,8,array);        //x++,y++;    CountCoor(x,y,-1,-1,-1,-1,array);    //x--,y--;    CountCoor(x,y,1,-1,8,-1,array);        //x++,y--;    CountCoor(x,y,-1,1,-1,8,array);        //x--,y++;}void CountCoor(int x,int y,int numx,int numy,int limitx,int limity,int (*array)[8]) //攻击范围显式标注{    while((x+= numx) != limitx && (y+=numy) != limity)    {        if(*(*(array + y)+x) != -1)        {            *(*(array + y)+ x) = -1;            setcolor(RED);            line(x * 60,y * 60,x * 60 + 60,y * 60 + 60);            line(x * 60 + 60 , y * 60,x * 60,y*60 + 60);        }    }}void checker(void)                                    //画棋盘{    int    i,j,k,coorx,coory;    for(i = 0 ; i < 8 ; i ++)    {        for(j = 0 ; j < 8 ; j ++)        {            if(i % 2 == 0 && j % 2 != 0)                continue;            else if(i % 2 != 0 && j % 2 == 0)                continue;            for(coorx = j * 60,coory = i * 60,k = 0; coorx + k < coorx + 60;k ++)                line(coorx + k,coory,coorx + k,coory + 60);        }    }}


[解决办法]
探讨
引用:
缺少#include <graphics.h>,运行不了。

borlandc 设置
options -> linker -> libraires -> []graphics library(选中此项)

initgraph(&graphdriver,&graphmode,"c:\\borlandc\\bgi");此句中字符串部分改为你电脑中bo……

[解决办法]
http://www.easyx.cn
[解决办法]
攻击范围计算可以用递归 x++哪些;

读书人网 >C++

热点推荐