为什么不行(玄关)
查看原帖
为什么不行(玄关)
1328579
mairuisheng楼主2024/8/5 21:27
#include<bits/stdc++.h>
using namespace std;
queue<int> q;
int a[401][401];
int n,m,k;
void print()
{
	int i,j;
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		{
			printf("%5d",a[i][j]);
		}
		puts("");
	}
	exit(0);
}
void bfs(int x,int y)
{
	k++;
	if((x+2)<=n&&(y+1)<=m&&a[x+2][y+1]!=0)
	{
		a[x+2][y+1]=k;
		q.push(x+2);
		q.push(y+1);
	}
	if((x+1)<=n&&(y+2)<=m&&a[x+1][y+2]!=0)
	{
		a[x+1][y+2]=k;
		q.push(x+1);
		q.push(y+2);
	}
	if((x-1)>0&&(y+2)<=m&&a[x-1][y+2]!=0)
	{
		a[x-1][y+2]=k;
		q.push(x-1);
		q.push(y+2);
	}
	if((x-2)>0&&(y+1)<=m&&a[x-2][y+1]!=0)
	{
		a[x-2][y+1]=k;
		q.push(x-2);
		q.push(y+1);
	}
	if((x-2)>0&&(y-1)>0&&a[x-2][y-1]!=0)
	{
		a[x-2][y-1]=k;
		q.push(x-2);
		q.push(y-1);
	}
	if((x-1)>0&&(y-2)>0&&a[x-1][y-2]!=0)
	{
		a[x-1][y-2]=k;
		q.push(x-1);
		q.push(y-2);
	}
	if((x+1)<=n&&(y-2)>0&&a[x+1][y-2]!=0)
	{
		a[x+1][y-2]=k;
		q.push(x+1);
		q.push(y-2);
	}
	if((x+2)<=n&&(y-1)>0&&a[x+2][y-1]!=0)
	{
		a[x+2][y-1]=k;
		q.push(x+2);
		q.push(y-1);
	}
	if(q.empty())print();
	else 
	{
		int _x=q.front();
		q.pop();
		int _y=q.front();
		q.pop();
		bfs(_x,_y);
		k--;
	}
}
int main()
{
	int x,y;
	scanf("%d %d %d %d",&n,&m,&x,&y);
	memset(a,-1,sizeof(a));
	a[x][y]=0;
	bfs(x,y);
	return 0;
}

本地都不行

2024/8/5 21:27
加载中...