八皇后

  1. //============================================================================

  2. // Name : EightQueen.cpp

  3. // Author : Rick

  4. // Version :

  5. // Copyright : Just for test ^_^

  6. // Description : Hello World in C++, Ansi-style

  7. //============================================================================


  8. #include <blitz/array.h>
  9. #include <stdio.h>
  10. #include <time.h>
  11. #include <iostream>
  12. #include <iomanip>

  13. using namespace blitz;

  14. void
  15.         SetFlag(Array<int, 2>& A, int xpos, int ypos, int xboundary,
  16.                 int yboundary);
  17. void UnSetFlag(Array<int, 2>& A, int xpos, int ypos, int xboundary,
  18.         int yboundary);
  19. void PlaceQueen(int cx, int cy);

  20. int main() {

  21.     long t1, t2, t;
  22.     time(&t1);

  23.     PlaceQueen(12, 12);

  24.     time(&t2);

  25.     t = t2 - t1;
  26.     cout << "Time consume: "<<t<<" Seconds"<<endl;
  27. }

  28. void SetFlag(Array<int, 2>& A, int xpos, int ypos, int xboundary, int yboundary) {

  29.     for (int i = 0; i < xboundary; ++i) //水平方向

  30.         A(i, ypos) += 1;
  31.     for (int i = 0; i < yboundary; ++i) //垂直方向

  32.         A(xpos, i) += 1;
  33.     for (int i = 1; ypos + i < yboundary; ++i) { //y+ x+ x-

  34.         if ((xpos + i) < xboundary) {
  35.             A(xpos + i, ypos + i) += 1;
  36.         }
  37.         if ((xpos - i) >= 0) {
  38.             A(xpos - i, ypos + i) += 1;
  39.         }
  40.     }
  41.     for (int i = -1; ypos + i >= 0; --i) { //y- x+ x-

  42.         if ((xpos + i) >= 0) {
  43.             A(xpos + i, ypos + i) += 1;
  44.         }
  45.         if ((xpos - i) < xboundary) {
  46.             A(xpos - i, ypos + i) += 1;
  47.         }
  48.     }
  49. }
  50. void UnSetFlag(Array<int, 2>& A, int xpos, int ypos, int xboundary,
  51.         int yboundary) {

  52.     for (int i = 0; i < xboundary; ++i) //水平方向

  53.         A(i, ypos) -= 1;
  54.     for (int i = 0; i < yboundary; ++i) //垂直方向

  55.         A(xpos, i) -= 1;
  56.     for (int i = 1; ypos + i < yboundary; ++i) { //y+ x+ x-

  57.         if ((xpos + i) < xboundary) {
  58.             A(xpos + i, ypos + i) -= 1;
  59.         }
  60.         if ((xpos - i) >= 0) {
  61.             A(xpos - i, ypos + i) -= 1;
  62.         }
  63.     }
  64.     for (int i = -1; ypos + i >= 0; --i) { //y- x+ x-

  65.         if ((xpos + i) >= 0) {
  66.             A(xpos + i, ypos + i) -= 1;
  67.         }
  68.         if ((xpos - i) < xboundary) {
  69.             A(xpos - i, ypos + i) -= 1;
  70.         }
  71.     }
  72. }
  73. void PlaceQueen(int cx, int cy) {
  74.     Array<int, 2> A(cx, cy);
  75.     A = 0;
  76.     int count = 0;
  77.     int *loop;
  78.     loop = new int[cx];
  79.     for (int i = 0; i < cx; i++)
  80.         loop[i] = 0;
  81.     //bool thislevel=true;

  82.     bool solve = true;
  83.     int level = 0;

  84.     while (solve) {
  85.         while (true/*thislevel*/) {
  86.             if (loop[level] >= cx) { //发现已经是最后一个位置

  87.                 if (level == 0) {
  88.                     solve = false; //已经找完了,结束大循环

  89.                     break;
  90.                 }
  91.                 loop[level] = 0; //设置到开始位置

  92.                 --level; //返回上一层

  93.                 A(level, loop[level]) -= '*'; //清除上一层的标记

  94.                 UnSetFlag(A, level, loop[level], cx, cy);

  95.                 ++loop[level];
  96.                 //本次循环结束

  97.                 break;
  98.             }
  99.             if (A(level, loop[level]) > 0) //不能放置,查看下一个位置

  100.                 ++loop[level];
  101.             else { //能够放置

  102.                 if (level == cx - 1) { //是不是最后一层

  103.                     A(level, loop[level]) += '*'; //设置标记

  104.                     SetFlag(A, level, loop[level], cx, cy);
  105.                     count++;

  106.                     A(level, loop[level]) -= '*';
  107.                     UnSetFlag(A, level, loop[level], cx, cy); //取消标记找下一个

  108.                     loop[level] = cx;
  109.                 } else {
  110.                     A(level, loop[level]) += '*'; //设置标记


  111.                     SetFlag(A, level, loop[level], cx, cy);
  112.                     ++level; //进入下一层

  113.                 }
  114.             }
  115.         }
  116.     }

  117.     delete[] loop;
  118.     cout <<"The Solution: "<< count << endl;
  119. }
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(91) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zheguangqi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值