hdu2567 EightII(IDA*/逆向BFS打表)

7 篇文章 0 订阅

Eight II
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 130000/65536 K (Java/Others)
Total Submission(s): 5014 Accepted Submission(s): 1096

Problem Description
Eight-puzzle, which is also called “Nine grids”, comes from an old game.

In this game, you are given a 3 by 3 board and 8 tiles. The tiles are numbered from 1 to 8 and each covers a grid. As you see, there is a blank grid which can be represented as an ‘X’. Tiles in grids having a common edge with the blank grid can be moved into that blank grid. This operation leads to an exchange of ‘X’ with one tile.

We use the symbol ‘r’ to represent exchanging ‘X’ with the tile on its right side, and ‘l’ for the left side, ‘u’ for the one above it, ‘d’ for the one below it.

A state of the board can be represented by a string S using the rule showed below.

The problem is to operate an operation list of ‘r’, ‘u’, ‘l’, ‘d’ to turn the state of the board from state A to state B. You are required to find the result which meets the following constrains:

  1. It is of minimum length among all possible solutions.
  2. It is the lexicographically smallest one of all solutions of minimum length.

Input
The first line is T (T <= 200), which means the number of test cases of this problem.

The input of each test case consists of two lines with state A occupying the first line and state B on the second line.
It is guaranteed that there is an available solution from state A to B.

Output
For each test case two lines are expected.

The first line is in the format of “Case x: d”, in which x is the case number counted from one, d is the minimum length of operation list you need to turn A to B.
S is the operation list meeting the constraints and it should be showed on the second line.

Sample Input
2
12X453786
12345678X
564178X23
7568X4123

Sample Output
Case 1: 2
dd
Case 2: 8
urrulldr

