80分,5和6测试点wa,求大佬帮忙看一下代码
  • 板块P5507 机关
  • 楼主Chaiduo2001
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/10/8 11:49
  • 上次更新2024/10/8 17:47:44
查看原帖
80分,5和6测试点wa,求大佬帮忙看一下代码
1092816
Chaiduo2001楼主2024/10/8 11:49
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <unordered_map>
using namespace std;
typedef pair<int, string> PIS;

int cv[13][5];
string s, t = "111111111111";
unordered_map<string, int> dis;
unordered_map<string, PIS> pre;
priority_queue<PIS, vector<PIS>, greater<PIS>> q;

int f(string c) {
    int cnt = 0;
    for(int i = 0; i < 12; i++) {
        if(c[i] != '1') cnt += ('5' - c[i]);
    }
    return cnt / 2 + (cnt % 2);
}

void a_star() {
    q.push({f(s), s});
    dis[s] = 0;
    while(!q.empty()) {
        string cur = q.top().second;
        q.pop();
        if(cur == t) break;
        for(int i = 0; i < 12; i++) {
            if(cur[i] == '1') continue;
            int j = cv[i][cur[i] - '0'] - 1;
            string p = cur;
            p[i]++; p[j]++;
            if(p[i] == '5') p[i] = '1';
            if(p[j] == '5') p[j] = '1';
            if(!dis.count(p)) {
                dis[p] = dis[cur] + 1;
                pre[p] = {i + 1, cur};
                q.push({dis[p] + f(p), p});
            }
        }
    }
}
int main()
{
    for(int i = 0; i < 12; i++) 
        for(int j = 0; j <= 4; j++) 
            cin >> cv[i][j]; 
    for(int i = 0; i < 12; i++) s += to_string(cv[i][0]);

    a_star();

    cout << dis[t] << endl;
    vector<int> op;
    while(t != s) {
        op.push_back(pre[t].first);
        t = pre[t].second;
    }
    for(int i = op.size() - 1; i >= 0; i--) cout << op[i] << " ";
    return 0;
}
2024/10/8 11:49
加载中...