AI-N Queen Problem(bfs)
2015-09-16 16:37
465 查看
#include <cstdio>//because of the limit of the queue's length,the biggest N is only 14(2s).And I really don't think bfs is a good way to solve this queen problem. #include <iostream> #include <algorithm> #define BG 10000000 using namespace std; struct point{ int row; int col; struct point* upper;//to find the "parent" queen of this point }; point* q[BG];//because I must find the whole way according to "upper",so I can't use "queue" and "pop()" to delete the head point. int h,t; int n; bool check(point* w,int nc)//to check whether the nc place is proper below w queen. { point* p; p=w; int nr; nr=w->row+1; while(p!=NULL) { if((p->col==nc)||(nr-p->row)==abs(nc-p->col)) return false; p=p->upper; } return true; } void print_out(point* last)//print out the result { point *b=last; while(b!=NULL) { printf("(%d,%d) ",b->row,b->col); b=b->upper; } cout<<endl; } int main() { cin>>n; int i; for(i=1;i<=n;i++) { q[i]=(point*)malloc(sizeof(point)); q[i]->row=1; q[i]->col=i; q[i]->upper=NULL; } h=1; t=n; while(h!=t) { point* w; w=q[h++];//fetch the head point of the queen int nc; for(nc=1;nc<=n;nc++) { if(check(w,nc))//the lower queen below w-queen in the nc col { q[t]=(point*)malloc(sizeof(point)); q[t]->row=w->row+1; q[t]->col=nc; q[t]->upper=w; if(q[t]->row==n) { print_out(q[t]); return 0; } t++; } } } }
相关文章推荐
- AI-N Queen Problem(dfs)
- JS获取URL中参数值(QueryString)的4种方法分享
- EasyUI,comobox兼容可编辑和和下拉框
- `MQTTClient (~> 0.2.6)` required by `Podfile`
- 3.0新成员 -value,vector,map
- Sphinx/Coreseek 4.1 跑 buildconf.sh 一个错误,无法生成configure档
- 利用Global 的BeginRequest事件实现域名的重写
- HDU 4746 Mophues(莫比乌斯反演)
- 产品交互设计入门书籍推荐(亲自看过)
- UGUI之slider详解
- iOS UItextField 总结
- Using 1.7 requires compiling with Android 4.4 (KitKat); currently using API XX
- Auto Layout still required after sending -viewDidLayoutSubviews to the view controller. 终极解决方法
- js给文本框赋值 value与innerHTML
- inverse = “true” example and explanation
- UGUI4.6---InputField
- UITableView的使用详解
- UEditor编辑器实现编辑器自适应宽度的解决方案
- 安装protractor进行前端自动化测试-web ui自动化测试
- 修改EasyUI的目录树的图标