关于调试
  • 板块灌水区
  • 楼主yangzhiqin
  • 当前回复2
  • 已保存回复2
  • 发布时间2020/9/11 21:08
  • 上次更新2023/11/5 13:24:17
查看原帖
关于调试
140411
yangzhiqin楼主2020/9/11 21:08

为什么我在调试的时候遇到了queue就不能继续了??

#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
typedef pair<int,int>pll;
int n,m,cnt,step,f[810][810];
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
bool s[810][810];
pll boy,girl,ghost[3];
bool check1(int x,int y)
{
	if(x>=1&&x<=n&&y>=1&&y<=m&&s[x][y]!='X')return true;
	return false;
}
bool check2(int x,int y)
{
	for(int i=1;i<=2;i++)
	{
		if(abs(x-ghost[i].first)+abs(y-ghost[i].second)<=step*2)return false;	
	}
	return true;
}
int bfs()
{
	queue<pll>qb,qg;
	qb.push(boy),qg.push(girl);
	step=0;
	while(qb.size()||qg.size())
	{
		step++;
		for(int i=1;i<=3;i++)
		{
			for(int j=1;j<=qb.size();j++)
			{
				pll t=qb.front();
				qb.pop();
				int x=t.first,y=t.second;
				if(!check1(x,y))continue;
				for(int k=0;k<=3;k++)
				{
					int xx=x+dx[k],yy=y+dy[k];
					if(!check2(xx,yy))continue;
					if(f[xx][yy]==2)return step;
					if(!f[xx][yy])
					{
						f[xx][yy]=1;
						qb.push({xx,yy});
					}
				}
			}
		}
		for(int i=1;i<=1;i++)
		{
			for(int j=1;j<=qg.size();j++)
			{
				pll t=qg.front();
				qg.pop();
				int x=t.first,y=t.second;
				if(!check1(x,y))continue;
				for(int k=0;k<=3;k++)
				{
					int xx=x+dx[k],yy=y+dy[k];
					if(!check2(xx,yy))continue;
					if(f[xx][yy]==1)return step;
					if(!f[xx][yy])
					{
						f[xx][yy]=2;
						qg.push({xx,yy});
					}
				}
			}
		} 
	}
	return -1;
}
int main()
{
	int t;scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)scanf("%s",s[i]+1);
		cnt=0;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				if(s[i][j]=='M')boy={i,j};
				else if(s[i][j]=='G')girl={i,j};
				else if(s[i][j]=='Z')ghost[++cnt]={i,j};
			}
		}
		memset(f,0,sizeof(f));
		printf("%d\n",bfs());
	}
	return 0;
} 
2020/9/11 21:08
加载中...