AcWing 179. 八数码 178. 第K短路 (A-star)

A-star 算法
在这里插入图片描述
在这里插入图片描述

算法证明略
做题步骤:
如果搜索空间过大,考虑使用A*算法,正常做真实距离,思考估价函数,验证估价函数的正确性,队列换成优先队列,新加一维 真实+估计,终点出队即为答案
AcWing 179. 八数码
题目链接
估价函数:当前状态种每个数与它的目标位置的曼哈顿距离之和
把数码存在解的条件:逆序对为偶数
估价函数涉及到曼哈顿距离的情况很多
代码:

#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef pair<int, string>PIS;
typedef pair<string, char>PSC;
int dx[] = {0, -1, 0, 1}, dy[] = {1, 0, -1, 0};
string op = {"ruld"};
string ed = {"12345678x"};
string ans;
int f(string s){
    int sum = 0;
    for (int i = 0; i < 9; i ++){
        if (s[i] == 'x') continue;
        int x = s[i] - '1';
        sum += abs(x / 3 - i / 3 ) + abs(x % 3 - i % 3);
    }
    return sum;
}
void bfs(string start){
    unordered_map<string, int>dis;
    unordered_map<string, PSC>pre;
    priority_queue<PIS, vector<PIS>, greater<PIS>>heap;
    heap.push({0 + f(start), start});
    //cout << f(start) <<endl;
    while (heap.size()){
        auto t = heap.top();
        heap.pop();
        string state = t.se;
        //cout << state << endl;
        if(state == ed) break;
        int idx;
        for (int i = 0; i < 9; i ++){
            if(state[i] == 'x') idx = i;
        }
        int x = idx / 3, y = idx % 3;
        for (int i = 0; i < 4; i ++){
            string now = state;
            int nx = x + dx[i], ny = y + dy[i];
            swap(now[idx], now[nx * 3 + ny]);
            if (nx < 0 || nx >= 3 || ny < 0 || ny >= 3) continue;
            if (dis[now] && dis[now] < dis[state] + 1) continue;
            dis[now] = dis[state] + 1;
            heap.push({dis[now] + f(now), now});
            pre[now] = {state, op[i]};
        }
    }

    while (ed != start){
        ans += pre[ed].se;
        ed = pre[ed].fi;
    }
    reverse(ans.begin(), ans.end());
}
int main(){
    string start, s;
    string c;
    while (cin >> c){
        start += c;
        if (c != "x") s += c;
    }
    int cnt = 0;
    for (int i = 0; i < 8; i ++){
        for (int j = i; j < 8; j ++){
            if (s[j] < s[i]) cnt++;
        }
    }
    if (cnt & 1) cout << "unsolvable" << endl;
    else {
        bfs(start);
        cout << ans <<endl;
    }
    return 0;
}

178. 第K短路
题目链接
估价函数:每个点到终点的最短距离(反向跑一边dij)
第k短路 -> 弹出顺序
注意起点和终点相同的情况
代码:

#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef pair<int, int>PII;
typedef pair<int, PII>PIII;
const int N = 2e5+10;
int h[N], rh[N], e[N], ne[N], w[N], idx;
int n, m;
int s, ed, k;
int dis[N], cnt[N];
bool st[N];
void add(int h[], int a, int b, int c){
    e[idx] = b;
    w[idx] = c;
    ne[idx] = h[a];
    h[a] = idx++;
}
void dij(){
    memset(dis, 0x3f, sizeof(dis));
    dis[ed] = 0;
    priority_queue<PII, vector<PII>, greater<PII>>heap;
    heap.push({0,ed});
    while (heap.size()){
        auto t = heap.top();
        heap.pop();
        int u = t.se;
        if (st[u]) continue;
        st[u] = true;
        for (int i = rh[u]; i != -1; i = ne[i]){
            int j = e[i];
            if (dis[j] > t.fi + w[i]){
                dis[j] = t.fi + w[i];
                heap.push({dis[j], j});
            }
        }
    }
}
int Astar(){
    priority_queue<PIII, vector<PIII>, greater<PIII>>heap;
    heap.push({dis[s], {0, s}});
    while (heap.size()){
        auto t = heap.top();
        heap.pop();
        int u = t.se.se, distance = t.se.fi;
        cnt[u]++;
        if (cnt[ed] == k) return distance;
        for (int i = h[u]; i != -1; i = ne[i]){
            int j = e[i];
            if (cnt[j] >= k) continue;//提前剪枝,减少搜索数量
            heap.push({distance + w[i] +dis[j], {distance + w[i], j}});
        }
    }
    return -1;
}
int main(){
    memset(rh, -1, sizeof(rh));
    memset(h, -1, sizeof(h));
    cin >> n >> m;
    for(int i = 0; i < m; i ++){
        int a, b, c;
        cin>> a >> b >>c;
        add(h, a, b, c);
        add(rh, b, a, c);//建反图
    }
    cin >> s >> ed >> k;
    if(s == ed) k ++;//特判起点和终点相同的情况
    dij();
    cout << Astar() << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值