AcWing 1131.拯救大兵瑞恩 题目详解

放一个题目链接在这里1131. 拯救大兵瑞恩 - AcWing题库🤔

思路

这是一个要用到“拆点”的最短路问题,我们发现有这个钥匙的存在,直接用坐标表示一个点的位置所包含的信息量不够,这时候可以考虑再开一个维度比如dist[x][y][state],来表示现在站在了(x,y)这个位置,并且有用state表示的已经取到了。看眼题上说钥匙的种类只有10种,我们就可以用state的二进制表示来存信息,比如00 0000 0001,就表示了现在只有第一种类的钥匙在手上。

现在状态的转移就比较容易写了

动作1:捡钥匙

这是不需要时间的,dist[x][y][state_又捡起这个位置上的所有钥匙] = min(dist[x][y][state_又捡起这个位置上的所有钥匙] , dist[x][y][state])

动作2:走一格

没有墙,或者有门但有钥匙就可以走一步

dist[x+dx][y + dy][state] = min(dist[x][y][state] + 1 , dist[x + dx][y + dy][state])

这样似乎就可以愉快地dp了😝

但因为这个状态转移是有相互依赖关系的,也就是说不是一个拓扑的序(计算A状态之前A所依赖的所有状态的值都已经确定下来了)

举一个简单的例子:

在一个田字格里原地打转,这4个位置就相互依赖了。

所以我们要把这个dp用最短路的算法来解。

上面两种动作就可以看成一个图里的边,dist[x][y][state]就是一个点,现在要求的就是从dist[0][0][0]跑到dist[n - 1][m - 1][随便什么state]的最短路。边长我们看到是只有0和1,就可以用一个双端队列bfs来写(类似dijkstra,但是我们会用一下边只有0,1的性质把dijkstra里插入删除堆过程种排序的过程绕开了,就快一些)

代码

抄yxc的,他写的也太好了

# include<iostream>
# include<cstdio>
# include<algorithm>
# include<deque>
# include<set>
# include<cstring>

#define x first
#define y second

using namespace std;

typedef pair<int, int> PII;
const int N = 11, M = N * N, E = 400, P = 1 << 10;

int n, m, p, k;
int h[M], e[E], ne[E], w[E], idx;
int g[N][N], key[M]; // 记录当前位置有没有钥匙
int dist[M][P];
bool st[M][P];

set<PII> edges;

void add(int a, int b, int c)
{
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}

void build()
{
    int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
    
    for (int i = 1;i <= n;i ++ )
        for (int j = 1;j <= m; j++)
            for (int u = 0;u < 4;u ++)
            {
                int x = i + dx[u], y = j + dy[u];
                if(!x || x > n || !y || y > m) continue; // 没有出界
                int a = g[i][j], b = g[x][y];
                // 不是墙也不是门,是正常的边
                if(edges.count({a, b}) == 0) add(a, b, 0);
            }
}

int bfs()
{
    memset(dist, 0x3f, sizeof dist);
    dist[1][0] = 0;
    
    deque<PII> q;
    q.push_back({1 , 0});
    
    while(q.size())
    {
        PII t = q.front();
        q.pop_front();
        
        if (st[t.x][t.y]) continue;
        st[t.x][t.y] = true;
        
        if(t.x == m * n) return dist[t.x][t.y];
        
        if(key[t.x])
        {
            int state = t.y | key[t.x];
            if(dist[t.x][state] > dist[t.x][t.y])
            {
                dist[t.x][state] = dist[t.x][t.y];
                q.push_front({t.x, state});
            }
        }
        
        for (int i = h[t.x]; i != -1;i = ne[i])
        {
            int j = e[i];
            if(w[i] && !(t.y >> w[i] - 1 & 1)) continue;
            if(dist[j][t.y] > dist[t.x][t.y] + 1)
            {
                dist[j][t.y] = dist[t.x][t.y] + 1;
                q.push_back({j, t.y});
            }
        }
    }
    
    return -1;

}

int main()
{
    cin >> n >> m >> p >> k;
    
    for(int i = 1 , t = 1; i <= n; i++)
        for(int j = 1; j <= m;j ++)
            g[i][j] = t ++;
    
    memset(h, -1, sizeof h); // 初始化
    
    while(k --)
    {
        int x1, y1, x2, y2, c;
        cin >> x1 >> y1 >> x2 >> y2 >> c;
        int a = g[x1][y1], b = g[x2][y2];
        edges.insert({a, b}) , edges.insert({b, a});
        if(c) add(a, b, c), add(b, a, c);
    }
    
    build();
    
    int s;
    cin >> s;
    while(s --)
    {
        int x, y, id;
        cin >> x >> y >> id;
        key[g[x][y]] |= 1 << id - 1;
    }
    
    cout << bfs() << endl;
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值