神奇!!!
查看原帖
神奇!!!
284154
max9189楼主2021/8/8 09:43

我的代码提交上去55分(T了4个),开了O2竟然AC了 ?!

有没有大佬帮忙解释一下这是为什么

#include <iostream>
#include <cstdio>
#include <queue>
#include <map>
using namespace std;
struct node
{
    string s,ans;
}now,nex;
const int d[10][5] = {0,0,0,0,0,1,2,4,5,0,1,2,3,0,0,2,3,5,6,0,1,4,7,0,0,2,4,5,6,8,3,6,9,0,0,4,5,7,8,0,7,8,9,0,0,5,6,8,9,0};
map<string,bool> vis;
node bfs(string s)
{
    queue<node> q;
    now.s = s;
    q.push(now);
    vis[s] = 1;
    while(!q.empty())
    {
        now = q.front();
        q.pop();
        bool b = 1;
        for(int i = 1;i <= 9;i++)
            if(now.s[i]!='4'){b = 0;break;}
        if(b)return now;
        for(int i = 1;i <= 9;i++)
        {
            nex.s = now.s;
            for(int j = 0;j < 5;j++)
                nex.s[d[i][j]] = (nex.s[d[i][j]]-'0')%4+'1';
            if(vis[nex.s])continue;
            nex.ans = now.ans+(char)(i+'0')+' ';
            q.push(nex);
//            cout << nex.ans << endl;
            vis[nex.s] = 1;
        }
    }
    return now;
}
int main()
{
    string a = " ";
    for(int i = 1;i <= 9;i++)
    {
        int x;
        cin >> x;
        a+=(char)(x/3+'0');
    }
    cout << bfs(a).ans;
    return 0;
}

2021/8/8 09:43
加载中...