栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

i d x ∗ + 剪 枝 + 迭 代 加 深 idx * + 剪枝 + 迭代加深 idx∗+剪枝+迭代加深
#include
using namespace std;
using pis = pair;

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 
#include 
#include 
#include 
#include 
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 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, vector>, greater> > q;
    unordered_map dist;
    unordered_map 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;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/976367.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号