社区讨论

错了……

P1443马的遍历参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@lo2qdz0s
此快照首次捕获于
2023/10/23 18:04
2 年前
此快照最后确认于
2023/10/23 18:04
2 年前
查看原帖
CPP
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
int map[500][500];
int n,m;
int m1[9] = {0,1,2,-1,-2,1,2,-1,-2};
int m2[9] = {0,2,1,2,1,-2,-1,-2,-1};
int X,Y;
int bps(int x,int y,int step)
{
	//cout << step << " ";
	queue<int> qx,qy,q1x,q1y;
	for(int i=1;i<=8;i++)
	{
		qx.push(x+m1[i]);
		qy.push(y+m2[i]);
	}
	while(1)
	{
		if(qx.empty())
			break;
		int mx=qx.front(),my=qy.front();
		qx.pop();qy.pop();
		if(map[mx][my]==0 && (mx!=X||my!=Y))
		{
			if(mx<=n && mx>=1 && my>=1 && mx<=m) 
			{
				q1x.push(mx);q1y.push(my);
				map[mx][my] = step;
			}
		}
	}
	while(1)
	{
		if(q1x.empty())
			break;
		bps(q1x.front(),q1y.front(),step+1);
		q1x.pop();q1y.pop();
	}
	return 0;
}
int main()
{
	int x,y;
	cin >> n >> m >> x >> y;
	map[x][y]=0;
	X=x;Y=y;
	bps(x,y,1);
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			cout << map[i][j] << " ";
		}
		cout << endl;
	}
	
	return 0;
}

回复

1 条回复,欢迎继续交流。

正在加载回复...