hdu2181__DFS

题目大意:http://acm.hdu.edu.cn/showproblem.php?pid=2181

           深度搜索!

            搜索有很多种,有简单的for循环(不一定简单),而深度搜索或是广度搜索其实考察的是对递归的理解程度;

            这里先说说个人对深搜的理解;

            注意以下几点:

             1.Dfs()应该什么类型,void  还是其他的什么类型;

             2.Dfs()   中有哪些参数

             3.还有就是对标记数组(就是用于标志某个结点是否被访问过的数组)

             4.....................

              

#include <iostream>
using namespace std;
int map[21][3];//用于记录i城市连接的三个城市
int used[21];//标记数组
int result[21];//用于记录数组
int nub=1;
void  Dfs(int m,int len,int ci)//第一个参数记录当前城市,第二个参数是构造结果的下标,第三个参数为了其实可以用全局变量来代替
{
 int i;
 result[len]=m;
 used[m]=1;///////////////////////////////深度搜索的精髓
 for(i=0;i<3;i++)
 {
  int temp=map[m][i];
  if(temp==ci&&len==19)
  {
   cout<<nub++<<":"<<"  "; 
   for(int j=0;j<20;j++)
    cout<<result[j]<<" ";
   cout<<ci<<endl;
   }
  if(!used[temp])
     Dfs(temp,len+1,ci);
 }
 used[m]=0;//理解深度搜索这开始(还有上面的used[m]=1),也是关键
 

}
int main()
{
 int i,len;
 for(i=1;i<=20;i++)
 {
  cin>>map[i][0]>>map[i][1]>>map[i][2];
 }
 int m;
 while(cin>>m&&m!=0)
 {
  
  memset(used,0,sizeof(used));
  Dfs(m,0,m);
 }
    return 0;
}

原文地址:https://www.cnblogs.com/orangeblog/p/2412454.html