⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 q8(2).cpp

📁 本程序为人工智能中较为经典的8皇后问题
💻 CPP
字号:
#include<iostream.h> 
class heng 
 { 
  public: 
   heng(int i):z(i){}; 
   void ty(); 
   int Total(); 
  private: 
   void print(); 
   int z; 
   static unsigned int y,j,l; 
   static int sum[8]; 
   static int total; 
 }; 
unsigned int heng::y=0; 
unsigned int heng::j=0; 
unsigned int heng::l=0; 
int heng::sum[8]={0,0,0,0,0,0,0,0}; 
int heng::total=0; 
void heng::ty() 
 { 
  for (int k=0; k<8; k++) 
{ 
if (!(y&1<<k) && !(j&1<<(z+k)) && !(l&1<<(z-k+7))) 
{ 
y|=1<<k; 
j|=1<<z+k; 
l|=1<<z-k+7; 
sum[z]=k; 
if (z<7) 
{ 
heng *next=new heng(z+1); 
next->ty(); 
 delete next; 
} 
       else 
{ 
 print(); 
 total+=1; 
} 
      y&=~(1<<k); 
j&=~(1<<z+k); 
l&=~(1<<z-k+7); 
} 
} 
} 
void heng::print() 
{ 
for (int k=0; k<8; k++) 
cout<<sum[k]+1<<' '; 
cout<<endl; 
} 
int heng::Total() 
{ 
return total; 
} 
class app 
{ 
public: 
int run(); 
private: 
heng *next; 
}; 
int app::run() 
{ 
next=new heng(0); 
next->ty(); 
  cout<<"Total="<<next->Total(); 
  delete next; 
  return 0; 
 } 
int main() 
 { 
  app a; 
  return a.run(); 
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -