欢迎来到天天文库
浏览记录
ID:19369606
大小:21.70 KB
页数:27页
时间:2018-10-01
《皇后及n皇后c++程序设计》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include#include#includestaticintnum;enumStates{used,free};intqueenPos[100];intsnum=0;charboard[100][8][8];voidpause(){cout<<"按任意键继续";getch();}voidNQueen(intn,intN)//下面编译求解N阶皇后函数{if(N<4){cout<=4&&N<=8){if(n
2、==N){for(inti=0;i3、4、abs(queenPos[j]-i)==abs(n-j))break;}if(j==n){queenPos[n]=i;NQueen(n+1,N);}}}if(N>8){if(n==N)//执行完毕,输出结果{if(snum==250)getch();for(inti=0;i5、;i++){cout<6、7、abs(queenPos[j]-i)==abs(n-j))//列相同,或者对角break;}if(j==n){queenPos[n]=i;NQueen(n+1,N);}}}}classBoard//一盘棋8*8{private:StatesRows[8],DiagsLR[15],Diags8、RL[15];public:Board();~Board(){}charboard[8][8];boolisAttacked(int,int);voidPrint();voidPlaceQueen(int,int);voidRemoveQueen(int,int);};Board::Board()//构造函数,初始化为空{for(inti=0;i<8;i++){Rows[i]=free;for(intj=0;j<8;j++)board[i][j]='.';}for(intk=0;k<15;k++)DiagsLR[k]=DiagsRL[k9、]=free;}voidBoard::Print()//显示一组排列结果{cout<10、11、DiagsLR[diagLR]==used12、13、DiagsRL[14、diagRL]==used)returntrue;returnfalse;}voidBoard::PlaceQueen(introw,intcol)//放皇后{intdiagLR=col-row+7;//左对角线元素intdiagRL=row+col;//右对角线元素board[row][col]='Q';Rows[row]=used;DiagsLR[diagLR]=used;DiagsRL[diagRL]=used;}voidBoard::RemoveQueen(introw,intcol)//移去皇后{intdiagLR=col-r15、ow+7;intdiagRL=row+col;board[row][col]='.';Rows[row]=free;DiagsLR[diagLR]=free;DiagsRL[diagRL]=free;}constintSSize=8;typedefintSType;classStack{private:STypedata[SSize];intTop;public:Stack(){Top=0;}~Stack(){}boolisEmpty(){return!Top;}intSize(){returnTop;}boolPush(SType);16、boolPop(SType&);boolStackTop(SType&);};boolStack::Push(STyped){if(Top==SSize)returnfalse;else{data[Top]
3、
4、abs(queenPos[j]-i)==abs(n-j))break;}if(j==n){queenPos[n]=i;NQueen(n+1,N);}}}if(N>8){if(n==N)//执行完毕,输出结果{if(snum==250)getch();for(inti=0;i5、;i++){cout<6、7、abs(queenPos[j]-i)==abs(n-j))//列相同,或者对角break;}if(j==n){queenPos[n]=i;NQueen(n+1,N);}}}}classBoard//一盘棋8*8{private:StatesRows[8],DiagsLR[15],Diags8、RL[15];public:Board();~Board(){}charboard[8][8];boolisAttacked(int,int);voidPrint();voidPlaceQueen(int,int);voidRemoveQueen(int,int);};Board::Board()//构造函数,初始化为空{for(inti=0;i<8;i++){Rows[i]=free;for(intj=0;j<8;j++)board[i][j]='.';}for(intk=0;k<15;k++)DiagsLR[k]=DiagsRL[k9、]=free;}voidBoard::Print()//显示一组排列结果{cout<10、11、DiagsLR[diagLR]==used12、13、DiagsRL[14、diagRL]==used)returntrue;returnfalse;}voidBoard::PlaceQueen(introw,intcol)//放皇后{intdiagLR=col-row+7;//左对角线元素intdiagRL=row+col;//右对角线元素board[row][col]='Q';Rows[row]=used;DiagsLR[diagLR]=used;DiagsRL[diagRL]=used;}voidBoard::RemoveQueen(introw,intcol)//移去皇后{intdiagLR=col-r15、ow+7;intdiagRL=row+col;board[row][col]='.';Rows[row]=free;DiagsLR[diagLR]=free;DiagsRL[diagRL]=free;}constintSSize=8;typedefintSType;classStack{private:STypedata[SSize];intTop;public:Stack(){Top=0;}~Stack(){}boolisEmpty(){return!Top;}intSize(){returnTop;}boolPush(SType);16、boolPop(SType&);boolStackTop(SType&);};boolStack::Push(STyped){if(Top==SSize)returnfalse;else{data[Top]
5、;i++){cout<6、7、abs(queenPos[j]-i)==abs(n-j))//列相同,或者对角break;}if(j==n){queenPos[n]=i;NQueen(n+1,N);}}}}classBoard//一盘棋8*8{private:StatesRows[8],DiagsLR[15],Diags8、RL[15];public:Board();~Board(){}charboard[8][8];boolisAttacked(int,int);voidPrint();voidPlaceQueen(int,int);voidRemoveQueen(int,int);};Board::Board()//构造函数,初始化为空{for(inti=0;i<8;i++){Rows[i]=free;for(intj=0;j<8;j++)board[i][j]='.';}for(intk=0;k<15;k++)DiagsLR[k]=DiagsRL[k9、]=free;}voidBoard::Print()//显示一组排列结果{cout<10、11、DiagsLR[diagLR]==used12、13、DiagsRL[14、diagRL]==used)returntrue;returnfalse;}voidBoard::PlaceQueen(introw,intcol)//放皇后{intdiagLR=col-row+7;//左对角线元素intdiagRL=row+col;//右对角线元素board[row][col]='Q';Rows[row]=used;DiagsLR[diagLR]=used;DiagsRL[diagRL]=used;}voidBoard::RemoveQueen(introw,intcol)//移去皇后{intdiagLR=col-r15、ow+7;intdiagRL=row+col;board[row][col]='.';Rows[row]=free;DiagsLR[diagLR]=free;DiagsRL[diagRL]=free;}constintSSize=8;typedefintSType;classStack{private:STypedata[SSize];intTop;public:Stack(){Top=0;}~Stack(){}boolisEmpty(){return!Top;}intSize(){returnTop;}boolPush(SType);16、boolPop(SType&);boolStackTop(SType&);};boolStack::Push(STyped){if(Top==SSize)returnfalse;else{data[Top]
6、
7、abs(queenPos[j]-i)==abs(n-j))//列相同,或者对角break;}if(j==n){queenPos[n]=i;NQueen(n+1,N);}}}}classBoard//一盘棋8*8{private:StatesRows[8],DiagsLR[15],Diags
8、RL[15];public:Board();~Board(){}charboard[8][8];boolisAttacked(int,int);voidPrint();voidPlaceQueen(int,int);voidRemoveQueen(int,int);};Board::Board()//构造函数,初始化为空{for(inti=0;i<8;i++){Rows[i]=free;for(intj=0;j<8;j++)board[i][j]='.';}for(intk=0;k<15;k++)DiagsLR[k]=DiagsRL[k
9、]=free;}voidBoard::Print()//显示一组排列结果{cout<10、11、DiagsLR[diagLR]==used12、13、DiagsRL[14、diagRL]==used)returntrue;returnfalse;}voidBoard::PlaceQueen(introw,intcol)//放皇后{intdiagLR=col-row+7;//左对角线元素intdiagRL=row+col;//右对角线元素board[row][col]='Q';Rows[row]=used;DiagsLR[diagLR]=used;DiagsRL[diagRL]=used;}voidBoard::RemoveQueen(introw,intcol)//移去皇后{intdiagLR=col-r15、ow+7;intdiagRL=row+col;board[row][col]='.';Rows[row]=free;DiagsLR[diagLR]=free;DiagsRL[diagRL]=free;}constintSSize=8;typedefintSType;classStack{private:STypedata[SSize];intTop;public:Stack(){Top=0;}~Stack(){}boolisEmpty(){return!Top;}intSize(){returnTop;}boolPush(SType);16、boolPop(SType&);boolStackTop(SType&);};boolStack::Push(STyped){if(Top==SSize)returnfalse;else{data[Top]
10、
11、DiagsLR[diagLR]==used
12、
13、DiagsRL[
14、diagRL]==used)returntrue;returnfalse;}voidBoard::PlaceQueen(introw,intcol)//放皇后{intdiagLR=col-row+7;//左对角线元素intdiagRL=row+col;//右对角线元素board[row][col]='Q';Rows[row]=used;DiagsLR[diagLR]=used;DiagsRL[diagRL]=used;}voidBoard::RemoveQueen(introw,intcol)//移去皇后{intdiagLR=col-r
15、ow+7;intdiagRL=row+col;board[row][col]='.';Rows[row]=free;DiagsLR[diagLR]=free;DiagsRL[diagRL]=free;}constintSSize=8;typedefintSType;classStack{private:STypedata[SSize];intTop;public:Stack(){Top=0;}~Stack(){}boolisEmpty(){return!Top;}intSize(){returnTop;}boolPush(SType);
16、boolPop(SType&);boolStackTop(SType&);};boolStack::Push(STyped){if(Top==SSize)returnfalse;else{data[Top]
此文档下载收益归作者所有