AC了,但是不知道怎么AC的
查看原帖
AC了,但是不知道怎么AC的
169015
未命名_1楼主2021/5/21 22:16

蒟蒻求助

离谱,输出除以二就过了,为什么??

就是想搞明白原理

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
struct nmsl{
	int x,y,step=-1;
	bool able=true;
};
queue<nmsl>q;
nmsl a[1001][1001];
int m,n,sx,sy,tx,ty;
int mx[10]={-2,-1,1,2, 2, 1,-1,-2};
int my[10]={ 1, 2,2,1,-1,-2,-2,-1};
nmsl t,tt;
bool check(int x,int y){
	if(x>=0&&y>=0&&x<n&&y<m&&a[x][y].able)return true;
	else return false;
}
int main(){
	cin>>n>>m>>sx>>sy;
	a[sx-1][sy-1].able=false;
	a[sx-1][sy-1].step=0;
	for(int i=0;i<n;i++)for(int j=0;j<m;j++){a[i][j].x=i;a[i][j].y=j;}
	q.push(a[sx-1][sy-1]);
	while(!q.empty()){
		t=q.front();
		q.pop();
		for(int i=0;i<8;i++){
			int tx=t.x+mx[i],ty=t.y+my[i];
			if(check(tx,ty)){
				if(a[tx][ty].step==-1)a[tx][ty].step=t.step+2;
				else a[tx][ty].step=t.step+1;
				q.push(a[tx][ty]);
				a[tx][ty].able=false;
			}
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			/*if(a[i][j].step!=0&&a[i][j].step!=-1)printf("%-5d",a[i][j].step/2);
			else */printf("%-5d",a[i][j].step);
		}
		cout<<endl;
	}
}
2021/5/21 22:16
加载中...