过不了#9!!!
  • 板块P1443 马的遍历
  • 楼主rpdg
  • 当前回复3
  • 已保存回复3
  • 发布时间2020/8/9 20:16
  • 上次更新2023/11/6 20:49:17
查看原帖
过不了#9!!!
342651
rpdg楼主2020/8/9 20:16
#include<bits/stdc++.h>
using namespace std;
int a[501][501];
int d[8][2]={{1,2},{1,-2},{-1,2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1}};
int h[20001][2];
int n,m,s,t;
void bfs(int p,int q)
{
	int t=0,w=1;
	h[1][0]=p;
	h[1][1]=q;
	a[p][q]=0;
	while(t<w)
	{
		t++;
		for(int i=0;i<8;i++)
		{
			int x,y;
			x=h[t][0]+d[i][0];
			y=h[t][1]+d[i][1];
			if(a[x][y]==-1&&x>=1&&x<=n&&y>=1&&y<=m)
			{
				w++;
				h[w][0]=x;
				h[w][1]=y;
				a[x][y]=a[h[t][0]][h[t][1]]+1;
			}
		}
	}
}
int main()
{
	memset(a,-1,sizeof(a));
	cin>>n>>m>>s>>t;
	bfs(s,t);
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			cout<<left<<setw(5)<<a[i][j];
		}
		cout<<endl;
	}
	return 0;
}

似乎也没什么不对
求助大佬

2020/8/9 20:16
加载中...