1330 【例8.3】最少步数

#include<bits/stdc++.h>
using namespace std;
int ax, ay, bx, by;
struct node{           
    int x, y, step;
};
node que[10010];        
int f, r;                
bool book[105][105];    
int fx[12][2]={{-1,-2},{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-2,-2},{-2,2},{2,2},{2,-2}};
void bfs(int x, int y){
    f=r=1;   
    memset(book,0,sizeof(book));                            
    que[r].x=x, que[r].y=y, que[r].step=0, book[x][y]=1;        
    while(f<=r){
        node t;                     
        t.x=que[f].x;
        t.y=que[f].y;
        t.step=que[f].step;
        if(t.x==1 && t.y==1){        
            cout<<t.step<<endl;
            break;
        }
        for(int i=0; i<12; i++){    
            int nx=t.x+fx[i][0];
            int ny=t.y+fx[i][1];
            if(nx>=1 && nx<=100 && ny>=1 && ny<=100 && book[nx][ny]==0){   
                book[nx][ny]=1;
                r++;                
                que[r].x=nx;
                que[r].y=ny;
                que[r].step=t.step+1;
            }
        }
        f++;           
    }
}
int main()
{
    cin>>ax>>ay>>bx>>by;
    bfs(ax, ay);
    bfs(bx, by);
    return 0;
 }

 

posted @ 2020-10-06 00:03  ssdaeda  阅读(686)  评论(1编辑  收藏  举报