过不了样例求助
查看原帖
过不了样例求助
104775
zinuo楼主2021/5/8 22:29
#include<bits/stdc++.h>
using namespace std;
struct node{
	int x,y,s; //坐标,步数 
};
int xh,yh,xb,yb,ans; //黑马坐标,白马坐标,答案步数 
bool f[110][110]; //记忆化数组 
int fx[15]={0,-2,-2,2,2,-1,-2,-2,-1,1,2,2,1};
int fy[15]={0,-2,2,-2,2,-2,-1,1,2,2,1,-1,-2}; //各种走的状态
void bfs(int qx,int qy){  //起始坐标 
	queue<node> q;
	q.push({qx,qy,0}); //合并入队 
	while(!q.empty()){ //非空
		node st=q.front(); //去队头 
		q.pop();
		for(int i=1;i<=12;i++){
			int xx=st.x+fx[i];
			int yy=st.y+fy[i]; //枚举各种走的状态
			cout<<xx<<" "<<yy<<"\n"; 
			int s=st.s++; //步数++ 
			if(xx<1||yy<1||xx>100||yy>100){ //判断出界 
				continue;
			}
			if(f[xx][yy]!=true){
				f[xx][yy]=true; //记忆化 
				if(xx==1&&yy==1){ //达到目标 
                    ans=s; //答案赋值 
                    return; //找到退出 
                }
				q.push({xx,yy,s}); //合并入队
			}
		}
	}
}
int main(){
	cin>>xh>>yh>>xb>>yb; //输入
	if(xh==yh==1){
		cout<<"1\n";
	}else{
		bfs(xh,yh);//搜索
		cout<<ans<<"\n";
	}
	memset(f,false,sizeof(f)); 
	ans=0; //清空
    if(xb==yb==1){
		cout<<"1\n";
	}else{
		bfs(xb,yb);//搜索
		cout<<ans<<"\n";
	}
	return 0;
}
2021/5/8 22:29
加载中...