是我太过愚蠢,还是程序太聪明
  • 板块学术版
  • 楼主君の名
  • 当前回复8
  • 已保存回复8
  • 发布时间2021/7/24 15:48
  • 上次更新2023/11/4 13:27:20
查看原帖
是我太过愚蠢,还是程序太聪明
358867
君の名楼主2021/7/24 15:48
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
struct node{
	int x;
	int y;
};
queue <node> Q;
int n,m;
int x1,y1;
int x2,y2;
int ans=0;
int a[505][505]={0};
bool b[505][505]={0};
inline void BFS()
{
    int x3=0,y3=0;
	while(!Q.empty())
	{ 
	    x3=Q.front().x;
	    y3=Q.front().y;
	    b[x3][y3]=1;
	    Q.pop();
		if(b[x3+1][y3]==0&&x3+1<=n&&a[x3+1][y3]==1)
		{
            if(x3+1==x2&&y3==y2)
            {
            	ans++;
            }
			b[x3+1][y3]=1;
			Q.push((node){x3+1,y3});
		}
		if(b[x3-1][y3]==0&&x3-1>0&&a[x3-1][y3]==1)
		{
			if(x3-1==x2&&y3==y2)
			{
                ans++;
            }
			b[x3-1][y3]=1;
			Q.push((node){x3-1,y3});
		}
		if(b[x3][y3-1]==0&&y3-1>0&&a[x3][y3-1]==1)
		{
            if(x3==x2&&y3-1==y2)
            {
            	ans++;
            }
			b[x3][y3-1]=1;
			Q.push((node){x3,y3-1});
		}
		if(b[x3][y3+1]==0&&y3+1<=m&&a[x3][y3+1]==1)
		{
            if(x3==x2&&y3+1==y2)
            {
            	ans++;
            }
			b[x3][y3+1]=1;
			Q.push((node){x3,y3+1});
		}
	}
}
int main()
{
	scanf("%d%d",&n,&m);
	for(register int i=1;i<=n;i++)
	{
		for(register int j=1;j<=m;j++)
		{
			char c;
			cin>>c;
			if(c=='g')
			{
				x2=i;
				y2=j;
			}
			else if(c=='s')
			{
				x1=i;
                y1=j;
			} 
			else if(c=='#')
			{
				a[i][j]=0; 
			} 
			else if(c=='.')
			{
				a[i][j]=1;
			}
		}
	}
    Q.push((node){x1,y1});
	BFS();
    printf("%d",ans);
	return 0;
}

为什么ans不会增加?

2021/7/24 15:48
加载中...