zoukankan      html  css  js  c++  java
  • 皇后问题

      1 #include"iostream"
      2 #include"windows.h"
      3 using namespace std;
      4 
      5 class Piece{
      6 private:
      7     int *p;
      8     int n;
      9 public:
     10     static int count;
     11     static char **map;
     12     
     13     //创建棋子
     14     Piece(int n);
     15     
     16     //创建棋盘
     17     void createMap(char bg,char piece);
     18     
     19     //打印棋盘
     20     void show();
     21     
     22     //判断棋子是否可放
     23     bool judge(int x);
     24     
     25     //移动棋子
     26     void move(int x);
     27 };
     28 
     29 int Piece::count = 0;
     30 char **Piece::map;
     31 int main(){
     32     char bg = '+';
     33     char pie = '#';
     34     int n = 8;
     35     Piece p(n);
     36     p.createMap(bg,pie);
     37     p.move(0);
     38     return 0;
     39 }
     40 
     41 Piece::Piece(int n){
     42     this->n = n;
     43     p = new int[n];
     44     for(int i = 0;i < n;i++){
     45         p[i] = -1;
     46     }
     47 }
     48 void Piece::createMap(char bg,char piece){
     49     map = new char*[n];
     50     for(int i = 0;i < n;i++){
     51         map[i] = new char[n];
     52     }
     53     for(i = 0;i < n;i++){
     54         for(int j = 0;j < n;j++){    
     55             map[i][j] = bg;
     56         }
     57     }
     58 }
     59 
     60 void Piece::show(){
     61     for(int i = 0;i < n;i++){
     62         for(int j = 0;j < n;j++){
     63             if(p[j] == i){
     64                 cout<<"O"<<" ";
     65             }
     66             else{
     67                 cout<<map[i][j]<<" ";
     68             }
     69         }
     70         cout<<endl;
     71     }
     72 }
     73 
     74 void Piece::move(int x){
     75     if(x<n){            //棋子在棋盘内
     76         for(int i = 0;i < n;i++){
     77             p[x] = i;
     78             if(judge(x)){            //棋子可以放
     79                 if(x == n-1){
     80                     cout<<"-------"<<++count<<"-------"<<endl;
     81                     this->show();
     82                 }
     83                 move(x + 1);        //放下一个棋子
     84             }
     85             //棋子不能放,放下一个
     86         }
     87         //都不能放,不放,重新放上一个
     88         p[x] = -1;
     89     }
     90 }
     91 bool Piece::judge(int x){            //第x列
     92     int x1 = x + p[x];        //反斜线
     93     int x2 = x - p[x];        //正斜线
     94     for(int i = 0;i < n;i++){
     95         if(p[i]!=-1&&i!=x&&p[x] == p[i]){        //
     96             return false;
     97         }
     98         if(x2+i!=x&&x2 + i>=0&&x2 + i<n&&p[x2 + i] != -1&&x2 + i - x == p[x2 + i] - p[x]){        //正斜线
     99             return false;
    100         }
    101         if(x1 - i!=x&&x1 - i>=0&&x1 - i<n&&p[x1 - i] != -1&&x1 - i - x == p[x] - p[x1 - i]){        //反斜线
    102             return false;
    103         }
    104     }
    105     return true;
    106 }
    BY oleolema
  • 相关阅读:
    GIT的使用
    工具函数:cookie的添加、获取、删除
    技术分布特点:枣核型与网状结构
    js基础知识:表达式
    js基础知识:变量
    解决ie8(及其以下)不支持getElementsByClassName的问题
    毕设进度(10.29)
    毕设进度(10.28)
    毕设进度(10.27)
    毕设进度(10.26)
  • 原文地址:https://www.cnblogs.com/oleolema/p/9028391.html
Copyright © 2011-2022 走看看