Loading

NOIP 模拟 $98\; \rm 寻宝$

题解 \(by\;zj\varphi\)

先将联通的点缩点,并查集即可。

再在缩完后的点上连传送门,因为传送门很少,也就是边数很少,所以可以直接 \(dfs\),复杂度 \(\mathcal{O\rm(qk)}\)

Code
#include<bits/stdc++.h>
#define ri signed
#define pd(i) ++i
#define bq(i) --i
#define func(x) std::function<x>
namespace IO{
    char buf[1<<21],*p1=buf,*p2=buf;
    #define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?(-1):*p1++
    #define dg1(x) std::cerr << #x"=" << x << ' '
    #define dg2(x) std::cerr << #x"=" << x << std::endl
    #define Dg(x) assert(x)
    struct nanfeng_stream{
        template<typename T>inline nanfeng_stream &operator>>(T &x) {
            bool f=false;x=0;char ch=gc();
            while(!isdigit(ch)) f|=ch=='-',ch=gc();
            while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=gc();
            return x=f?-x:x,*this;
        }
    }cin;
}
using IO::cin;
namespace nanfeng{
    #define FI FILE *IN
    #define FO FILE *OUT
    template<typename T>inline T cmax(T x,T y) {return x>y?x:y;}
    template<typename T>inline T cmin(T x,T y) {return x>y?y:x;}
    static const int N=5e4+7;
    struct edge{int v,nxt;}e[N];
    int *ba[N],fa[N],first[N],st[N],t=1,cnt,n,m,k,q,al,x1,y1,x2,y2,aim;
    int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
    bool vis[N];
    char s[N];
    auto add=[](int u,int v) {e[t]={v,first[u]},first[u]=t++;};
    func(int(int)) find=[](int x) {return x==fa[x]?x:fa[x]=find(fa[x]);};
    auto merge=[](int x,int y) {
        x=find(x),y=find(y);
        if (x==y) return;
        fa[y]=x;
    };
    auto ch=[](int x,int y) {return (x-1)*m+y;};
    func(bool(int)) dfs=[](int x) {
        vis[st[++cnt]=x]=true;
        if (x==aim) return true;
        for (ri i(first[x]),v;i;i=e[i].nxt) 
            if (!vis[v=e[i].v]&&dfs(v)) return true;
        return false;
    };
    inline int main() {
        FI=freopen("treasure.in","r",stdin);
        FO=freopen("treasure.out","w",stdout);
        scanf("%d%d%d%d",&n,&m,&k,&q);
        for (ri i(1);i<=n;pd(i)) {
            scanf("%s",s+1);
            ba[i]=new int[m+3];
            for (ri j(1);j<=m;pd(j)) ba[i][j]=s[j]=='.';
        }
        al=n*m;
        for (ri i(1);i<=al;pd(i)) fa[i]=i;
        for (ri i(1);i<=n;pd(i))
            for (ri j(1);j<=m;pd(j)) {
                if (!ba[i][j]) continue;
                const int nw=ch(i,j);
                for (ri k(0);k<4;pd(k)) {
                    int ti=i+dx[k],tj=j+dy[k];
                    if (ti<1||ti>n||tj<1||tj>m||!ba[ti][tj]) continue;
                    merge(nw,ch(ti,tj));
                }
            }
        for (ri i(1);i<=k;pd(i)) {
            cin >> x1 >> y1 >> x2 >> y2;
            int u=find(ch(x1,y1)),v=find(ch(x2,y2));
            if (u==v) continue;
            add(u,v);
        }
        for (ri i(1);i<=q;pd(i)) {
            cin >> x1 >> y1 >> x2 >> y2;
            aim=find(ch(x2,y2));
            cnt=0;
            printf("%d\n",dfs(find(ch(x1,y1))));
            for (ri j(1);j<=cnt;pd(j)) vis[st[j]]=false;
        }
        return 0;
    }
}
int main() {return nanfeng::main();}
posted @ 2021-11-14 20:16  ナンカエデ  阅读(40)  评论(0编辑  收藏  举报