Understanding and Learning of DFS and BFS + Template

After knowing the basic principles of DFS&BFS, let's look at a few examples.

                                         Example 1: Looking for a girlfriend

        Topic description:

        X, as a loyal fan of outdoor sports, always doesn't want to stay at home. Now, he wants to pull the dead house Y out of the house. Ask what is the shortest time from X's home to Y's home.

To simplify the problem, we abstract the map as an n*m matrix with row numbers 1 to n from top to bottom and columns 1 to m from left to right. In the matrix, 'X' indicates the initial coordinate of X, 'Y' indicates the position of Y, '#' indicates that the current position cannot be walked, and '*' indicates that the current position is passable. X can only move up, down, left, and right adjacent '*' each time, and each move takes 1 second.
        Enter :
       Multiple sets of inputs. Each set of test data first input two integers n, m (1<= n , m<=15 ) represents the map size. The next n lines, m characters each. Ensure that the entered data is legal.
       output:
       If X can reach Y's home, output the minimum time, otherwise output -1.
       example input
The DFS code is as follows:
#include<stdio.h>
#include<string.h>
int mv[16][16];
char map[16][16];
int mx[4]={-1,0,1,0};
int my[4]={0,-1,0,1};
int min;
struct B
{
    int x,y,years;
};
/*DFS开始*/
void dfs(int x,int y,int n,int m,int ans)
{
    int i;
    if(ans>=min)
        return ;
    if(ans<min&&map[x][y]=='Y'){
        min = ans;
        return ;
    }
    struct B t;
    for(i=0;i<4;i++)
    {
        t.x=x+mx[i];
        t.y=y+my[i];
        if(0<=t.x&&t.x<n&&0<=t.y&&t.y<m&&mv[t.x][t.y]==0&&map[t.x][t.y]!='#'){
            mv[t.x][t.y]=1;
            dfs(t.x,t.y,n,m,ans+1);
            mv[t.x][t.y]=0;
        }
    }
}
intmain ()
{
    int i,j,n,m;
    while(~scanf("%d %d",&n,&m)){
#include<stdio.h>
#include<string.h>
int mv[16][16];
char map[16][16];
int mx[4]={-1,0,1,0};
int my[4]={0,-1,0,1};
struct B
{
    int x,y,years;
}q[300],t,f;
int main()
{
    int i,j,n,m,flag,s,e;
    while(~scanf("%d %d",&n,&m)){
        for(i=0;i<n;i++)
            scanf("%s",map[i]);
        memset(mv,0,sizeof(mv));
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
                if(map[i][j]=='X')
                break;
            if(j!=m)break;
        } // BFS start 
        s=flag=e= 0 ;
        tx =i,ty=j,t.ans= 0 ;
        q[e++]=t;//进栈
        mv[t.x][t.y]=1;
        while(s<e){
            t=q[s++];//出栈
            if(map[t.x][t.y]=='Y'){
                printf("%d\n",t.ans);
                flag=1;
                break;
            }
            for(i=0;i<4;i++)
            {
                f.x=t.x+mx[i];
                f.y=t.y+my[i];
                if(0<=f.x&&f.x<n&&0<=f.y&&f.y<m&&mv[f.x][f.y]==0&&map[f.x][f.y]!='#'){
                    f.years =t.years+ 1 ;
                    q[e++]=f;
                    mv[f.x][f.y]=1;
                }
            }
        }
        if(flag==0)printf("-1\n");
    }
    return 0;
}
 
   

 

for(i=0;i<n;i++)
            scanf("%s",map[i]);
        memset(mv,0,sizeof(mv));
        min=300;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
                if(map[i][j]=='X')
                break;
            if(j!=m)break;
        }
        dfs(i,j,n,m,0);
        min==300?printf("-1\n"):printf("%d\n",min);
    }
    return 0;
}

 

        3 3
       X#Y
       ***
       #*#
        3 3
       X#Y
       *#*
       #*#      
      Sample output

       4

       -1

 The BFS code is as follows:

Thanks: http://www.cnblogs.com/Yan-C/p/3550625.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325271870&siteId=291194637