【启发式搜索】[ZOJ1217]Eight

题目描述

Scenario

The 15-puzzle has been around for over 100 years; even if you don’t know it by that name, you’ve seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all packed into a 4 by 4 frame with one tile missing. Let’s call the missing tile ‘x’; the object of the puzzle is to arrange the tiles so that they are ordered as:

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 x

where the only legal operation is to exchange ‘x’ with one of the tiles with which it shares an edge. As an example, the following sequence of moves solves a slightly scrambled puzzle:

1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
5 6 7 8 5 6 7 8 5 6 7 8 5 6 7 8
9 x 10 12 9 10 x 12 9 10 11 12 9 10 11 12
13 14 11 15 13 14 11 15 13 14 x 15 13 14 15 x
r-> d-> r->

The letters in the previous row indicate which neighbor of the ‘x’ tile is swapped with the ‘x’ tile at each step; legal values are ‘r’,’l’,’u’ and ‘d’, for right, left, up, and down, respectively.

Not all puzzles can be solved; in 1870, a man named Sam Loyd was famous for distributing an unsolvable version of the puzzle, and
frustrating many people. In fact, all you have to do to make a regular puzzle into an unsolvable one is to swap two tiles (not counting the missing ‘x’ tile, of course).

In this problem, you will write a program for solving the less well-known 8-puzzle, composed of tiles on a three by three
arrangement.

题目分析

就是根据每两个位置之间的曼哈顿距离作为估值函数。。。然后用康拓展开判重。

代码

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <iostream>
#include <string>
using namespace std;
const int MAXVIS = 363900;
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'};
int 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_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, 0x3f, sizeof vis);
    while(!que.empty()) que.pop();
    State tmp=st; State t2;
    now[tmp.mh] = 0;
    int pos;
    vis[tmp.mh] = tmp.h = _h(st.s); tmp.g = 0;
    que.push(tmp);
    while(!que.empty()){
        tmp = que.top(); que.pop();
        pos=0;
        if(tmp.mh == ed.mh){
            Print();
            return ;
        }
        if(tmp.g + tmp.h > vis[tmp.mh]) continue;
        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); t2.h = _h(t2.s); (++t2.g);
                if(t2.g + t2.h > vis[t2.mh]) continue;
                vis[t2.mh] = t2.g + t2.h;
                now[t2.mh] = ch[i]; pre[t2.mh] = tmp.mh;
                que.push(t2);
            }
        }
    }
    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(st.mh == ed.mh) puts("");
        else solve();
    }

    return 0;
}

posted on 2016-01-30 19:43  JeremyGuo  阅读(317)  评论(0编辑  收藏  举报

导航