八数码 (ida* + 剪枝 + 迭代加深) / (A*)

在这里插入图片描述

i d a ∗ + 剪 枝 + 迭 代 加 深 ida * + 剪枝 + 迭代加深 ida++

#include<bits/stdc++.h>
using namespace std;
using pis = pair<int, string>;

int dx[] = {1, 0, 0, -1}, dy[] = {0, -1, 1, 0};
char d[] = {'d', 'l','r', 'u'};//按照字典序排列

char s[10], ed[10];
int dep, x, y;
int edx[9], edy[9], path[100];

bool dfs(int u, int ida){
    if(u + ida > dep) return 0;//剪枝1:现有的深度和估价出的深度要超过最大深度的话就直接return
    if(!strcmp(s, ed)){//如果 s == ed
        cout << u << '\n';
        for(int i = 0; i < u; i++) cout << d[path[i]];
        cout << '\n';
        return 1;
    }

    int tmpx = x, tmpy = y;
    for(int i = 0; i < 4; i++){
        if(u && path[u - 1] + i == 3) continue;//剪枝2:不走反方向
        int nx = tmpx + dx[i], ny = tmpy + dy[i];
        if(nx >= 0 && ny >= 0 && nx < 3 && ny < 3){
            int a = s[nx * 3 + ny] - '1', ret = 0;//a:X即将走的位置的数字,ret:ida的改变量
            ret += abs(tmpx - edx[a]) - abs(nx - edx[a]) + abs(tmpy - edy[a]) - abs(ny - edy[a]);//
            x = nx, y = ny, path[u] = i;//更新x,y,path

            swap(s[nx * 3 + ny], s[tmpx * 3 + tmpy]);//更新s
            if(dfs(u + 1, ida + ret)) return 1;
            swap(s[nx * 3 + ny], s[tmpx * 3 + tmpy]);//还原s
        }
    }
    x = tmpx, y = tmpy;//还原x,y,
    return 0;
}
int main(){
    ios::sync_with_stdio(0); cin.tie(0);
    int tt; cin >> tt;
    for(int i = 1; i <= tt; i++){
        cout << "Case " << i << ": ";
        for(int i = 0; i < 9; i++) cin >> s[i];
        for(int i = 0; i < 9; i++) cin >> ed[i];

        for(int i = 0; i < 9; i++){
            if(ed[i] != 'X') edx[ed[i] - '1'] = i / 3, edy[ed[i] - '1'] = i % 3;//记录每一位的x,y值

            if(s[i] == 'X') x = i / 3, y = i % 3;//初始化位置x,y
        }
        int ida = 0;
        for(int i = 0; i < 9; i++)
            if(s[i] != 'X') ida += abs(i / 3 - edx[s[i] - '1']) + abs(i % 3 - edy[s[i] - '1']);
        for(dep = 0; ;dep++) if(dfs(0, ida)) break;
    }

    return 0;
}

A ∗ A * A (数据大了会tle)


#include <iostream>
#include <unordered_map>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

string sta, ed;
int dx[] = {1, 0, 0, -1}, dy[] = {0, -1, 1, 0};
char path[] = {'d', 'l', 'r', 'u'};


int f(string x, string y)
{
    unordered_map<char, int> mp;
    
    for (int i = 0;i < (int)y.size();i ++ )
        mp[y[i]] = i;
        
    int res = 0;
    for (int i = 0;i < (int)x.size();i ++ )
        if (x[i] != 'X')
        {
            int t = x[i] - '1';
            res += abs(mp[x[i]] / 3 - t / 3) + abs(mp[x[i]] % 3 - t % 3);
        }
        
    return res;
}


void bfs()
{
    cin >> sta >> ed;
    priority_queue<pair<int, string>, vector<pair<int, string>>, greater<pair<int, string>> > q;
    unordered_map<string, int> dist;
    unordered_map<string, string> pa;
    
    dist[sta] = 0;
    q.push({f(sta, ed), sta});
    
    while (q.size())
    {
        auto t = q.top(); q.pop();
        
        string state = t.second;
      //  cout << state;   
        if (state == ed){
          // cout << " YES \n"; 
          //cout << state << endl;
            cout << pa[state].size() << endl;
            cout << pa[state] << endl;
            return;
        } 
        
        int step = t.first;
        
        int x, y;
        for (int i = 0;i < state.size();i ++ )
            if (state[i] == 'X') 
            {
                x = i / 3, y = i % 3;
                //cout << i ;
                
                break;
            }
        // cout << "::::" << x << ' ' << y << endl;
        string s = state;
        for (int i = 0;i < 4;i ++ )
        {  
            int a = dx[i] + x, b = y + dy[i];
           
            if (a >= 0 && a < 3 && b >= 0 && b < 3)
            {
                
                swap(s[a * 3 + b], s[x * 3 + y]);
                if (!dist.count(s) || dist[s] > step + 1)
                {
                  // cout << a << ' ' << b << endl;
                    dist[s] = step + 1;
                    pa[s] = pa[state] + path[i];
                  // cout << pa[s] << endl;
                    q.push({f(s, ed) + dist[s], s});
                }       
                swap(s[a * 3 + b], s[x * 3 + y]);
                
            }
        }
    }
}


int main()
{
    int T; cin >> T;
    for (int i = 1;i <= T;i ++ )
    {
        cout << "Case " << i << ": " ;
        bfs();
    }
    
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

璀璨的秋叶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值