大家帮忙看看BFS的哪一步错了
查看原帖
大家帮忙看看BFS的哪一步错了
668320
fufuQAQ楼主2022/4/10 10:38
cpp
#include<bits/stdc++.h>
using namespace std;
const int N=410;
int n,m,x,y,ans,temp,tmp;
queue<int> q;
int vis[N],dis[N];
int dir[8][2]={{2,1},{-2,1},{2,-1},{-2,-1},{1,2},{1,-2},{-1,2},{-1,-2}}; 

int bfs(int xxx,int yyy)
{
	tmp=x*m+y;
	cout<<"tmp="<<tmp<<endl;
	q.push(tmp);
	vis[tmp]=1;
	dis[tmp]=1;//记录步数 
	while(!q.empty())
	{
	    temp=q.front();
		q.pop();
		int xx=temp/3;
		int yy=temp%3;
		if(xx == xxx && yy == yyy)  return ans=dis[temp];
		for(int i=0;i<8;i++)
		{
			int dx=dir[i][0]+xx;
			int dy=dir[i][1]+yy;
			if(dx<1 || dx>n || dy<1 || dy>n)  continue;
			int tt=dx*m+dy;
			vis[tt]=1;
			dis[tt]=dis[temp]+1;
			q.push(tt);
		}
	}
	
}

int main()
{
	cin>>n>>m>>x>>y;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			cout<<"i="<<i<<" "<<"j="<<j<<" ";
			printf("%5d",bfs(i,j));
		}
		cout<<endl;
	}
    return 0;
}
2022/4/10 10:38
加载中...