A*不太能处理字典序的问题,靠入队顺序没办法判,比如说一个字典序不靠前的移动序列过早的少估了,那么最后结果就是错的。。。相比起来IDA*有先天优势。同时有些大常数的剪枝没什么卵用
可以不连续的加深,需要维护一个估计的最小深度。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
const LL mod=1e9+7;
const double eps=1e-9;
string Input;
char str[10];
int dx[4]={1,0,0,-1};
int dy[4]={0,-1,1,0};
char nxtdir[5]="dlru";
char predir[5]="urld";
int is[500000];
//int tt[500000];
int pos[10];
stack<char>ans;
struct node{
    char mp[10];
    int x,y,h,hsh;
}st;
int cantor(char *s){
    int ans=0;
    for(int i=0;i<8;++i){
        for(int j=i+1;j<9;++j)if(s[j]<s[i])ans++;
        ans*=(8-i);
    }
    return ans;
}
inline int trans(int x,int y){return 3*(x-1)+y-1;}
int get_h(char *s){
    int h=0;
    for(int i=0;i<9;i++){
        if(s[i]!=9)h+=(abs(i/3-pos[s[i]]/3)+abs(i%3-pos[s[i]]%3));
    }
    return h;
}
int debug(){
    for(int i=0;i<9;i++)cout<<(int)st.mp[i];
    cout<<' '<<st.h<<endl;
}
int IDA(int mxd,int d,int pre=-1){
    //debug();
    if(!st.h)return 1;
    if(d+st.h>mxd||is[st.hsh]<d)return 0;
    //if(tt[st.hsh]==mxd&&is[st.hsh]==d))return 0;
    //tt[st.hsh]=mxd;
    //is[st.hsh]=d;
    node tmp=st;
    for(int i=0;i<4;i++){
        if(i+pre==3)continue;
        st.x=tmp.x+dx[i];
        st.y=tmp.y+dy[i];
        if(st.x>3||st.x<1||st.y<1||st.y>3){st=tmp;continue;}
        swap(st.mp[trans(st.x,st.y)],st.mp[trans(tmp.x,tmp.y)]);
        //st.hsh=cantor(st.mp);
        st.h=get_h(st.mp);
        if(IDA(mxd,d+1,i)){ans.push(nxtdir[i]);return 1;}
        st=tmp;
    }
    //debug();
    return 0;
}
void store(string p,node &s){
    int k=0;
    for(int i=0;i<Input.length();i++){
        if(Input[i]=='X'){
            Input[i]='9';
            s.x=k/3+1;
            s.y=k%3+1;
        }
        if(Input[i]==' ')continue;
        s.mp[k++]=Input[i]-'0';
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin>>T;
    for(int cas=1;cas<=T;cas++){
        node ed;
        cin>>Input;
        store(Input,st);
        cin>>Input;
        store(Input,ed);
        for(int i=0;i<9;i++)pos[ed.mp[i]]=i;
        st.h=get_h(st.mp);
        st.hsh=cantor(st.mp);
        memset(is,INF,sizeof is);
        //memset(tt,-1,sizeof tt);
        for(int i=st.h;!IDA(i,0);i++){}
        cout<<"Case "<<cas<<": "<<ans.size()<<endl;
        while(!ans.empty()){cout<<ans.top();ans.pop();}
        cout<<endl;
    }
    return 0;
}

逆向bfs的思路主要是把原串和目标串重新映射一下,使目标串被归约到九种情况,用正常的逆向bfs就可以做了(写完以后反过来一想感觉逆向是没有必要的,规约初始串是一样的。。。)

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
const LL mod=1e9+7;
const double eps=1e-9;
string Input;
char str[10];
int dx[4]={1,0,0,-1};
int dy[4]={0,-1,1,0};
char nxtdir[5]="dlru";
char predir[5]="urld";
int nxt[10][500000];
int op[10][500000];
int vis[10][500000];
int pos[10];
struct node{
    char mp[10];
    int x,y,d;
};
int cantor(char *s){
    int ans=0;
    for(int i=0;i<8;++i){
        for(int j=i+1;j<9;++j)if(s[j]<s[i])ans++;
        ans*=(8-i);
    }
    return ans;
}
int trans(int x,int y){
    return 3*(x-1)+y-1;
}
void bfs(int id,node st){
    int sthsh=cantor(st.mp);
    nxt[id][sthsh]=-1;st.d=0;
    queue<node>mq;
    mq.push(st);vis[id][sthsh]=st.d;
    while(!mq.empty()){
        node nw=mq.front();
        mq.pop();int prehsh=cantor(nw.mp);
        for(int i=3;i>=0;i--){
            node pp=nw;
            int nxtx=pp.x+dx[i];
            int nxty=pp.y+dy[i];
            if(nxtx<1||nxtx>3||nxty<1||nxty>3)continue;
            swap(pp.mp[trans(pp.x,pp.y)],pp.mp[trans(nxtx,nxty)]);
            int hsh=cantor(pp.mp);
            pp.x=nxtx,pp.y=nxty;pp.d++;
            if(vis[id][hsh]==-1||(vis[id][hsh]==pp.d&&i>op[id][hsh])){op[id][hsh]=i;nxt[id][hsh]=prehsh;}
            if(vis[id][hsh]==-1){
                vis[id][hsh]=pp.d;
                mq.push(pp);
            }
        }
    }
}
node Createnode(int x){
    node p;int k=1;
    for(int i=0;i<9;i++){
        if(i!=x)p.mp[i]=k++;
    }p.mp[x]=k;
    p.x=x/3+1;
    p.y=x%3+1;
    return p;
}
void print(int id,int q){
    if(nxt[id][q]==-1){cout<<endl;return ;}
    cout<<predir[op[id][q]];
    print(id,nxt[id][q]);
}
void store(string p,node &s){
    int k=0;
    for(int i=0;i<Input.length();i++){
        if(Input[i]=='X'){
            Input[i]='9';
            s.x=k/3+1;
            s.y=k%3+1;
        }
        if(Input[i]==' ')continue;
        s.mp[k++]=Input[i]-'0';
    }
}
void init(){
    memset(vis,-1,sizeof vis);
    for(int i=0;i<9;i++){
        bfs(i,Createnode(i));
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    init();
    int T;
    cin>>T;
    node st,ed;
    for(int cas=1;cas<=T;cas++){
        cin>>Input;store(Input,st);
        cin>>Input;store(Input,ed);
        int pp=0,kk=1;
        for(int i=0;i<9;i++){
            if(ed.mp[i]==9){pp=i;continue;}
            pos[ed.mp[i]]=kk++;
        }
        for(int i=0;i<9;i++){
            if(st.mp[i]==9)continue;
            st.mp[i]=pos[st.mp[i]];
        }
        int hsh=cantor(st.mp);
        cout<<"Case "<<cas<<": "<<vis[pp][hsh]<<endl;
        print(pp,hsh);
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值