3 条题解

  • 1
    @ 2025-03-28 20:40:37

    #include <iostream>
    #include <cstdlib>
    #include <algorithm>
    #include <queue>
    #include <string>
    #include <map>
    #define INIT 400000
    using namespace std;

    struct eight
    {
    bool b;
    int sum;
    eight():b(false),sum(0)
    {

    }
    };
    int ans=INIT;
    map<string,eight> judge;
    queue<string> q;

    void BFS(const string& str)
    {
    string t;
    judge[str].b=true;
    q.push(str);
    while(q.empty()==false)
    {
    t=q.front();
    q.pop();
    if(t=="123804765")
    ans=min(ans,judge[t].sum);
    else if(judge[t].sum<ans)
    {
    int loc=t.find('0');
    int x=loc/3;
    int y=loc%3;
    int step=judge[t].sum;
    if(x-1>=0)
    {
    swap(t[loc],t[loc-3]);
    if(judge[t].b==false)
    {
    judge[t].b=true;
    judge[t].sum=step+1;
    q.push(t);
    }
    swap(t[loc],t[loc-3]);
    }
    if(y-1>=0)
    {
    swap(t[loc],t[loc-1]);
    if(judge[t].b==false)
    {
    judge[t].b=true;
    judge[t].sum=step+1;
    q.push(t);
    }
    swap(t[loc],t[loc-1]);
    }
    if(y+1<=2)
    {
    swap(t[loc],t[loc+1]);
    if(judge[t].b==false)
    {
    judge[t].b=true;
    judge[t].sum=step+1;
    q.push(t);
    }
    swap(t[loc],t[loc+1]);
    }
    if(x+1<=2)
    {
    swap(t[loc],t[loc+3]);
    if(judge[t].b==false)
    {
    judge[t].b=true;
    judge[t].sum=step+1;
    q.push(t);
    }
    swap(t[loc],t[loc+3]);
    }
    }
    }
    }

    int main()
    {
    string str;
    cin>>str;
    if(str=="123804765")
    cout<<0<<endl;
    else
    BFS(str);
    if(ans==INIT)
    cout<<-1<<endl;
    else
    cout<<ans<<endl;
    return 0;
    }
    //我尽力了!!!!

  • 1
    @ 2025-03-28 20:36:48

    #include <iostream>
    #include <queue>
    #include <cmath>
    #include <cstring>
    using namespace std;

    char target[10] = "123804765";
    int target_hash;
    const int pos[][2]={{1,1},{0,0},{0,1},{0,2},{1,2},{2,2},{2,1},{2,0},{1,0}};
    bool vis[500000];
    int hashs[10];

    struct Node {
    int f,g,h;
    char c[10];
    friend bool operator < (const Node &a,const Node &b){
    if(a.f == b.f) return a.g < b.g;
    return a.f > b.f;
    }

    };

    /*
    void print(const Node &n){
    cout<<"Node: "<<n.f<<" = "<<n.g<<" + "<<n.h<<endl;
    for(int i=0,j;i<3;i++){
    for(j=0;j<3;j++)
    cout<<n.c[i*3+j]<<" ";

    cout<<endl;
    }
    cout<<endl;
    }
    */

    int unmatch(const char * c){
    int cnt = 0;
    for(int i=0,j;i<3;i++){
    for(j=0;j<3;j++){
    int p = c[i*3+j] - '0';
    if(p == 0) continue;
    //if(abs(pos[p][0] - i) || abs(pos[p][1] - j))
    //cnt+=1;
    cnt+=abs(pos[p][0] - i) + abs(pos[p][1] - j);
    }
    }
    return cnt;
    }

    int cantor(const char * c){
    int cnt=0,tmp;
    for(int i=0,k;i<9;i++){
    tmp = 0;
    for(k=i-1;k>=0;k--){
    if(c[k]>c[i]) tmp++;
    }
    cnt += hashs[i] * tmp;
    }
    return cnt;
    }

    bool up(char * c){
    int p0=-1;
    while(1){
    if(c[++p0] == '0') break;
    }
    if(p0>5) return false;
    c[p0] = c[p0+3];
    c[p0+3] = '0';
    return true;
    }

    bool down(char * c){
    int p0=-1;
    while(1){
    if(c[++p0] == '0') break;
    }
    if(p0<3) return false;
    c[p0] = c[p0-3];
    c[p0-3] = '0';
    return true;
    }

    bool left(char * c){
    int p0=-1;
    while(1){
    if(c[++p0] == '0') break;
    }
    if(p0%3 == 2) return false;
    c[p0] = c[p0+1];
    c[p0+1] = '0';
    return true;
    }

    bool right(char * c){
    int p0=-1;
    while(1){
    if(c[++p0] == '0') break;
    }
    if(p0%3 == 0) return false;
    c[p0] = c[p0-1];
    c[p0-1] = '0';
    return true;
    }

    int search(Node n){
    target_hash = cantor(target);
    if(cantor(n.c) == target_hash) return 0;
    vis[cantor(n.c)]=true;
    memset(vis,0,sizeof(vis));
    priority_queue<Node> que;
    que.push(n);
    while(!que.empty()){
    Node curr = que.top();
    que.pop();

    Node set[4];
    set[0]=set[1]=set[2]=set[3]=curr;
    up(set[0].c);left(set[1].c);down(set[2].c);right(set[3].c);
    for(int i=0;i<4;i++){
    set[i].g=curr.g+1;
    set[i].h=unmatch(set[i].c);
    set[i].f=set[i].g+set[i].h;
    int t = cantor(set[i].c);
    //cout<<i<<" "<<t<<":"<<endl;
    //print(set[i]);
    if(t == target_hash) return set[i].g;
    if(!vis[t]){
    vis[t] = true;
    que.push(set[i]);
    //cout<<"pushed"<<endl<<endl;
    }
    }
    }
    }

    int main(){

    hashs[0]=1;
    for(int i=1;i<11;i++){
    hashs[i] = hashs[i-1] * i;
    }
    Node start;
    cin>>start.c;
    //strcpy(start.c,"203184765");
    start.g=0;
    start.h=unmatch(start.c);
    start.f=start.g+start.h;
    //print(start);
    cout<<search(start);
    return 0;
    }

  • 0
    @ 2025-03-28 20:42:59

    #include <iostream>
    #include <cstdlib>
    #include <algorithm>
    #include <queue>
    #include <string>
    #include <map>
    #define INIT 400000
    using namespace std;

    struct eight
    {
    bool b;
    int sum;
    eight():b(false),sum(0)
    {

    }
    };
    int ans=INIT;
    map<string,eight> judge;
    queue<string> q;

    void BFS(const string& str)
    {
    string t;
    judge[str].b=true;
    q.push(str);
    while(q.empty()==false)
    {
    t=q.front();
    q.pop();
    if(t=="123804765")
    ans=min(ans,judge[t].sum);
    else if(judge[t].sum<ans)
    {
    int loc=t.find('0');
    int x=loc/3;
    int y=loc%3;
    int step=judge[t].sum;
    if(x-1>=0)
    {
    swap(t[loc],t[loc-3]);
    if(judge[t].b==false)
    {
    judge[t].b=true;
    judge[t].sum=step+1;
    q.push(t);
    }
    swap(t[loc],t[loc-3]);
    }
    if(y-1>=0)
    {
    swap(t[loc],t[loc-1]);
    if(judge[t].b==false)
    {
    judge[t].b=true;
    judge[t].sum=step+1;
    q.push(t);
    }
    swap(t[loc],t[loc-1]);
    }
    if(y+1<=2)
    {
    swap(t[loc],t[loc+1]);
    if(judge[t].b==false)
    {
    judge[t].b=true;
    judge[t].sum=step+1;
    q.push(t);
    }
    swap(t[loc],t[loc+1]);
    }
    if(x+1<=2)
    {
    swap(t[loc],t[loc+3]);
    if(judge[t].b==false)
    {
    judge[t].b=true;
    judge[t].sum=step+1;
    q.push(t);
    }
    swap(t[loc],t[loc+3]);
    }
    }
    }
    }

    int main()
    {
    string str;
    cin>>str;
    if(str=="123804765")
    cout<<0<<endl;
    else
    BFS(str);
    if(ans==INIT)
    cout<<-1<<endl;
    else
    cout<<ans<<endl;
    return 0;
    }///////////////////////////对(666)

  • 1

信息

ID
1188
难度
10
分类
搜索 | 搜索与剪枝 点击显示
标签
递交数
20
已通过
0
通过率
0%
被复制
4
上传者