求助 P1443
  • 板块灌水区
  • 楼主Ethan_tang
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/12/9 13:02
  • 上次更新2024/12/9 19:44:08
查看原帖
求助 P1443
1491661
Ethan_tang楼主2024/12/9 13:02

无法运行

#include <bits/stdc++.h>
using namespace std;
int n,m,x,y,vis[405][405],num;
int f_x[8] = {-2,-2,2,2,1,-1,1,-1},f_y[8] = {-1,1,-1,1,2,-2,-2,2};
struct zb
{
	int x;
	int y;
};
queue <zb> q;
zb pos(int x,int y)
{
	zb w;
	w.x = x;
	w.y = y;
	return w;
}
void bfs()
{
	while (!q.empty())
	{
		int x = q.front().x,y = q.front().y;
		q.pop();
		for (int i = 0;i < 8;i++)
		{
			if (vis[x + f_x[i]][y + f_y[i]] != -1 || vis[x + f_x[i]][y + f_y[i]] > vis[x][y] + 1)
			{
				q.push(pos(x + f_x[i],y + f_y[i]));
				vis[x + f_x[i]][y + f_y[i]] = vis[x][y] + 1;
			}
		}
		
	}
}
int main()
{
	cin >> n >> m >> x >> y;
	q.push(pos(x,y));
	memset(vis,-1,sizeof(vis));
	vis[x][y] = 0;
	bfs();
	for (int i = 1;i <= n;i++)
	{
		for (int j = 1;j <= m;j++)
		{
			cout << vis[i][j] << " ";	
		}
		cout << endl;
	}
	return 0;
}
2024/12/9 13:02
加载中...