为什么AC了,但是样例4过不去?
查看原帖
为什么AC了,但是样例4过不去?
1185696
afreshmanofclanguage楼主2024/10/5 19:48

这是代码:

#include<iostream>
#include<map>
#include<queue>
using namespace std;
int s, a1, a2, a3, a4;
int arr[15][8]; // 状态数组,两维都是状态
vector<int>start;
vector<int>en = { 1,1,1,1,1,1,1,1,1,1,1,1 };
map<vector<int>, bool>h;
int f(vector<int>v) // 估价函数
{
    int sum = 0;
    for (auto item : v)
        sum += (5 - item) % 4;
    return sum;
}
typedef struct state
{
    vector<int>num; // 当前的门锁状态
    int d; //   到这个状态花费了多少权值
    vector<int>add; // 转动了什么按钮
    bool operator < (const struct state& other) const
    {
        return d + f(num) > other.d + f(other.num);
    }
}st;
struct st_cmp
{
    bool operator()(const struct state& p1, const struct state& p2) const
    {
        return p1 < p2;
    }
};
priority_queue<st, vector<st>, st_cmp>q;
pair<int, vector<int>> bfs()
{
    vector<int>v;
    q.push({ start,0,v });
    h[start] = 0;
    while (q.size())
    {
        auto t = q.top();
        q.pop();
        vector<int>tem, more;
        for (int i = 0; i < 12; i++)
        {
            tem = t.num;
            int next = arr[i][tem[i]] - 1;
            tem[next] = (tem[next] == 4 ? 1 : tem[next] + 1);
            tem[i] = (tem[i] == 4 ? 1 : tem[i] + 1);
            if (h[tem]) continue;
            more = t.add;
            more.push_back(i + 1);
            st son = { tem,t.d + 1,more };
            if (tem == en)
                return { son.d,more };
            h[tem] = 1;
            q.push(son);
        }
    }
    return { -1,v };
}
int main()
{
    for (int i = 0; i < 12; i++)
    {
        cin >> s >> a1 >> a2 >> a3 >> a4;
        start.push_back(s);
        arr[i][1] = a1;
        arr[i][2] = a2;
        arr[i][3] = a3;
        arr[i][4] = a4;
    }
    auto ans = bfs();
    cout << ans.first << endl;
    for (auto item : ans.second)
        printf("%d ", item);
    return 0;
}
2024/10/5 19:48
加载中...