【启发式搜索】[POJ 1077]Eight
八数码问题,用的A*解决,但是数据太强A不了。。。TLE只能在POJ上先过一发了。
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <iostream>
#include <string>
using namespace std;
const int MAXVIS = 500000;
struct State{
unsigned short s[9];
int h, g, mh;
bool operator < (const State& s) const {
return (g+h)>(s.g+s.h);
}
bool operator > (const State& s) const {
if((g+h) == (s.g+s.h))
return g < (s.g+s.h);
return (g+h)<(s.g+s.h);
}
}st, ed;
priority_queue<State> que;
char now[MAXVIS+5]; int pre[MAXVIS+5];
int _pow[9]; pair<int, int> show[9];
int fx[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int ppos[9][2] = {{0, 0}, {0, 1}, {0, 2}, {1, 0}, {1, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2}};
int fppos[3][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
char ch[4] = {'d', 'r', 'u', 'l'};
bool vis[MAXVIS+5];
int _hash(unsigned short s[]){
int ret = 0, count =0 ;
for(int i=0;i<9;i++){
count = 0;
for(int j=i+1;j<9;j++)
count += int(s[j]<s[i]);
ret += count * _pow[8-i];
}
return ret;
}
int _abs(int u){return u>0?u:-u;}
int _h(unsigned short s[]){
int ret = 0;
for(int i=0;i<9;i++) if(s[i])
ret += _abs(ppos[i][0]-show[s[i]].first) + _abs(ppos[i][1]-show[s[i]].second);
return ret;
}
bool check(const State& s){
for(int i=0;i<9;i++)
if(s.s[i] != ed.s[i])
return false;
return true;
}
bool check_pos(const int& a, const int& b){
if(a > 2 || b > 2 || a < 0 || b < 0)
return false;
return true;
}
stack<char> sta;
void Print(){
while(!sta.empty()) sta.pop();
int nowm = ed.mh;
while(nowm != st.mh) {
sta.push(now[nowm]);
nowm = pre[nowm];
}
while(!sta.empty()){
putchar(sta.top());
sta.pop();
}
puts("");
}
void solve(){
int counter = 0;
for(int i=0;i<9;i++) if(st.s[i]){
for(int j=i+1;j<9;j++) if(st.s[j]){
if(st.s[i] > st.s[j]) counter++;
}
}
if(counter & 1){
cout<<"unsolvable"<<endl;
return ;
}
memset(vis, 0, sizeof vis);
while(!que.empty()) que.pop();
State tmp=st; State t2;
tmp.mh=_hash(st.s);
now[tmp.mh] = 0;
int pos;
vis[tmp.mh] = true;
tmp.h = _h(st.s); tmp.h = 0;
que.push(tmp);
while(!que.empty()){
tmp = que.top(); que.pop();
pos=0;
while(tmp.s[pos]) pos++;
for(int i=0;i<4;i++) {
if(check_pos(ppos[pos][0] + fx[i][0], ppos[pos][1] + fx[i][1])){
t2 = tmp;
swap(t2.s[pos], t2.s[fppos[ppos[pos][0] + fx[i][0]][ppos[pos][1] + fx[i][1]]]);
t2.mh = _hash(t2.s);
if(vis[t2.mh]) continue;
vis[t2.mh] = true; (++t2.g);
now[t2.mh] = ch[i]; pre[t2.mh] = tmp.mh;
que.push(t2);
if(t2.mh == ed.mh){
Print();
return ;
}
}
}
}
cout<<"unsolvable"<<endl;
}
int main(){
char tstr[5];
_pow[0] = 1;
for(int i=1;i<=8;i++) _pow[i] = _pow[i-1] * i;
ed.s[0] = 1; ed.s[1] = 2; ed.s[2] = 3; ed.s[3] = 4;
ed.s[4] = 5; ed.s[5] = 6; ed.s[6] = 7; ed.s[7] = 8;
ed.s[8] = 0;
ed.mh = _hash(ed.s);
for(int i=0;i<9;i++){show[ed.s[i]] = make_pair(i/3, i%3);}
while(true){
for(int i=0;i<9;i++){
if(scanf("%s", tstr) == EOF) return 0;
if(tstr[0] != 'x')
st.s[i]=tstr[0]-'0';
else st.s[i] = 0;
}
st.mh = _hash(st.s);
if(check(st)) puts("");
else solve();
}
return 0;
}