zoukankan      html  css  js  c++  java
  • 迷宫问题,打印所有路径,深度搜索,dfs

    #include<iostream>
    using namespace std;
    int maze [5][5] = {
    0, 1, 0, 0, 0,
    0, 1, 0, 1, 0,
    0, 0, 0, 0, 0,
    0, 1, 1, 1, 0,
    0, 0, 0, 0, 0,
    };
    typedef struct  
    {int x;int y;}Point;
    int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
    Point path[40];int len=-1;
    void dfs(int x,int y)
    {
    
    	int k,nx,ny;
    	maze[x][y]=1;
    	path[++len].x=x;path[len].y=y;
    	if(x==4 && y==4){//结束条件 
    	    cout<<"one case:"<<endl;
    	    for(k=0;k<=len;k++)
    		     cout<<path[k].x<<'	'<<path[k].y<<endl;
        }
    	else
        for(k=0;k<4;k++)
    	{
    		nx=x+dir[k][0];ny=y+dir[k][1];//下一位置(nx,ny)
    		if(nx>=0 &&nx<5 &&ny>=0 && ny<5 && maze[nx][ny]==0){
    			dfs(nx,ny);	
    		}
    	}
    	maze[x][y]=0;//把(nx,ny)尝试后清除当前标记 
    	len--;	
    	
    }
    int main()
    {
       dfs(0,0); 
    } 
    

      

  • 相关阅读:
    docker基础
    paas平台
    django 多数据分库
    s3对象存储
    css
    __construct()和__initialize()
    js function
    phpstorm ftp 使用
    php
    thinkphp 笔记
  • 原文地址:https://www.cnblogs.com/ewitt/p/14623119.html
Copyright © 2011-2022 走看看