又错啦
  • 板块P1443 马的遍历
  • 楼主imx2g
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/1/19 17:45
  • 上次更新2025/1/19 20:34:08
查看原帖
又错啦
1494013
imx2g楼主2025/1/19 17:45
#include<iostream>
#include<queue>
using namespace std;
struct horse//马子结构体 
{
	int num;//步数 
	int x;//坐标 
	int y; 
}; 
int n,m,x,y;
bool flag[10000][10000],flag1;
int time[10000][10000];//走几次 
queue<horse> q;
int main(){
	cin>>n>>m>>x>>y;
	
	
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			time[i][j]=-1;
		}
	}
	
	
	horse n1;
	n1.x=x;
	n1.y=y;
	n1.num=0;
	flag[n1.x][n1.y]=true;
	time[n1.x][n1.y]=0;
	q.push(n1);//入队 
	
	while(!q.empty()){
		
		n1=q.front();//保留 
		q.pop();//出队
		horse n2=n1;
			//八大判定 
			if(n2.x+1>=0&&n2.x+1<=n&&n2.y+2>=0&&n2.y+2<=n&&flag[x+1][y+2]==false){//(1.2)
				n2.x+=1;
				n2.y+=2;
				flag[n2.x][n2.y]=true;
				time[n2.x][n2.y]=n2.num;
				q.push(n2);
				n2.x-=1;
				n2.y-=2;
			}
			if(n2.x+2>=0&&n2.x+2<=n&&n2.y+1>=0&&n2.y+1<=n&&flag[x+2][y+1]==false){//(2,1)
				n2.x+=2;
				n2.y+=1;
				flag[n2.x][n2.y]=true;
				time[n2.x][n2.y]=n2.num;
				q.push(n2);
				n2.x-=2;
				n2.y-=1;
			}
			if(n2.x-1>=0&&n2.x-1<=n&&n2.y+2>=0&&n2.y+2<=n&&flag[x-1][y+2]==false){//(-1,2)
				n2.x-=1;
				n2.y+=2;
				flag[n2.x][n2.y]=true;
				time[n2.x][n2.y]=n2.num;
				q.push(n2);
				n2.x+=1;
				n2.y-=2;
			}
			if(n2.x-2>=0&&n2.x-2<=n&&n2.y+1>=0&&n2.y+1<=n&&flag[x-2][y+1]==false){//(-2,1)
				n2.x-=2;
				n2.y+=1;
				flag[n2.x][n2.y]=true;
				time[n2.x][n2.y]=n2.num;
				q.push(n2);
				n2.x+=2;
				n2.y-=1;
			}
			
			if(n2.x+2>=0&&n2.x+2<=n&&n2.y-1>=0&&n2.y-1<=n&&flag[x+2][y-1]==false){//(2,-1)
				n2.x+=2;
				n2.y-=1;
				flag[n2.x][n2.y]=true;
				time[n2.x][n2.y]=n2.num;
				q.push(n2);
				n2.x-=2;
				n2.y+=1;
			}
			if(n2.x-1>=0&&n2.x-1<=n&&n2.y-2>=0&&n2.y-2<=n&&flag[x-1][y-2]==false){//(-1,-2)
				n2.x-=1;
				n2.y-=2;
				flag[n2.x][n2.y]=true;
				time[n2.x][n2.y]=n2.num;
				q.push(n2);
				n2.x+=1;
				n2.y+=2;
			}
			
			if(n2.x-2>=0&&n2.x-2<=n&&n2.y-1>=0&&n2.y-1<=n&&flag[x-2][y-1]==false){//(-2,-1)
				n2.x-=2;
				n2.y-=1;
				flag[n2.x][n2.y]=true;
				time[n2.x][n2.y]=n2.num;
				q.push(n2);
				n2.x+=2;
				n2.y+=1;
			}
			
		n2.num++;
	}
	
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cout<<time[i][j]<<" ";
		}
		cout<<endl;
	}
	return 0;
}
2025/1/19 17:45
加载中...