问一下为什么这样写bfs不对
查看原帖
问一下为什么这样写bfs不对
181805
天马行空mz楼主2022/3/30 15:37
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define maxn 2000
int nt[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//右下左上
int n,f[maxn][maxn];
int vis[maxn][maxn];
struct qe
{
	int x,y;
};
queue<qe> q;
void bfs(int x,int y)
{
	qe start,next;
	start.x=x;
	start.y=y;
	q.push(start);
	while(!q.empty())
	{
		start=q.front();
		q.pop();
		for(int i=0;i<4;i++)
		{
			next.x=start.x+nt[i][0];
			next.y=start.y+nt[i][1];
			if(next.x<1||next.x>n||next.y<1||next.y>n)
				continue;//不满足条件跳过取
			if(vis[x][y]==1||f[next.x][next.y]==1)
				continue;
			f[next.x][next.y]=2;
			vis[next.x][next.y]=1;
			q.push(next);
		}
	}
}
2022/3/30 15:37
加载中...