1 solutions

  • 0
    @ 2022-3-25 21:24:43
    #include <stdio.h>
    #include <math.h>
    #include <algorithm>
    #include <iostream>
    #include <string.h>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <vector>
    using namespace std;
    int n,m,ans,x,y,s,t;
    int dx[8]= {-2,-1,1,2,-2,-1,1,2},dy[8]= {-1,-2,-2,-1,1,2,2,1};
    char a[10][10];
    int vis[10][10];
    struct temp {
        int tx,ty,step;
    };
    queue<temp> q;
    void bfs(int xx,int yy) {
        while(!q.empty()) {
            temp k=q.front();
            q.pop();
            if(k.tx==s&&k.ty==t) {
                cout<<k.step<<endl;
                return;
            }
            if(vis[k.tx][k.ty])
                continue;
            vis[k.tx][k.ty]=1;
            for(int i=0; i<8; i++) {
                int rx=k.tx+dx[i],ry=k.ty+dy[i];
                if(rx>0&&ry>0&&rx<=n&&ry<=m&&!vis[rx][ry]) {
                    q.push({rx,ry,k.step+1});
                }
            }
        }
    }
    int main() {
        scanf("%d%d%d%d%d%d",&n,&m,&x,&y,&s,&t);
        q.push({x,y,0});
        bfs(x,y);
        return 0;
    }
    
    

    Information

    ID
    775
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    3
    Tags
    # Submissions
    36
    Accepted
    23
    Uploaded By