题解代码

· · 题解

这段代码是一个求解迷宫问题的程序。给定一个迷宫,迷宫中有一些障碍物,用#表示,起点用A表示,终点用B表示。在迷宫中,只能上下左右移动,不能斜向移动。

程序的主要思路是使用深度优先搜索 (dfs) 和 Tarjan 算法来找出连通块,并使用广度优先搜索来计算起点到终点的最短路径。

具体来说,程序首先读入迷宫的大小和迷宫的内容。然后根据迷宫的内容构建图,并使用 Tarjan 算法找出连通块。接下来,使用广度优先搜索 (bfs) 计算起点到终点的最短路径,并将结果保存在数组 f 中。最后,根据询问的坐标判断该位置是否可达终点。\

\texttt{Code:}

#include <bits/stdc++.h>
#define res register int
#define N 1505
#define M 2265025
const int dx[4]={0,-1,1,0};
const int dy[4]={-1,0,0,1};
using namespace std;
int n,m,Q,st[M],f[N][N][4],q,top,id[N][N],tot,fa[M],tim,sx,sy,ex,ey,d[M],num,dfn[M],low[M],cnt,pos[M];
char s[N];
bool vis[N][N];
inline int read(){
    res w=0,X=0;char ch=0;
    while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
    while(isdigit(ch)){X=(X<<1)+(X<<3)+(ch^48);ch=getchar();}
    return w?-X:X;
}
struct papa{
    int to,next;
}e[M<<2];
struct point{
    int x,y,op;
    point():x(0),y(0),op(0){};
    point(int x0,int y0):x(x0),y(y0),op(-1){};
    point(int x0,int y0,int op0):x(x0),y(y0),op(op0){};
};
queue<point> l;
inline void add(res x,res y){
    e[++tot].to=y;
    e[tot].next=d[x];
    d[x]=tot;
}
inline bool check(res x,res y){
    return pos[x]==pos[y]||fa[pos[x]]==y||fa[pos[y]]==x;
}
inline void tarjan(res now,res fath){
    dfn[now]=low[now]=++tim;
    st[++top]=now;
    for(res i=d[now];i;i=e[i].next){
        res x=e[i].to;
        if(x==fath) continue;
        if(!dfn[x]){
            tarjan(x,now);
            low[now]=min(low[now],low[x]);
            if(dfn[now]>low[x]) continue;
            num++;
            fa[num]=now;
            for(;st[top]!=x;){
                pos[st[top]]=num;
                top--;
            }
            pos[st[top]]=num;
            top--;
        }
        else
          low[now]=min(low[now],dfn[x]);
    }
}
int main(){
    n=read(),m=read(),q=read();
    for(res i=1;i<=n;i++){
        scanf("%s",s+1);
        for(res j=1;j<=m;j++){
            if(s[j]=='#') continue;
            id[i][j]=++cnt;
            if(s[j]=='A') sx=i,sy=j;
            if(s[j]=='B') ex=i,ey=j;
            for(res k=0;k<2;k++){
                res nx=i+dx[k],ny=j+dy[k];
                if(nx<1||nx>n||ny<1||ny>m||!id[nx][ny]) continue;
                add(id[i][j],id[nx][ny]),add(id[nx][ny],id[i][j]);
            }
        }
    }
    num=cnt;
    for(res i=1;i<=cnt;i++)
      if(!dfn[i])
        tarjan(i,-1);
    l.push(point(sx,sy));
    while(!l.empty()){
        res x=l.front().x,y=l.front().y;
        l.pop();
        for(res i=0;i<4;i++){
            res nx=x+dx[i],ny=y+dy[i];
            if(nx<1||nx>n||ny<1||ny>m||!id[nx][ny]||vis[nx][ny]|(nx==ex&&ny==ey)) continue;
            vis[nx][ny]=1;
            l.push(point(nx,ny));
        }
    }
    memset(f,-1,sizeof(f));
    for(res i=0;i<4;i++){
        res nx=ex+dx[i],ny=ey+dy[i];
        if(nx<1||nx>n||ny<1||ny>n||!vis[nx][ny]) continue;
        f[ex][ey][i]=0;
        l.push(point(ex,ey,i));
    }
    memset(vis,0,sizeof(vis));
    vis[ex][ey]=1;
    while(!l.empty()){
        res x=l.front().x,y=l.front().y,op=l.front().op;
        l.pop();
        res nx=x+dx[3-op],ny=y+dy[3-op];
        vis[x][y]=1;
        if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&id[nx][ny]&&f[nx][ny][op]==-1)
          f[nx][ny][op]=f[x][y][op]+1,l.push(point(nx,ny,op));
        for(res i=0;i<4;i++){
            if(f[x][y][i]!=-1) continue;
            nx=x+dx[i],ny=y+dy[i];
            if(!id[nx][ny]) continue;
            if(!check(id[nx][ny],id[x+dx[op]][y+dy[op]])) continue;
            f[x][y][i]=f[x][y][op];
            l.push(point(x,y,i));
        }
    }
    for(;q;q--){
        res x=read(),y=read();
        vis[x][y]==1?puts("YES"):puts("NO"); 
    }
    return 0;
}