rt.
代码:
#include <bits/stdc++.h>
#define int long long
using namespace std;
int fx[50][10];
void init(){
int tmp=1;
for(int i=0;i<10;i++) fx[i][0]=tmp,tmp<<=1;
tmp=1;
for(int i=10;i<20;i++) fx[i][1]=tmp,tmp<<=1;
tmp=1;
for(int i=20;i<30;i++) fx[i][0]=-tmp,tmp<<=1;
tmp=1;
for(int i=30;i<40;i++) fx[i][1]=-tmp,tmp<<=1;
}
struct node{
int x,y,cost;
};
queue<node> q;
int n,m,ex,ey,vis[1005][1005];
int pre_h[1005][1005],nxt_h[1005][1005],pre_l[1005][1005],nxt_l[1005][1005];
char mp[1005][1005];
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
init();
cin >> n >> m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin >> mp[i][j];
if(mp[i][j]=='$' || mp[i][j]=='#'){
if(mp[i][j]=='#') ex=i,ey=j;
}
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
pre_h[i][j]=pre_h[i][j-1]+(mp[i][j]=='.' || mp[i][j]=='X');
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
pre_l[i][j]=pre_l[i-1][j]+(mp[i][j]=='.' || mp[i][j]=='X');
for(int i=1;i<=n;i++)
for(int j=m;j>=1;j--)
nxt_h[i][j]=nxt_h[i][j+1]+(mp[i][j]=='.' || mp[i][j]=='X');
for(int i=n;i>=1;i--)
for(int j=1;j<=m;j++)
nxt_l[i][j]=nxt_l[i+1][j]+(mp[i][j]=='.' || mp[i][j]=='X');
vis[1][1]=1;
q.push((node){1,1,0});
while(!q.empty()){
node now=q.front();
q.pop();
if(now.x==ex && now.y==ey){
cout << now.cost << "\n";
return 0;
}
for(int i=0;i<40;i++){
int xx=now.x+fx[i][0];
int yy=now.y+fx[i][1];
if(vis[xx][yy]==0 && xx>=1 && xx<=n && yy>=1 && yy<=m){
if(i<10) {
if(pre_l[xx][yy]!=0) continue;
}
else if(10<=i<20){
if(pre_h[xx][yy]!=0) continue;
}
else if(20<=i<30){
if(nxt_l[xx][yy]!=0) continue;
}
else{
if(nxt_h[xx][yy]!=0) continue;
}
vis[xx][yy]=1;
q.push((node){xx,yy,now.cost+1});
}
}
}
return 0 ;
}