Maze (bfs)

1, INF least, will not let you go back and walked.

2, typedef pair <int, int> usage did not say clear, useless right.

3, weird. .

#include<iostream>
#include<queue>
#include<utility>
using namespace std;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int n,m,d[1005][1005],sx,sy,fx,fy;
char b[1005][1005];
const int INF=1e6;
typedef pair<int,int> p;
queue<p> que;r
int answer;
int bfs(int x,int y)
{
    que.push(p(x,y));
    d[x][y]=0;
    while(que.size())
    {
        p=que.front();
        que.pop();
        if(p.first==fx&&p.second==fy) break;
        
        for(int i=0;i<=3;i++)
        {
            int xx=p.first+dx[i];
            int yy=p.second+Dy [I];
             IF (XX> = . 1 && YY> = . 1 ! && XX <= n-&& YY <= m && B [XX] [YY] = ' # ' && D [XX] [YY] = INF) 
            { 
                que.push (P ( XX, YY)) // people that are in parentheses, usage have look 
                D [XX] [YY] = D [p.first] [p.second] + . 1 ; 
                
            } 
        } 
    } 
    return D [FX] [FY]; 
} 
int main () { 
    CIN >> >> n- m; 
    CIN >> SX SY >> >> >> FX FY;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            cin>>b[i][j];
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            d[i][j]=INF;
    answer=bfs(sx,sy);
    cout<<answer<<endl;
}

4, do it next time

Guess you like

Origin www.cnblogs.com/beiyueya/p/12123778.html
Recommended