H - Maze

[Title] Maze Italian problem, every road to find the shortest path, bfs inside classic title

[Idea] bfs by searching method, a shortest path for each of the array are accessed;

However, there is a process in the access techniques: with a two-dimensional array to access points on each maze; a recursive manner to all output points;

[Note] Do not be confused, you can draw yourself a few experiments

Here is my code:

#include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
bool vis[5][5];
char  mp[5][5];
int fx[4]={-1,0,1,0};
int fy[4]={0,1,0,-1};
struct mk{
    int x, y;
S} [ . 5 ] [ . 5 ]; // with each array access point 
BOOL Check ( int X, int Y)
{
    if(mp[x][y]=='0'&&x>=0&&x<5&&y>=0&&y<5)//判断边界条件
    {
        return 1;
    }
    return 0;
}
void Fun ( int X, int Y) // answer recursive 
{
     IF (X == 0 && Y == 0 ) return ;
    fun(s[x][y].x,s[x][y].y);//与真实二维坐标不一样哦
    cout<<"("<<s[x][y].x<<", "<<s[x][y].y<<")"<<"\n";
    return ;
}
void bfs(int x,int y)
{
    queue<mk> q;
    q.push({x,y});
    while(q.size())
    {
        mk now=q.front();
        q.pop();
        IF (now.x == . 4 && now.y == . 4 ) { // This description has reached the target time 
            Fun ( . 4 , . 4 ); return ;
        }
        for(int i=0;i<4;i++)
        {
            int nextx=now.x+fx[i];
            int nexty=now.y+fy[i];
            if(!vis[nextx][nexty]&&check(nextx,nexty))
               {vis[nextx][nexty]=1;
                   s[nextx][nexty].x=now.x;
                   s[nextx][nexty].y=now.y;
                   q.push({nextx,nexty});
               }
        }
        
    }
    
    return ;
}

int main ()
{

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    //memset(vis,0,sizeof(vis));
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<5;j++)
        {
            cin>>mp[i][j];
        }        
    }
    bfs(0,0);
    COUT << " (. 4,. 4) " << " \ n- " ; // because the final point is not recorded
    
    
}
View Code

This question is the most important thing is to understand the problem of access to the array, the output function is fun, this question is after big brother to explain clearly the reasons, though, to resolve the matter. There may be some small errors in the code, welcome! !

Of course, in the back doing the title search and found that not necessarily all and explain it to you in dfs or bfs. To engage in active practice.

Guess you like

Origin www.cnblogs.com/WGD943/p/12230011.html