AT_abc431_e [ABC431E] Reflection on Grid

· · 题解

分享一个很好写的做法。

Solution

考虑建图跑最短路。

钦定一个网格拥有两条边:右边的边,下面的边。将右边的边记为 0,下面的边记为 1。即 (x,y,0) 表示网格 (x,y) 右边的边,(x,y,1) 表示网格 (x,y) 下面的边。

假设当前网格为 (i,j)。

对于类型 A:

同理,对于类型 B:

对于类型 C:

上述建边的边权为:若走法为类型 A,但网格 (i,j) 不为字符A,那么边权为 1,否则边权为 0。其他类型同理。

连完边就可以跑最短路了。记左上角网格的坐标为 (1,1),右下角网格的坐标为 (n,m):

最短路直接拖的 Dijkstra 板子,所以复杂度为 O(nm \log nm)。

写法上,可以将每个网格进行标号,方便计算节点编号。具体可以参考代码,很好写。

Code

AC记录

:::success[代码]

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const ll LLINF=0x3f3f3f3f3f3f3f3fLL;
const int MAX=8e5+10;
struct Dijkstra
{
    #define type int
    const type inf=INF;
    struct node
    {
        int id;
        type v;
        friend bool operator <(node a,node b){return a.v>b.v;}
    };
    static const int N=MAX;
    vector<node> mp[N];
    type dis[N];
    int n,vis[N];
    void init(int _n)
    {
        n=_n;
        for(int i=0;i<=n;i++) mp[i].clear();
    }
    void add_edge(int x,int y,type v){mp[x].push_back({y,v});}
    void work(int s)
    {
        int i,to;
        type w;
        priority_queue<node> q;
        for(i=0;i<=n;i++)
        {
            dis[i]=inf;
            vis[i]=0;
        }
        dis[s]=0;
        q.push({s,type(0)});
        while(!q.empty())
        {
            node t=q.top();
            q.pop();
            if(vis[t.id]) continue;
            vis[t.id]=1;// this node has already been extended
            for(auto &it:mp[t.id])
            {
                to=it.id;
                w=it.v;
                if(dis[to]>dis[t.id]+w)
                {
                    dis[to]=dis[t.id]+w;
                    if(!vis[to]) q.push({to,dis[to]});
                }
            }
        }
    }
    #undef type
}dij;
char s[MAX];
void add_edge(int a,int b,int x,
              int c,int d,int y,
              int w,vector<vector<int>>& id)
{
    dij.add_edge(id[a][b]+x,id[c][d]+y,w);
    dij.add_edge(id[c][d]+y,id[a][b]+x,w);
}
int main()
{
    int T,n,m,i,j,tot;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        vector<vector<int>> id(n+2,vector<int>(m+2,0));
        vector<string> mp;
        mp.push_back("");
        for(i=1;i<=n;i++)
        {
            scanf("%s",s);
            mp.push_back(" "+string(s));
        }
        tot=1;
        for(i=0;i<=n;i++)
        {
            for(j=0;j<=m;j++)
            {
                id[i][j]=tot;
                tot+=2;
            }
        }
        dij.init(tot+2);
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                // right=0, down=1
                add_edge(i,j-1,0,i,j,0,mp[i][j]!='A',id);
                add_edge(i-1,j,1,i,j,1,mp[i][j]!='A',id);
                add_edge(i,j-1,0,i,j,1,mp[i][j]!='B',id);
                add_edge(i-1,j,1,i,j,0,mp[i][j]!='B',id);
                add_edge(i,j-1,0,i-1,j,1,mp[i][j]!='C',id);
                add_edge(i,j,1,i,j,0,mp[i][j]!='C',id);
            }
        }
        dij.work(id[1][0]+0);
        printf("%d\n",dij.dis[id[n][m]+0]);
    }
    return 0;
}

:::