HUD 1043 Eight

Source: Internet
Author: User

EightTime limit:10000/5000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 14009 Accepted Submission (s): 3965
Special Judge


Problem DescriptionThe 15-puzzle have been around for over years; Even if you don't know it by so name, you ' ve seen it. It is constructed with the sliding tiles, each with a number from 1 to the IT, and all packed into a 4 by 4 frame with on E tile missing. Let's call the missing tile ' x '; The object of the puzzle is to arrange, the tiles so, they are ordered as:
1  2  3  4 5  6  7  8 9 1213  X

Where the only legal operation are 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     9 x  9 (     9)  1213            -ten-ten-----X r->            d->            r->

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

Not all puzzles can solved; In 1870, a man named Sam Loyd is famous for distributing an unsolvable version of the puzzle, and
Frustrating many people. In fact, any of the regular puzzle into a unsolvable one are to swap the tiles (not counting the missing ' X ' tile, of course).

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

Inputyou would receive, several descriptions of configuration of the 8 puzzle. One description is just a list of the tiles in their initial positions, with the rows listed from top to bottom, and the T Iles listed from left to right within a row, where the tiles is represented by numbers 1 to 8, plus ' x '. For example, this puzzle

1 2 3
X 4 6
7 5 8

is described by this list:

1 2 3 x 4 6 7 5 8

Outputyou would print to standard output either the word "unsolvable", if the puzzle has no solution, or a string consist ing entirely of the letters ' R ', ' l ', ' u ' and ' d ' that describes a series of moves that produce a solution. The string should include no spaces and start at the beginning. Don't print a blank line between cases.

Sample Input
2  3  4  1  5  x  7  6  8

Sample Output
Ullddrurdllurdruldr

Eight digital problems.

Bfs+ Kang Expand Open


#include <iostream> #include <stdio.h> #include <string> #include <cstring> #include < algorithm> #include <cmath> #include <queue> #include <stack> #define N 400000using namespace std;    struct node{int pos,val;    Char a[9]; string path;} Cur,now;int f[15]= {40320,5040,720,120,24,6,2,1,1}; Kang Drag expand the sentence weight int dx[4]={-1,1,0,0};//up and down int dy[4]={0,0,-1,1};char dir[5]= "Durl"; string Anspath[n],ans;int Vis[n];char C;    Queue<node>q;int Kanto (char *ss) {int ans=0;    int num;        for (int i=0;i<9;i++) {num=0;        for (int j=i+1;j<9;j++) {if (ss[i]>ss[j]) num++;    } Ans+=num*f[i]; } return ans+1;}    void BFs () {int X,Y,T,T1,TEMPV;    Char temps[9];     memset (vis,0,sizeof Vis);    while (!q.empty ()) Q.pop ();    for (int i=0;i<8;i++) cur.a[i]=i+1-0+ ' 0 ';    cur.a[8]= ' 0 ';    cur.pos=9;    Cur.path= "";    Cur.val=kanto (CUR.A);    Vis[cur.val]=1;    Anspath[cur.val]=cur.path;    Q.push (cur); WHile (!q.empty ()) {Now=q.front ();        T=now.pos;        y=t%3;        if (y==0) y=3;        x= (t-y)/3+1;        Q.pop ();        int nx,ny;            for (int i=0;i<4;i++) {nx=x+dx[i];            ny=y+dy[i];//eyes are blind, just see written n=y=dy[i];(⊙o⊙) ... t1= (nx-1) *3+ny;            memcpy (temps,now.a,sizeof (temps)); TEMPS[T-1]=NOW.A[T1-1];            Analog mobile temps[t1-1]= ' 0 ';            Tempv=kanto (temps);                if (nx>=1 && nx<=3 && ny>=1 && ny<=3 &&!VIS[TEMPV]) {                Vis[tempv]=1;                CUR.POS=T1;                Cur.path=now.path+dir[i];                memcpy (cur.a,temps,sizeof (temps));                CUR.VAL=TEMPV;                Q.push (cur);            Anspath[cur.val]=cur.path;    }}}}int Main () {//f[0]=1;//for (int i=1;i<9;i++)//f[i]=f[i-1]*i;    BFS ();     while (cin>>c) {if (c== ' x ') {Cur.pos=1;       cur.a[0]= ' 0 ';        } else cur.a[0]=c;            for (int i=1;i<9;i++) {cin>>c;                if (c== ' x ') {cur.pos=i+1;            cur.a[i]= ' 0 ';        } else cur.a[i]=c;        } cur.val=kanto (CUR.A);        int flag;            if (Vis[cur.val]) {flag=anspath[cur.val].length ();        for (int i=flag-1;i>=0;i--) printf ("%c", Anspath[cur.val][i]);        } else printf ("unsolvable");    cout<<endl; } return 0;}






HUD 1043 Eight

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.