日常迷惑MLE

P1746 离开中山路

萧笙 @ 2020-12-05 21:45:03

这次加了TLE (quq555~~)

#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#define in inline
#define re register
using namespace std;
const int dx[4]={0,0,-1,1};
const int dy[4]={1,-1,0,0};
struct node{
    int x,y,step;
};
int n,x1,x2,y1,y2,ans;
bool map[1002][1002];
queue<node>q;
string str;
in void bfs()
{
    node now,next;
    now.x=x1,now.y=y1,now.step=0;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        int x=now.x,y=now.y,t=now.step;
        if(x==x2&&y==y2)    
        {ans=t;break;}
        map[x][y]=true;
        for(re int i=0;i<=3;i++)
        {
            int xx=now.x+dx[i];
            int yy=now.y+dy[i];
            if(xx>=1&&xx<=n&&yy>=1&&yy<=n&&!map[xx][yy])
            {
                next.x=xx;
                next.y=yy;
                next.step=t+1;
                q.push(next);
            }
        }
    }
    return;
}
int main()
{
    scanf("%d",&n);
    for(re int i=1;i<=n;i++)
    {
        cin>>str;
        int len=str.length();
        for(int j=0;j<len;j++)
            map[i][j+1]=str[j]==48?false:true;
    }
    scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
    bfs();
    printf("%d",ans);       
    return 0;
}

by a15090067818 @ 2021-08-20 16:07:39

加标记数组


|