bfs能过,A*就只有60,为什么啊 (https://www.luogu.com.cn/record/184515460) 求大佬帮调谢谢
#include <bits/stdc++.h>
#define __init ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
typedef long long ll;
struct node {
int state[3][3];
ll H = 0, ns = 0;
ll step;
bool operator<(const node &a) const {
return (this->H+this->step) > (a.H+a.step);
}
}now;
using namespace std;
unordered_map<ll, ll> rec;
int tl[3][3];
const int el[3][3] = {{9, 1, 2}, {3, 4, 5}, {6, 7, 8}};
const int lindex[9][2] = {{0, 1}, {0, 2}, {1, 0}, {1, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2}, {0, 0}};
inline void M1(node &a) {
int tmp = a.state[0][0];
a.state[0][0] = a.state[1][0];
a.state[1][0] = a.state[2][0];
a.state[2][0] = a.state[2][1];
a.state[2][1] = a.state[2][2];
a.state[2][2] = a.state[1][2];
a.state[1][2] = a.state[0][2];
a.state[0][2] = a.state[0][1];
a.state[0][1] = tmp;
}
inline void M2(node &a) {
int tmp = a.state[1][0];
a.state[1][0] = a.state[1][2];
a.state[1][2] = a.state[1][1];
a.state[1][1] = tmp;
}
inline ll Con(int st[3][3]) {
ll tmpn = 0;
for(int i = 0;i < 3;i++) {
for(int j = 0;j < 3;j++) {
tmpn += st[i][j];
tmpn = (tmpn<<1) + (tmpn<<3);
}
}
return tmpn/10;
}
inline void Print(ll num) {
int l[9];
for(int i = 8;i >= 0;i--) {
l[i] = num % 10;
if(l[i] == 9) l[i] = 0;
num /= 10;
}
for(int i = 1;i <= 9;i++) {
cout << l[i-1] <<' ';
if(i % 3 == 0) cout << endl;
}
cout << endl;
}
inline void Bfs() {
//queue<node> q;
priority_queue<node> q;
node next;
now.ns = Con(now.state);
q.push(now);
while(!q.empty()) {
now = q.top();
//now = q.front();
q.pop();
for(int i = 0;i < 2;i++) {
memcpy(&next, &now, sizeof(struct node));
i == 0 ? M1(next) : M2(next);
next.step++;
next.ns = Con(next.state);
if(!rec[next.ns]) {
rec[next.ns] = now.ns;
if(next.ns == 912345678) {
cout << next.step << endl;
ll print[next.step];
for(int i = 0;i < 3;i++) {
for(int j = 0;j < 3;j++)
cout << (tl[i][j] == 9 ? 0 : tl[i][j]) << ' ';
cout << endl;
}
cout << endl;
print[next.step-1] = 912345678;
ll tn = rec[912345678];
for(int i = next.step-2;i >= 0;i--) {
print[i] = tn;
tn = rec[tn];
}
for(int i = 0; i < next.step;i++) {
Print(print[i]);
cout << endl;
}
return;
}
next.H = 0;
for (int x = 0; x < 3; x++)
for (int y = 0; y < 3; y++)
next.H += abs(x - lindex[next.state[x][y] - 1][0]) + abs(y - lindex[next.state[x][y] - 1][1]);
q.push(next);
}
}
}
cout << "UNSOLVABLE";
}
int main() {
__init;
for(int i = 0;i < 3;i++) {
for(int j = 0;j < 3;j++) {
cin >> now.state[i][j];
if(now.state[i][j] == 0) now.state[i][j] = 9;
}
}
memcpy(tl, now.state, sizeof(tl));
Bfs();
return 0;
}