# 用结构体定义队列为啥不对嘞?!
查看原帖
# 用结构体定义队列为啥不对嘞?!
469778
xujialin268楼主2021/7/24 15:50

结构体下面的定义队列一直报错。。为什么呀 如果有其他问题,dalao一定帮忙指出啊!

#include<cstdio>
#include<iostream>
#include<cmath>
#include<queue>
#include<string>
#include<cstring>
int n,m,x,y;
struct node{
	int x,y,step;
};
 queue <node> q;
const int movex[8]={-1,-2,-2,-1,1,2,2,1};
const int movey[8]={2,1,-1,-2,2,1,-1,-2};
int ans[425][425];
bool panduan[450][450];

void bfs(int x,int y,int step)
{
   
	q.push(x,y);
	ans[x][y]=step;
	panduan[x][y]=true;
	while(!q.empty())
	{
		node now=q.front();
		q.pop();
		if(ans[now.x][now.y]<now.step)
		continue;
		else ans[now.x][now.y]=now.step;
		for(int i=1;i<=8;i++)
		{
			int newx=now.x+movex[i];
			int newy=now.y+movey[i];
			if(newx>0&&newx<=n&&newy>0&&newy<=m)
			{
				q.push(newx,newy,now.step+1);
			}
		}
	}
	
}
 
 int main()
 {
 	printf("%d%d%d%d",&n,&m,&x,&y);
 	memset(ans,-1,sizeof(ans));
	memset(panduan,false,sizeof(panduan));
	bfs(x,y,0);
	for(int i=1;i<=n;i++)
	{
		for(int j=1;i<=m;j++)
		{
			printf("%-5d",ans[i][j]);
		}
		printf("\n");
	}
 	return 0;
 }//谢谢啦!
2021/7/24 15:50
加载中...