uva10085 The most distant state(BFS)

来源:互联网 发布:大数据方面的项目 编辑:程序博客网 时间:2024/06/01 09:33

Problem A

The Most Distant State

Input: standard input

Output: standard output

 

The 8-puzzle is a square tray in which eight square tiles are placed. The remaining ninth square is uncovered. Each tile has a number on it. A tile that is adjacent to the blank space can be slid into that space. A game consists of a starting state and a specified goal state. The starting state can be transformed into the goal state by sliding (moving) the tiles around. The 8-puzzle problem asks you to do the transformation in minimum number of moves.

 

2

8

3

 

 

 

1

2

3

1

6

4

=>

8

 

4

7

 

5

 

 

 

7

6

5

Start

 

 

 

Goal

 

However, our current problem is a bit different. In this problem, given an initial state of the puzzle your are asked to discover a goal state which is the most distant (in terms of number of moves) of all the states reachable from the given state.


Input

The first line of the input file contains an integer representing the number of test cases to follow. A blank line follows this line.


Each test case consists of 3 lines of 3 integers each representing the initial state of the puzzle. The blank space is represented by a 0 (zero). A blank line follows each test case.

 

Output

For each test case first output the puzzle number. The next 3 lines will contain 3 integers each representing one of the most distant states reachable from the given state. The next line will contain the shortest sequence of moves that will transform the given state to that state. The move is actually the movement of the blank space represented by four directions : U (Up), L (Left), D (Down) and R (Right). After each test case output an empty line.

 

Sample Input

1

2 6 4
1 3 7
0 5 8

Sample Output

Puzzle #1
8 1 5
7 3 6
4 0 2
UURDDRULLURRDLLDRRULULDDRUULDDR

__________________________________________________________________________________________ 
Rezaul Alam Chowdhury 

"A fool looks for happiness in the distance, those who are intelligent grow it under their own feet."


姿势1:(BFS+map) 十进制记录状态,bfs一遍即可

#include <iostream>#include <cstring>#include <cstdio>#include <map>#include <queue>#define ll long longusing namespace std;int T;struct node{    int x,y,st;    string ans;}f,Ans;ll ST;map<ll,int> vis;char s[4]={'U','L','D','R'};int d[4][2]={-1,0,0,-1,1,0,0,1};ll g[10];int a[4][4];void init(){    g[0]=1;    for(int i=1;i<10;i++) g[i]=g[i-1]*10;}void read(){    vis.clear();    ST=0;    for(int i=0;i<3;i++){        for(int j=0;j<3;j++){            scanf("%d",&a[i][j]);            if(a[i][j]==0) f.x=i,f.y=j;            ST=ST*10+a[i][j];        }    }}int calc(int x,int y){    return (2-x)*3+(2-y);}void solve(int ca){    queue<node> q;    f.st=ST,f.ans="";    q.push(f);    vis[ST]=1;    while(!q.empty()){        node z=q.front();        q.pop();        for(int k=0;k<4;k++){            f.x=z.x+d[k][0];            f.y=z.y+d[k][1];            if(!(f.x>=0&&f.x<3&&f.y>=0&&f.y<3)) continue;            int ff=calc(f.x,f.y),zz=calc(z.x,z.y);            int fx=z.st/g[ff]%10;            f.st=z.st;            f.st-=fx*g[ff];            f.st+=fx*g[zz];            if(!vis[f.st]){                vis[f.st]=1;                f.ans=z.ans+s[k];                q.push(f);            }        }        if(q.empty()) Ans=z;    }    printf("Puzzle #%d\n",ca);    for(int i=8;i>=0;i--){        if(i%3!=2) printf(" ");        printf("%d",Ans.st/g[i]%10);        if(i%3==0) printf("\n");    }    printf("%s\n",Ans.ans.c_str());}int main(){    init();    scanf("%d",&T);    for(int ca=1;ca<=T;ca++){        read();        solve(ca);    }    return 0;}


姿势2:(BFS+hash)hash大法 好,用前向星。

#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <algorithm>#include <string>#define maxn 400000#define mod 509989using namespace std;int T;char s[4]={'U','L','D','R'};int d[4][2]={-1,0,0,-1,1,0,0,1};int st[maxn][9],tot;int head[mod],next[mod],pre[maxn],dir[maxn];void init(){    tot=0;    memset(head,-1,sizeof head);    memset(pre,-1,sizeof pre);}int hash(int x){    int ret=0;    for(int i=0;i<9;i++) ret=ret*10+st[x][i];    return ret%mod;}int find(int x){    int u=hash(x);    for(int i=head[u];i!=-1;i=next[i]){        if(memcmp(st[i],st[x],sizeof st[x])==0) return 1;    }    next[x]=head[u];    head[u]=x;    return 0;}void read(){    for(int i=0;i<9;i++)scanf("%d",&st[0][i]);    tot++;}void solve(int ca){    queue<int> q;    q.push(0);    find(0);    int ans;    while(!q.empty()){        int x=q.front(),p;        q.pop();        for(int i=0;i<9;i++){            if(st[x][i]==0) {p=i;break;}        }        int px=p/3,py=p%3;        for(int i=0;i<4;i++){            int xx=px+d[i][0];            int yy=py+d[i][1];            int z=xx*3+yy;            if(!(xx>=0&&xx<3&&yy>=0&&yy<3)) continue;            memcpy(st[tot],st[x],sizeof st[x]);            swap(st[tot][z],st[tot][p]);            if(!find(tot)){                pre[tot]=x,dir[tot]=i;                q.push(tot++);            }        }        if(q.empty()) ans=tot-1;    }    printf("Puzzle #%d\n",ca);    for(int i=0;i<9;i++){        if(i%3!=0) printf(" ");        printf("%d",st[ans][i]);        if(i%3==2) printf("\n");    }    int x=ans;    string Ans="";    while(pre[x]!=-1){        Ans.push_back(s[dir[x]]);        x=pre[x];    }    reverse(Ans.begin(),Ans.end());    printf("%s\n",Ans.c_str());}int main(){    scanf("%d",&T);    for(int ca=1;ca<=T;ca++){        init();        read();        solve(ca);    }    return 0;}


1 0
原创粉丝点击