8皇后的另类解法
来源:互联网 发布:第一个克隆人 知乎 编辑:程序博客网 时间:2024/05/07 10:36
题目:在8×8 的国际象棋上摆放八个皇后,使其不能相互攻击,即任意两个皇后不得处在同一行、同一列或者同一对角斜线上。下图中的每个黑色格子表示一个皇后,这就是一种符合条件的摆放方法。请求出总共有多少种摆法。
我的思路很简单,就是把8皇后看成是一个8*8的矩阵,初始化的值为0,核心思想是递归。
1、第一行定了一个皇后,把这个皇后的列,两边斜对角value+1;
2、①从第二行开始把value为0的格子定为皇后(依次遍历)②把皇后的从下一行开始的列,两斜对角value+1;
3、如果某一行的格子全部不为0,则返回上一层递归,同时执行2中②的value-1操作。
代码(编译可执行):
#include <stdlib.h>#include <stdio.h>#include <vector>#include <iostream>#include <unistd.h>const int QueenValue=8;using namespace std;void SetQueenValue(int **arry,int row,int column,int value){ int rRow=row; int lRow=row; int lColumn=column-1; int rColumn=column+1; for(unsigned int i=row;i<QueenValue;++i) { arry[i][column]+=value; if(rRow<=QueenValue-1&&rColumn<=QueenValue-1) { arry[rRow][rColumn]+=value; ++rRow; ++rColumn; } if(lRow<=QueenValue-1&&lColumn>=0) { arry[lRow][lColumn]+=value; ++lRow; --lColumn; } } return;}void printpath(vector<pair<int,int> > QueenWays){ if(QueenWays.size()!=QueenValue) return; for(vector<pair<int,int> >::iterator it=QueenWays.begin();it!=QueenWays.end();++it) {// cout<<it->first<<" "<<it->second<<endl; for(unsigned int i=0;i<QueenValue;++i) { if(i==it->second) cout<<"1"; cout<<"0"; } cout<<endl; } cout<<endl; sleep(10); return;}void IsQueen(int **arry,int &total,vector<pair<int,int> >&QueenWays,int row){ if(QueenWays.size()!=row) return; if(row>=QueenValue) { total+=1; printpath(QueenWays); return; } for(unsigned int i=0;i<QueenValue;++i) { if(arry[row][i]==0){ QueenWays.push_back(make_pair(row,i)); SetQueenValue(arry,row+1,i,1); IsQueen(arry,total,QueenWays,row+1); QueenWays.pop_back(); SetQueenValue(arry,row+1,i,-1); } } return;}int GetDequeOfQueen(){ int **arry=new int* [QueenValue]; for(unsigned int i=0;i<QueenValue;++i) arry[i]=new int[QueenValue]; // memset(arry,0,sizeof(arry)); cout<<arry[0][0]<<endl; int total=0; vector<pair<int,int> > QueenWays; IsQueen(arry,total,QueenWays,0); return total;}int main(){ cout<<GetDequeOfQueen()<<endl; return 0;}
- 8皇后的另类解法
- 另类的八皇后求解法.
- 八皇后问题----另类解法
- 8皇后问题的两种解法
- 八皇后的解法
- 八皇后及N皇后的解法
- 皇后问题的一个解法
- N 皇后的递归解法
- 八皇后问题的解法
- 八皇后的递归解法
- n皇后问题的解法
- 8皇后位操作解法
- C语言 8皇后解法
- 一种便于理解的8皇后问题的回溯解法
- 打印1~1000个数的另类解法
- 中国象棋将帅问题的另类解法
- 01 背包问题的另类解法
- 三次贝塞尔曲线均分的另类解法
- linux内核空间和用户空间的区别及交互
- Graph
- const型变量与函数重载
- Discuz X2.5 修复漏洞后引发首页DIY数据为空失效
- 应届毕业生求职,注意事项
- 8皇后的另类解法
- CBP详解
- WebKit之Port
- 《构建高性能web站点》笔记--应用程序篇
- JavaScriptCore、V8如何与WebCore交互
- 写一个仿string的类
- 导师今天说的关于找工作话
- CABAC之手把手教你H.264编码
- Swing,Border设置边框演示