搞笑🤪
liuyilun123 · · 个人记录
1. 小学生作文《笨嘴》火了,老师笑到手抖:写得真好,下次别写了
2. 小学生“倒数第一”试卷走红,老师无奈家长需重视
3. 小学生“倒数第一”试卷走红,老师:孩子智商太高教不了
4.小学生满分作文《不要妹妹》走红 妈妈羞得没脸去学校
5.小学生试卷答案太“精彩”走红,老师凌乱网友笑到肚子疼
#include<bits/stdc++.h>
using namespace std;
const int N=105;
bool vis[N][N][4][N*10];
int n,sx,sy,ex,ey,ans=1e9;
char a[N][N];
struct node
{
int x,y,fx,step;
};
queue<node> q;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
void bfs()
{//第2遍是从起点走到终点
for(int i=0;i<4;i++)
{//4个方向都放入
q.push({sx,sy,i,0});
vis[sx][sy][i][0]=1;
}
while(q.size())
{
node hh=q.front();
q.pop();
if(hh.x==ex&&hh.y==ey)
{
ans=min(ans,hh.step);
continue ;
}
int nx=hh.x, ny=hh.y;
P2845 [USACO15DEC] Switching on the Lights S
while(1)
{//模拟直线走,沿着现在的方向
nx+=dx[hh.fx], ny+=dy[hh.fx];
if(nx<=0||nx>n||ny<=0||ny>n) break;
if(a[nx][ny]=='x') break;
if(vis[nx][ny][hh.fx][hh.step]) break;
vis[nx][ny][hh.fx][hh.step]=1;
q.push({nx,ny,hh.fx,hh.step});
}
for(int i=1;i<=2;i++)
{
int fx;
if(i==1) fx=(hh.fx+1)%4;//左转
else fx=(hh.fx-1+4)%4;//右转
nx=hh.x+dx[fx],ny=hh.y+dy[fx];
if(nx<=0||nx>n||ny<=0||ny>n) continue;
if(vis[nx][ny][fx][hh.step+1]) continue;
if(a[nx][ny]=='x') continue;//障碍不能走
vis[nx][ny][fx][hh.step+1]=1;//多转了一个弯
node tt={nx,ny,fx,hh.step+1};
q.push(tt);
}
}
return;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
cin>>a[i][j];
if(a[i][j]=='A') sx=i, sy=j;
if(a[i][j]=='B') ex=i, ey=j;
}
bfs();
if(ans==1e9) cout<<"-1";
else cout<<ans;
return 0;
}