您的位置:首页 > 其它

HDOJ 题目1043 Eight(单向BFS,康拓展开,打表)

2015-11-05 17:59 477 查看


Eight

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 16879 Accepted Submission(s): 4640

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 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.

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)

Recommend

JGShining | We have carefully selected several similar problems for you: 1044 1026 1180 1067 1175

题目大意:
问序列变成12345678x的路径
ac代码

153941362015-11-05 17:54:45Accepted1043202MS8936K2668 BC++XY_
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
char num[15];
int fac[15];
void Fac()
{
int i;
fac[1]=1;
for(i=2;i<=10;i++)
fac[i]=fac[i-1]*i;
}
int cot(char *num)
{
int i,j;
int sum=0;
for(i=0;i<8;i++)
{
int c=0;
for(j=i+1;j<9;j++)
{
if(num[i]>num[j])
c++;
}
sum+=c*fac[9-i-1];
}
return sum+1;
}
struct s
{
int x,y;
int cnt;
char num[15];
}a,temp;
int vis[1010000];
int dirx[4]={0,1,0,-1};
int diry[4]={1,0,-1,0};
char p[1010000];
int pre[1010000];
void bfs()
{
int i;
queue<struct s>q;
memset(vis,0,sizeof(vis));
for(i=0;i<8;i++)
a.num[i]=i+'1';
a.num[8]='0';
a.cnt=cot(a.num);
//printf("%d\n",a.cnt);
a.x=2;
a.y=2;
q.push(a);
vis[a.cnt]=1;
p[a.cnt]=-1;
while(!q.empty())
{
a=q.front();
q.pop();
for(int i=0;i<4;i++)
{
temp.x=a.x+dirx[i];
temp.y=a.y+diry[i];
if(temp.x<0||temp.x>2||temp.y<0||temp.y>2)
continue;

int pos2=temp.x*3+temp.y;
int pos1=a.x*3+a.y;
strcpy(temp.num,a.num);
swap(temp.num[pos1],temp.num[pos2]);
temp.cnt=cot(temp.num);
//  printf("%d\n",temp.cnt);
if(vis[temp.cnt])
continue;
vis[temp.cnt]=1;
pre[temp.cnt]=a.cnt;
if(i==0)
p[temp.cnt]='l';
else
if(i==1)
p[temp.cnt]='u';
else
if(i==2)
p[temp.cnt]='r';
else
p[temp.cnt]='d';
q.push(temp);
}
}
}
char str[1010];
void print(int tt)
{
int x=tt;
while(p[x]!=-1)
{
printf("%c",p[x]);
x=pre[x];
}
printf("\n");
}
int main()
{
Fac();
bfs();
while(gets(str)!=NULL)
{
int i;
int len=strlen(str);
char ss[15],k=0;
for(i=0;i<len;i++)
{
if(str[i]=='x')
{
ss[k++]='0';
}
else
if(str[i]>='1'&&str[i]<='8')
ss[k++]=str[i];
}
ss[k]='\0';
int tt=cot(ss);
if(!vis[tt])
{
printf("unsolvable\n");
}
else
{
print(tt);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: