DFS算法

来源:互联网 发布:python 列表内元组 编辑:程序博客网 时间:2024/05/14 08:30


voiddfs (int step)

{

判断边界

尝试每一种可能for (i = 1;i < n;i++)

{

 book[i]==1; //处理前标记下一个节点

 继续下一步dfs (step+1);

 book[i]==0; //还原处理过的节点,以进行下一次尝试

}

返回

}

打印路径---DFS---

用数组实现栈,先进后出

struct node
{
int x;
int y;
}
struct node s[256];
int top;
void dfs(int x,int y,int front)//front 进水管方向
{
  //判断是否已经到终点
  if(x==n && y==m+1)
  {
      flag=1;
   for(int i=1;i<=top;++i)
   {
         printf("%d %d",s[i].x,s[j].y)
   }
       return;
  }
//判断是否越界
book[x][y]=true;
top++;
s[top].x=x;
s[top].y=y;
......
 book[x][y]=false;//取消标记
top--;//当前方块出栈
}


0 0
原创粉丝点击