社区讨论
求助:为什么49和52行换成48和51行就不对
学术版参与者 3已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @lz22qlao
- 此快照首次捕获于
- 2024/07/26 10:19 2 年前
- 此快照最后确认于
- 2024/07/26 11:24 2 年前
CPP
#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
回复
共 3 条回复,欢迎继续交流。
正在加载回复...