您的位置:首页 > 其它

uva 10085 - The most distant state

2012-08-18 16:19 393 查看
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

要求从起始状态(已知)到目标状态(未知)移动次数尽可能多,当然不能重复移动所以要判重,没想到高效的算法,只能把所以可以移动的都求出来,那么最后一次不能移动自然就是目标状态,他的移动次数一定是最多的,bfs出所有的状态;判重用全排列的哈希,状态数目太多了bfs写成递归调用貌似会栈溢出的样子;

#include<string.h>
#include<stdio.h>
#define P   for (i=0;i<9;i++){printf("%d",q[top].a[i]);if ((i+1)%3==0) printf("\n");else   printf(" ");}
struct node
{int a[9],x,y,succ,dir;
}q[362881];
int top,tail,visit[362881],exp[8]={40320,5040,720,120,24,6,2,1},
move[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int s[1000];
int hash(struct node *A,int num )//全排列哈希
{int i,j,x,y=0;
for (i=0;i<9;i++)
{
x=0;
for (j=i+1;j<9;j++)
if (q[num].a[i]>q[num].a[j]) ++x; //统计逆序数
y=y+x*exp[i];
}
return y;
}

int main()
{begin
int i,j,k,t,T,px,py,pk,sum,temp;
scanf("%d",&T);
for (t=1;t<=T;t++)
{
memset(visit,0,sizeof(visit));
for (i=0;i<9;i++)
{
scanf("%d",&q[0].a[i]);
if (q[0].a[i]==0) {q[0].x=i/3;q[0].y=i%3;}
}

top=0; tail=0;
visit[hash(&q[0],0)]=1;

while (top<=tail)
{
for (i=0;i<4;i++)
{
px=q[top].x+move[i][0];
py=q[top].y+move[i][1];
if ((px>=0)&&(py>=0)&&(px<3)&&(py<3))
{
k=q[top].x*3+q[top].y;
pk=px*3+py;
++tail;
for (j=0;j<9;j++)
q[tail].a[j]=q[top].a[j];
temp=q[tail].a[k]; q[tail].a[k]=q[tail].a[pk]; q[tail].a[pk]=temp;
k=hash(&q[tail],tail);
if (visit[k]) --tail;
else {visit[k]=1;q[tail].succ=top; q[tail].dir=i;q[tail].x=px;q[tail].y=py;}
}
}
++top;
}
--top;
printf("Puzzle #%d\n",t);P;
sum=0;
while (top)
{
s[++sum]=q[top].dir;
top=q[top].succ;
}
for (i=sum;i>=1;i--)
{
if (s[i]==0) printf("U");
if (s[i]==1) printf("D");
if (s[i]==2) printf("L");
if (s[i]==3) printf("R");
}
printf("\n\n");
}
end;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: