最少步数

hekexun 2023-01-09 12:41:51 26 返回题目

#include<bits/stdc++.h>

using namespace std;

struct poos{

int x,y,step;

};

bool vis[200][200];

const int dir[12][2]={{-2,2},{2,-2},{2,2},{-2,-2},{-1,2},{1,-2},{-1,-2},{1,2},{2,-1},{2,1},{-2,-1}};

int bfs(int x,int y){

memset(vis,0,sizeof(vis));
queue<poos> a;
a.push({x,y,0});
vis[x][y]=true;
while(!a.empty()/*q.empty()判断是否为0*/ ){
	poos u=a.front();a.pop();
	if(u.x==1&&u.y==1){
		return u.step;
	}for(int i=0;i<12;++i){
		int x=u.x+dir[i][0];
		int y=u.y+dir[i][1];
		int s=u.step+1;
		if(x>=1&&x<=100&&y>=1&&y<=100&&!vis[x][y]){
			vis[x][y]=true;
			a.push({x,y,s});
		}
	}
}

}

int main(){

int x,y;
cin>>x>>y;
cout<<bfs(x,y)<<endl;
cin>>x>>y;
cout<<bfs(x,y)<<endl;
return 0;

}

{{ vote && vote.total.up }}