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

📄 马的遍历.txt

📁 里面包含vc的经典算法,如动态规划,分治,回溯,贪心算法等
💻 TXT
字号:
#include<iostream>
#include<iomanip>
using namespace std;
int   Forward[8][2]={{-1,-2},{-1,2},{1,-2},{1,2},{-2,-1},{-2,1},{2,-1},{2,1}};//马的遍历方向
class Dfs_horse
{
public:
    Dfs_horse(); 
    void Dfs_Visit(int x,int y);               
private:
    int  Qipan[8][8] ;//每个格子的值,表示是否遍历过 
    int  Count ;    //计数,遍历棋盘的格子的数目 
    int  Num ;        //控制输出. 
};

Dfs_horse::Dfs_horse() //初始化,使每个格子的值为假
{
   for (int i = 0; i < 8;i ++)
   for(int j = 0;j < 8;j ++)
   Qipan[i][j] = 0;   
   Count = 0;
   Num = 0;
}
                     
void  Dfs_horse::Dfs_Visit(int x,int y)
{
	if(x > 8 || x<1 || y>8 || y<1) return;//越界返回
    if(Qipan[x-1][y-1] != 0) return;//若此格已经遍历过则返回 
    Count ++;           
    Qipan[x-1][y-1] = Count;//记录这个格子的遍历时间     
    for(int ix = 0;ix < 8;ix ++)  //向各个方向搜索           
       Dfs_Visit(x+Forward[ix][0],y+Forward[ix][1]) ;       
     if(Count == 64)  //Num控制输出遍历结果的次数 
     {        
         Num ++;   
         cout << "遍历次数 " << Num << endl ;         
         cout <<"-----遍历的路径为-----"<<endl;
         for (int i = 0;i < 8;i ++)
         {
             for(int j = 0;j < 8;j ++)    
                cout<< setw(2) << right<< Qipan[i][j] <<" " ;
               cout << endl; 
         }                    
     }    
}


int  main()
{
     Dfs_horse  abc;
     abc.Dfs_Visit(7,2);    
     system("PAUSE");
     return 0;
}

⌨️ 快捷键说明

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