多组数据使用BFS,一定要将队列定在函数内部
查看原帖
多组数据使用BFS,一定要将队列定在函数内部
518110
OJone楼主2023/11/17 11:42
void bfs(int x,int y)
{
	queue<node> q;
	node h,t;
	t.x = x; t.y=y; t.step=0; vis[t.x][t.y]=1;
	q.push(t);
	
	while(!q.empty())
	{
		h=q.front();q.pop();
		if(h.x==1&&h.y==1){cout<<h.step<<endl;return;}
		for(int i=0;i<12;i++)
		{
			int tx = h.x+nex[i][0];
			int ty = h.y+nex[i][1];
			if(tx<1||tx>n||ty<1||ty>n||vis[tx][ty]==1){continue;} 
			else
			{
				vis[tx][ty]=1;
				t.x = tx,t.y = ty;
				t.step = h.step+1;
				q.push(t);
			}
		}
	}
}
2023/11/17 11:42
加载中...