回朔算法之 八皇后 问题
2015-08-19 12:08
288 查看
#include<iostream> #include<iomanip> using namespace std; const int NUM = 8; int x[NUM] = {-1}; void NQueen( int k, int n); bool Place(int k, int i); int _tmain(int argc, _TCHAR* argv[]) { NQueen(1,8); return 0; } bool Place(int k, int i) { for( int j = 1; j < k; j++ ) { if( x[j] == i || abs(j-k) == abs( x[j]-i ) ) return false; } return true; } void NQueen( int k, int n ) { if( k > n ) return; for( int i = 1; i <= n; i++ ) { if( Place( k,i ) ) { x[k] = i; if( k==n ) { for(int j = 1; j<=n; j++) cout << setw( x[j] * 2 ) << 'Q' << endl; cout << "----------------------------------------------"<<endl; } else NQueen( k+1,n ); } } }
相关文章推荐
- 我所常用的ajax调用格式
- iOS第三方语音-讯飞语音
- js的一些琐碎
- uva 11987 删点并查集
- Windows下C++访问数据库(ADO)
- 线程状态
- sublime text3 前端插件介绍
- Linux命令(一)
- robot(1):关于机器人和互联网的思考
- Push Notification Doc
- jQuery实现的仿select功能代码
- 讯飞语音——唤醒
- PROC系列之---/proc/pid/stat
- ActiveMQ实战
- postgresql常用命令
- 讯飞语音——唤醒
- Ionic学习笔记三 Gulp在ionic中的使用
- What do /deep/ and ::shadow mean in a CSS selector?
- /proc/stat 的解释
- const经典面试题