首页 > 其他分享 >Poj 1077 Eight!(BFS-A*)

Poj 1077 Eight!(BFS-A*)

时间:2024-02-04 16:34:46浏览次数:22  
标签:dist string int 1077 BFS ++ state Eight ans

1077 -- Eight (poj.org)

由结论可以知道逆序对为奇数的时候无解,f为估值函数,计算曼哈顿距离。

想用康托展开写,但多状态数码问题用数组存储状态不够,有TLE的风险,还是A*吧!

吃一个tomato宣告今日...不知道结不结束

#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
using namespace std;
const int dx[] = {-1,0,1,0},dy[] = {0,1,0,-1};
const char op[] = "urdl";
typedef pair <int,string> pii;
char ch;
string start,s;
int f (string state) {
    int ans = 0;
    for (int i = 0;i < 9;i++) {
        if (state[i] == 'x') continue;
        int t = state[i] - '1';
        ans += abs (i / 3 - t / 3) + abs (i % 3 - t % 3);
    }
    return ans;
}
string Astar () {
    string end = "12345678x";
    unordered_map <string,int> dist;
    unordered_map <string,pair <char,string>> prev;
    priority_queue <pii,vector <pii>,greater <pii>> heap;
    dist[start] = 0;
    heap.push ({f(start),start});
    while (!heap.empty ()) {
        auto t = heap.top ();
        heap.pop ();
        string state = t.second;
        if (state == end) break;
        int x,y;
        for (int i = 0;i < 9;i++) {
            if (state[i] == 'x') {
                x = i / 3,y = i % 3;
                break;
            }
        }
        string source = state;
        for (int i = 0;i < 4;i++) {
            int a = x + dx[i],b = y + dy[i];
            if (a < 0 || a > 2 || b < 0 || b > 2) continue;
            swap (state[a*3+b],state[x*3+y]);
            if (!dist.count (state) || dist[state] > dist[source] + 1) {
                dist[state] = dist[source] + 1;
                prev[state] = {op[i],source};
                heap.push ({dist[state] + f(state),state});
            }
            state = source;
        }
    }
    string ans;
    while (end != start) {
        ans = prev[end].first + ans;
        end = prev[end].second;
    }
    return ans;
}
signed main () {
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    for (int i = 0;i < 9;i++) {
        cin >> ch;
        start += ch;
        if (ch != 'x') s += ch;
    }
    int cnt = 0;
    for (int i = 0;i < 8;i++) {
        for (int j = i;j < 8;j++) {
            if (s[i] > s[j]) cnt++;
        }
    }
    if (cnt & 1) puts ("unsolvable");
    else cout << Astar () << endl;
    return 0;
}

 

标签:dist,string,int,1077,BFS,++,state,Eight,ans
From: https://www.cnblogs.com/accbulb/p/18006461

相关文章

  • hdu 1312 Red and Black (BFS模板题)
    Problem-1312(hdu.edu.cn)BFS模板题#include<iostream>#include<queue>usingnamespacestd;typedeflonglongll;constintINF=0x3f3f3f3f;intwx,hy,num;charroom[25][25];#defineCHECK(x,y)(x>=0&&x<wx&&y>=0&&am......
  • 【阅读笔记】对比度增强-《Efficientcontrast enhancement using adaptive gamma corr
    2013年发表在TIP上的对比度增强算法AGCWD(Efficientcontrastenhancementusingadaptivegammacorrectionwithweightingdistribution)提出了一种自动映射技术,通过亮度像素的伽马校正和概率分布来提高调暗图像的亮度。为了增强视频,所提出的图像增强方法使用关于每帧之间差异的......
  • 【阅读笔记】对比度增强-《Efficientcontrast enhancement using adaptive gamma corr
    2013年发表在TIP上的对比度增强算法AGCWD(Efficientcontrastenhancementusingadaptivegammacorrectionwithweightingdistribution)提出了一种自动映射技术,通过亮度像素的伽马校正和概率分布来提高调暗图像的亮度。为了增强视频,所提出的图像增强方法使用关于每帧之间差异的......
  • hdu1240 Asteroids! (三维BFS)
    Problem-1240(hdu.edu.cn)三维的BFS,存在一个坐标点的变换,即输入的是(y,z,x),进行转换即可#include<iostream>#include<queue>#include<cstring>usingnamespacestd;intn,x1,y1,z1,x2,y2,z2,flag,vis[20][20][20];charroom[20][20][20];structnode{int......
  • Poj 3414 Pots (BFS+回溯+队列)
    这道题需要输出最后结果的执行过程,可以通过结构体,在结构体中定义一个数组s,s中存储了每一步的执行过程,实现了回溯。并且在运行中可以适当剪枝,减少枚举次数。 #include<iostream>#include<queue>#include<cstring>usingnamespacestd;constintN=110;intaa,bb,cc,vis[N......
  • 2024-02-03:用go语言,你有 k 个背包。给你一个下标从 0 开始的整数数组 weights, 其中 we
    2024-02-03:用go语言,你有k个背包。给你一个下标从0开始的整数数组weights,其中weights[i]是第i个珠子的重量。同时给你整数k,请你按照如下规则将所有的珠子放进k个背包。没有背包是空的。如果第i个珠子和第j个珠子在同一个背包里,那么下标在i到j之间的所有珠......
  • Poj 3278 Catch That Cow (BFS+队列)
    #include<iostream>#include<queue>#include<cstring>usingnamespacestd;constintN=1e5+10;intn,k,line[N],way;structnode{intloc,step;};queue<node>q;voidBFS(intn,intk){while(!q.empty())q.pop();nodestart,next......
  • Poj3126 Prime Path (BFS+筛素数)
    #include<iostream>#include<queue>#include<cstring>constintN=10010;intt,aa,bb,prime[N],vis[N],step[N];usingnamespacestd;voidprime_(){//埃式筛法prime[0]=prime[1]=1;for(inti=2;i<10000;i++){if(prime[i])contin......
  • 享元模式(FlyWeight Pattern)
    享元模式(FlyWeightPattern) 概要 记忆关键字:细粒度、共享 定义:运用共享技术有效地支持大量细粒度的对象 类型:结构型 分析:共享对象,将对象的一部分状态(内部状态)设计成可共享的,以减少对象的数量,达到节省内存的目的。 UML类图如下:一、涉及的角色1. 抽象享元类......
  • 在Tkinter中,`Frame`的大小可以通过多种方式进行调整: 1. **设置宽度和高度**:在创建`Fr
    在Tkinter中,`Frame`的大小可以通过多种方式进行调整:1.**设置宽度和高度**:在创建`Frame`时,可以直接设置其宽度(`width`)和高度(`height`)¹⁴。例如:  ```python  frame=tk.Frame(root,width=200,height=100)  frame.pack()  ```2.**自适应窗口大小**:可以使......