已经用 vector 优化了,怎么还会 MLE 呢?
#include<bits/stdc++.h>
using namespace std;
#define int short
int mx[]={0,1,0,-1};
int my[]={1,0,-1,0};
int lx[]={-1,-1,-1,0,1,1,1,0};
int ly[]={-1,0,1,1,1,0,-1,-1};
vector<vector<char>> a;
int n,m;
vector<vector<bool>> vis,lk;
struct node{
short x,y,step;
};
void look(int ex,int ey){
for(int i=0;i<8;i++){
int h=ex+lx[i],l=ey+ly[i];
while(h>=1&&h<=n&&l>=1&&l<=m&&a[h][l]=='O'){
lk[h][l]=1;
h+=lx[i],l+=ly[i];
}
}
}
int bfs(int sx,int sy){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
vis[i][j]=0;
}
}
queue<node> q;
q.push({sx,sy,0});
while(!q.empty()){
node h=q.front();
q.pop();
vis[h.x][h.y]=1;
if(lk[h.x][h.y])return h.step;
for(int i=0;i<4;i++){
short hh=h.x+mx[i],ll=h.y+my[i];
if(hh>=1&&hh<=n&&ll>=1&&ll<=m&&!vis[hh][ll]&&a[hh][ll]=='O'){
q.push({hh,ll,h.step+1});
}
}
}
return -1;
}
signed main(){
cin>>n>>m;
a.resize(n+1);
vis.resize(n+1);
lk.resize(n+1);
for(int i=1;i<=n;i++){
a[i].resize(m+1);
vis[i].resize(m+1);
lk[i].resize(m+1);
for(int j=1;j<=m;j++){
cin>>a[i][j];
}
}
int sx,sy,ex,ey;
while(cin>>ex>>ey>>sx>>sy){
if(!sx&&!sy&&!ex&&!ey)break;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
lk[i][j]=0;
}
}
look(ex,ey);
int d=bfs(sx,sy);
if(d==-1)cout<<"Poor Harry\n";
else cout<<d<<endl;
}
return 0;
}