【无标题】

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define ll long long
int n,m,s1,s2,e1,e2,ans=10000;
int a[1007][1007],vis[1007][1007];
int nx[4]= {0,0,1,-1},ny[4]= {1,-1,0,0};
struct node {
    int x,y,step,flag;
};
queue q;
void bfs(int xx,int yy,int ss) {
    q.push({xx,yy,ss,3});
    while(!q.empty()) {
        node temp=q.front();
        q.pop();
        if(vis[temp.x][temp.y])
            continue;
        vis[temp.x][temp.y]=1;
        if(temp.x==e1&&temp.y==e2) {
            cout<             return;
        }
        for(int i=0; i<4; i++) {
            int tx=temp.x+nx[i],ty=temp.y+ny[i];
            if(tx>0&&ty>0&&tx<=n&&ty<=m&&!vis[tx][ty]&&a[tx][ty]==0) {
                if(temp.flag==3) {
                    if(temp.x==tx)
                        q.push({tx,ty,temp.step,2});
                    else if(temp.y==ty)
                        q.push({tx,ty,temp.step,1});
                } else {
                    if(temp.flag==2) {
                        if(tx!=temp.x) {
                            ans=min(ans+1,temp.step+1);
                            q.push({tx,ty,ans,1});
                        } else
                            q.push({tx,ty,temp.step,2});
                    } else if(temp.flag==1) {
                        if(ty!=temp.y) {
                            ans=min(ans+1,temp.step+1);
                            q.push({tx,ty,ans,2});
                        } else
                            q.push({tx,ty,temp.step,1});
                    }
                }
            }
        }
    }
}
int main() {
    cin>>n>>m;
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=m; j++) {
            cin>>a[i][j];
        }
    }
    cin>>s1>>s2>>e1>>e2;
    bfs(s1,s2,0);
    return 0;
}

你可能感兴趣的:(追梦算法,c++)