求助!!!!
  • 板块P1443 马的遍历
  • 楼主AC_bee
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/8/2 08:10
  • 上次更新2024/8/2 08:18:34
查看原帖
求助!!!!
583593
AC_bee楼主2024/8/2 08:10
#include<bits/stdc++.h>
using namespace std;
int n,m,a1,b1,len,aa,bb,ok;
bool t[1001][1001];
void dfs(int x,int y)
{
	t[x][y]=true;
	if(ok==1) return;
	if(x==aa && y==bb) 
	{
		ok=1;
		return;
	}
	len++;
	if(x+1<=n && y+2<=m && t[x+1][y+2]==false) dfs(x+1,y+2);
	if(x+2<=n && y+1<=m && t[x+2][y+1]==false) dfs(x+2,y+1);
	if(x-1>=1 && y-2>=1 && t[x-1][y-2]==false) dfs(x-1,y-2);
	if(x-2>=1 && y-1>=1 && t[x-2][y-1]==false) dfs(x-2,y-1);
	if(x+1<=n && y-2>=1 && t[x+1][y-2]==false) dfs(x+1,y-2);
	if(x+2<=n && y-1<=m && t[x+2][y-1]==false) dfs(x+2,y-1);
	if(x-1>=1 && y+2<=m && t[x-1][y+2]==false) dfs(x-1,y+2);
	if(x-2>=1 && y+1<=m && t[x-2][y+1]==false) dfs(x-1,y+1);
}
int main()
{
	cin>>n>>m>>a1>>b1;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			ok=len=0;
			aa=i;
			bb=j;
			dfs(a1,b1);
			if(ok==0) cout<<-1<<' ';
			else cout<<len<<' ';
			for(int z1=1;z1<=n;z1++)
			{
				for(int z2=1;z2<=m;z2++) t[i][j]=false;
			}
		}
		cout<<endl;
	}
	return 0;
}
2024/8/2 08:10
加载中...