求助...只过了样例
查看原帖
求助...只过了样例
307612
Brunhild楼主2020/7/21 21:42

RT

#include <bits/stdc++.h>
using namespace std;
struct node{
	int x,y,d;
};
queue <node> a;
bool b[1000][1000];
node p;
int n,m,x,y;  //n行m列,x行y列 
int qp[1000][1000];
void command(int x,int y){
	if (x<1||x>n||y<1||y>m) return;
	if (b[x][y]!=0) return;
	b[x][y]=1;
	node by;
	by.x=x;
	by.y=y;
	by.d=p.d+1;
	a.push(by);
}
int main()
{
	cin>>n>>m>>x>>y;
	b[x][y]=1;
	p.x=x;
	p.y=y;
	p.d=0;
	a.push(p);
	while (a.empty()!=true){
		p=a.front();
		qp[p.x][p.y]=p.d;
		a.pop();
		//
		command(p.x+1,p.y+2);
		command(p.x+1,p.y-2);
		command(p.x-2,p.y-1);
		command(p.x-2,p.y+1);
		command(p.x+1,p.y-2);
		command(p.x+1,p.y+2);
		command(p.x+2,p.y-1);
		command(p.x+2,p.y+1);
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			if (qp[i][j]==0) qp[i][j]--;
		}
	}
	qp[1][1]=0;
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			cout<<qp[i][j]<<"     ";
		}
		cout<<endl;
	}
	return 0;
}

......

2020/7/21 21:42
加载中...