求助:为什么49和52行换成48和51行就不对
  • 板块学术版
  • 楼主zuiyishiwenjing
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/7/26 10:17
  • 上次更新2024/7/26 11:24:57
查看原帖
求助:为什么49和52行换成48和51行就不对
594136
zuiyishiwenjing楼主2024/7/26 10:17
#include<bits/stdc++.h>
using namespace std;
int vis[1005][1005],n;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
struct node{int x,y,step,flag;};
queue<node>q,L;
char c;
void bfs()
{
	while(!q.empty())
	{
		node wj=q.front();
		int x=wj.x,y=wj.y,step=wj.step,flag=wj.flag;
		q.pop();
		for(int i=0;i<4;i++)
		{
			int px=x+dx[i];
			int py=y+dy[i];
			if(px<1||px>n||py<1||py>n)
			{
				if(flag)
				{
					cout<<step+1;
					exit(0);
				}
				else
				 continue;
			}
			if(vis[px][py])continue;
			vis[px][py]=1;
			q.push((node){px,py,step+1,flag});
		}
	}
	return;
}
int main()
{
	cin>>n;
	int a,b;
	for(int i=1;i<=n;i++)
	 for(int j=1;j<=n;j++)
	 {
	 	scanf(" %c",&c);
	 	if(c=='#')vis[i][j]=1;
	 	if(c=='*')q.push((node){i,j,0,0});
	 	if(c=='S')a=i,b=j;
	 	//if(c=='S')L.push((node){i,j,0,1});
	 }
	q.push((node){a,b,0,1});
	//q.push(L.front()),L.pop();
	bfs();
	printf("IMPOSSIBLE\n");
	return 0;
}

题目地址:https://www.luogu.com.cn/problem/T345262

2024/7/26 10:17
加载中...