- //============================================================================
- // Name : EightQueen.cpp
- // Author : Rick
- // Version :
- // Copyright : Just for test ^_^
- // Description : Hello World in C++, Ansi-style
- //============================================================================
- #include <blitz/array.h>
- #include <stdio.h>
- #include <time.h>
- #include <iostream>
- #include <iomanip>
- using namespace blitz;
- void
- SetFlag(Array<int, 2>& A, int xpos, int ypos, int xboundary,
- int yboundary);
- void UnSetFlag(Array<int, 2>& A, int xpos, int ypos, int xboundary,
- int yboundary);
- void PlaceQueen(int cx, int cy);
- int main() {
- long t1, t2, t;
- time(&t1);
- PlaceQueen(12, 12);
- time(&t2);
- t = t2 - t1;
- cout << "Time consume: "<<t<<" Seconds"<<endl;
- }
- void SetFlag(Array<int, 2>& A, int xpos, int ypos, int xboundary, int yboundary) {
- for (int i = 0; i < xboundary; ++i) //水平方向
- A(i, ypos) += 1;
- for (int i = 0; i < yboundary; ++i) //垂直方向
- A(xpos, i) += 1;
- for (int i = 1; ypos + i < yboundary; ++i) { //y+ x+ x-
- if ((xpos + i) < xboundary) {
- A(xpos + i, ypos + i) += 1;
- }
- if ((xpos - i) >= 0) {
- A(xpos - i, ypos + i) += 1;
- }
- }
- for (int i = -1; ypos + i >= 0; --i) { //y- x+ x-
- if ((xpos + i) >= 0) {
- A(xpos + i, ypos + i) += 1;
- }
- if ((xpos - i) < xboundary) {
- A(xpos - i, ypos + i) += 1;
- }
- }
- }
- void UnSetFlag(Array<int, 2>& A, int xpos, int ypos, int xboundary,
- int yboundary) {
- for (int i = 0; i < xboundary; ++i) //水平方向
- A(i, ypos) -= 1;
- for (int i = 0; i < yboundary; ++i) //垂直方向
- A(xpos, i) -= 1;
- for (int i = 1; ypos + i < yboundary; ++i) { //y+ x+ x-
- if ((xpos + i) < xboundary) {
- A(xpos + i, ypos + i) -= 1;
- }
- if ((xpos - i) >= 0) {
- A(xpos - i, ypos + i) -= 1;
- }
- }
- for (int i = -1; ypos + i >= 0; --i) { //y- x+ x-
- if ((xpos + i) >= 0) {
- A(xpos + i, ypos + i) -= 1;
- }
- if ((xpos - i) < xboundary) {
- A(xpos - i, ypos + i) -= 1;
- }
- }
- }
- void PlaceQueen(int cx, int cy) {
- Array<int, 2> A(cx, cy);
- A = 0;
- int count = 0;
- int *loop;
- loop = new int[cx];
- for (int i = 0; i < cx; i++)
- loop[i] = 0;
- //bool thislevel=true;
- bool solve = true;
- int level = 0;
- while (solve) {
- while (true/*thislevel*/) {
- if (loop[level] >= cx) { //发现已经是最后一个位置
- if (level == 0) {
- solve = false; //已经找完了,结束大循环
- break;
- }
- loop[level] = 0; //设置到开始位置
- --level; //返回上一层
- A(level, loop[level]) -= '*'; //清除上一层的标记
- UnSetFlag(A, level, loop[level], cx, cy);
- ++loop[level];
- //本次循环结束
- break;
- }
- if (A(level, loop[level]) > 0) //不能放置,查看下一个位置
- ++loop[level];
- else { //能够放置
- if (level == cx - 1) { //是不是最后一层
- A(level, loop[level]) += '*'; //设置标记
- SetFlag(A, level, loop[level], cx, cy);
- count++;
- A(level, loop[level]) -= '*';
- UnSetFlag(A, level, loop[level], cx, cy); //取消标记找下一个
- loop[level] = cx;
- } else {
- A(level, loop[level]) += '*'; //设置标记
- SetFlag(A, level, loop[level], cx, cy);
- ++level; //进入下一层
- }
- }
- }
- }
- delete[] loop;
- cout <<"The Solution: "<< count << endl;
- }
相关热门文章
- 五十年代初大馬老粵語歌歌書考...
- 欢迎八路城管希在ChinaUnix博...
- 欢迎一锤八十在ChinaUnix博客...
- 欢迎一梦八百年在ChinaUnix博...
- 欢迎初八2000在ChinaUnix博客...
- A sample .exrc file for vi e...
- IBM System p5 服务器 HACMP ...
- 游标的特征
- busybox的httpd使用CGI脚本(Bu...
- Solaris PowerTOP 1.0 发布
给主人留下些什么吧!~~
评论热议