社区讨论
(起死回生)ABC D求调
灌水区参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @m5icab6c
- 此快照首次捕获于
- 2025/01/04 23:29 去年
- 此快照最后确认于
- 2025/11/04 11:58 4 个月前
CPP
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e3+5;
string s[N];
int a[N][N];
int vis1[N][N],vis2[N][N];
int sx,sy,tx,ty;
struct node{
int x,y;
int num;
int last;
int type;
};
int rx[5];
int ry[5];
void bfs()
{
queue<node> q;
node st;
st.x=sx,st.y=sy,st.num=0,st.last=2,st.type=0;
q.push(st);
vis1[sx][sy]=1;
vis2[sx][sy]=1;
while(!q.empty())
{
node tp=q.front();
q.pop();
int xx=tp.x,yy=tp.y,nn=tp.num,last=tp.last,type=tp.type;
// cout<<xx<<" "<<yy<<" "<<nn<<" "<<last<<endl;
if(type==1)vis1[xx][yy]=1;
else if(type==2) vis2[xx][yy]=1;
if(xx==tx&&yy==ty)
{
cout<<nn<<endl;
exit(0);
}
int dir=0;
for(int i=1;i<=4;i++)
{
if(i==1||i==2)
{
if(last==1) continue;
dir=1;
if(type==0) type=1;
}
if(i==3||i==4)
{
if(last==0) continue;
dir=0;
if(type==0) type=2;
}
int tox=xx+rx[i],toy=yy+ry[i];
if(type==1)
{
if(a[tox][toy]==1&&!vis1[tox][toy])
{
vis1[tox][toy]=1;
node ad;
ad.x=tox;
ad.y=toy;
ad.num=nn+1;
ad.last=dir;
ad.type=type;
q.push(ad);
}
}
else
{
if(a[tox][toy]==1&&!vis2[tox][toy])
{
vis2[tox][toy]=1;
node ad;
ad.x=tox;
ad.y=toy;
ad.num=nn+1;
ad.last=dir;
ad.type=type;
q.push(ad);
}
}
}
}
cout<<-1<<endl;
}
signed main()
{
int x,y;
cin>>x>>y;
rx[1]=0,ry[1]=1;
rx[2]=0,ry[2]=-1;
rx[3]=1,ry[3]=0;
rx[4]=-1,ry[4]=0;
for(int i=1;i<=x;i++)
{
cin>>s[i];
for(int j=1;j<=y;j++)
{
if(s[i][j-1]=='.')a[i][j]=1;
else if(s[i][j-1]=='#') a[i][j]=0;
else if(s[i][j-1]=='S')
{
sx=i,sy=j;
a[i][j]=1;
}
else
{
tx=i,ty=j;
a[i][j]=1;
}
}
}
bfs();
return 0;
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...