hdu1043Eight

来源:互联网 发布:昆山cnc编程培训 编辑:程序博客网 时间:2024/05/22 12:19

Eight

Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 16   Accepted Submission(s) : 1
Special Judge
Problem Description
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 1213 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 1213 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.
 

Input
You will 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 tiles listed from left to right within a row, where the tiles are 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
 

Output
You will print to standard output either the word ``unsolvable'', if the puzzle has no solution, or a string consisting 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 of the line. Do not print a blank line between cases.
 

Sample Input
2 3 4 1 5 x 7 6 8
 

Sample Output
ullddrurdllurdruldr
 

Source
South Central USA 1998 (Sepcial Judge Module By JGShining)
 
本来用双向bfs结果tle到死啊~~~
参考别人打表过~~~~用到康托展开
#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <queue>#define INF 0x3f3f3f3f#define N 400000#define BUG printf("here!\n")using namespace std;struct node{    char num[9];    int cantor;    int pos;};int xx[4]={0,1,0,-1};int yy[4]={1,0,-1,0};char temp_str[30];char num[9];char tmp_num[10]="123456780";int fac[10]={1,1,2,6,24,120,720,5040,40320,362880};int vis[N];int route[N];int precantor[N];int cantor(char *num);void bfs(){    queue<node> q;    memset(vis,0,sizeof(vis));    memset(precantor,-1,sizeof(precantor));    node temp;    int i;    for(i=0;i<8;i++)        temp.num[i]='1'+i;    temp.num[8]='0';    temp.pos=8;    temp.cantor=cantor(temp.num);    q.push(temp);    vis[temp.cantor]=1;    while(!q.empty())    {        node cur=q.front();        q.pop();        int cx=cur.pos/3;        int cy=cur.pos%3;        for(i=0;i<4;i++)        {            int nx=cx+xx[i];            int ny=cy+yy[i];            if(nx<0||nx>=3||ny<0||ny>=3)                continue;            node next=cur;            next.pos=nx*3+ny;            next.num[cur.pos]=next.num[next.pos];            next.num[next.pos]='0';            next.cantor=cantor(next.num);            if(vis[next.cantor])                continue;            vis[next.cantor]=1;            if(i==0)            {                route[next.cantor]='l';                precantor[next.cantor]=cur.cantor;            }            else if(i==1)            {                route[next.cantor]='u';                precantor[next.cantor]=cur.cantor;            }            else if(i==2)            {                route[next.cantor]='r';                precantor[next.cantor]=cur.cantor;            }            else            {                route[next.cantor]='d';                precantor[next.cantor]=cur.cantor;            }            q.push(next);        }    }    return ;}void print(int tar){    int i;    for(i=tar;i!=-1;i=precantor[i])    {        printf("%c",route[i]);    }    printf("\n");}int cantor(char *num){    int i,j,tol=0,sum=0;    for(i=0;i<9;i++)    {        tol=0;        for(j=i+1;j<9;j++)        {            if(num[i]<num[j])                tol++;        }        sum+=tol*fac[9-i-1];    }    return sum;}int main(){    bfs();    while(gets(temp_str)!=NULL)    {        int k=0;        int i,l=strlen(temp_str);        for(i=0;i<l;i++)        {            if(temp_str[i]=='x')            {                num[k]='0';                k++;            }            else if(temp_str[i]>='1'&&temp_str[i]<='8')                num[k++]=temp_str[i];        }        int tt=cantor(num);        if(vis[tt]==0)            printf("unsolvable\n");        else        {            print(tt);        }    }    return 0;}



原创粉丝点击