太菜了,随机开题玩一玩被寄了
球帮助qwq
#include<bits/stdc++.h>
using namespace std;
const int N=1e3+5;
int n,m;
int a[N][N];
const int dx[4]={1,-1,0,0};
const int dy[4]={0,0,1,-1};
struct node{
int x,y,stp;
};
int h[N][N],c[N][N],flag[N][N];
inline int bfs(){
queue<node> q;
q.push({1,1,0});
flag[1][1]=1;
while(!q.empty()){
node tmp=q.front();q.pop();
int x=tmp.x,y=tmp.y,stp=tmp.stp;
if(a[x][y]==2){
return stp;
}
for(register int i=0;i<2;++i){
int xx=x+dx[i],r=1;
while(h[xx][y]==h[x][y] && xx<=n && y<=m && xx>0 && y>0){
if(flag[xx][y]) {r*=2,xx=x+r*dx[i];continue;}
flag[xx][y]=1;
q.push({xx,y,stp+1});
r*=2;
xx=x+r*dx[i];
}
}
for(register int i=2;i<4;++i){
int yy=y+dy[i],r=1;
while(c[x][yy]==c[x][y] && x<=n && yy<=m && x>0 && yy>0){
if(flag[x][yy]) {r*=2,yy=y+r*dy[i];continue;}
flag[x][yy]=1;
q.push({x,yy,stp+1});
r*=2;
yy=y+r*dy[i];
}
}
}
return -1;
}
signed main(){
ios::sync_with_stdio(0),cin.tie(),cout.tie();
cin>>n>>m;
for(register int i=1;i<=n;++i){
for(register int j=1;j<=m;++j){
char ch;cin>>ch;
if(ch=='.' || ch=='$') a[i][j]=0;//road
else if(ch=='X') a[i][j]=1;//barricade
else a[i][j]=2;//goal
}
}
for(register int i=1;i<=n;++i)
for(register int j=1;j<=m;++j){
h[i][j]=h[i-1][j]+(a[i][j]==1);
c[i][j]=c[i][j-1]+(a[i][j]==1);
}
cout<<bfs();
return 0;
}