您的位置:首页 > 其它

poj 1077 Eight(经典八数码问题:bfs/Dbfs)

2016-06-03 16:08 579 查看
poj 1077 Eight(经典八数码问题:bDfs/Dbfs)

总时间限制: 5000ms 内存限制: 65536kB

Special Judge

描述

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:



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.

输入

You will receive a description of a configuration of the 8 puzzle. The 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

输出

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.

样例输入

2 3 4 1 5 x 7 6 8

样例输出

ullddrurdllurdruldr

来源

South Central USA 1998

本题是经典八数码问题,这一次以这道题为契机学习了Dbfs,现在已经学会了用bfs(version 1)和Dbfs(version 2)实现了。注意本题是special judge的,这样才不必考虑合理的搜索顺保证字典序,否则Dbfs无法安排搜索顺序…

注意一个编码小技巧Cantor launched,用来给全排列编码而且很节省空间。以后要用了推一推还是可以记起来的。

下面是代码(含测试部分)和评测结果,可以发现Dbfs快于bfs

version 1

Accepted    9140kB  220ms  2398 B   G++


#define TEST
#undef TEST

#define MAX_LEN 362880
#define TARGET_STATE 46233

#include<stdio.h>
#include<iostream>

using namespace std;

const char operate_name[4]={'u','d','l','r'};
const int change[9][4]={{-1, 3,-1, 1},{-1, 4, 0, 2},{-1, 5, 1,-1},
{ 0, 6,-1, 4},{ 1, 7, 3, 5},{ 2, 8, 4,-1},
{ 3,-1,-1, 7},{ 4,-1, 6, 8},{ 5,-1, 7,-1}};
/*
* 0 1 2
* 3 4 5
* 6 7 8
*/

int queue[MAX_LEN][9];
int blank[MAX_LEN],father[MAX_LEN],operate[MAX_LEN];
int head,tail;

char str_out[MAX_LEN];
int len;

bool visit[MAX_LEN];

int cantor_launched(int a[]);
void read_and_init();
void bfs();
void print(int last);
void test();

int main()
{
#ifdef TEST
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
read_and_init();
bfs();
#ifdef TEST
test();
#endif
return 0;
}

int cantor_launched(int a[])
{
int anti_num,frac=1,ans=0;
for (int i=8;i>=0;i--)
{
anti_num=0;
for (int j=i;j<9;j++)
if (a[j]<a[i])
anti_num++;
ans+=frac*anti_num;
frac*=(9-i);
}
return ans;
}

void read_and_init()
{
char ch;
for (int i=0;i<9;i++)
{
cin>>ch;
if (ch=='X' || ch=='x')
{
blank[0]=i;
queue[0][i]=0;
}
else
queue[0][i]=ch-'0';
}
head=0;
tail=0;
father[0]=0;
operate[0]=0;
return;
}

void bfs()
{
int new_blank;
while (head<=tail)
{
if (cantor_launched(queue[head])==TARGET_STATE)
{
print(head);
return;
}
for (int d=0;d<4;d++)
{
new_blank=change[blank[head]][d];
if (new_blank!=-1)
{
tail++;
for (int i=0;i<9;i++)
queue[tail][i]=queue[head][i];
queue[tail][new_blank]=0;
queue[tail][blank[head]]=queue[head][new_blank];
if (visit[cantor_launched(queue[tail])])
tail--;
else
{
visit[cantor_launched(queue[tail])]=true;
blank[tail]=new_blank;
father[tail]=head;
operate[tail]=d;
}
}
}
head++;
}
printf("unsolvable\n");
return;
}

void print(int last)
{
while (father[last]!=last)
{
str_out[len++]=operate_name[operate[last]];
#ifdef TEST
printf("\n");
for (int t=0;t<9;t++)
printf("%d%c",queue[last][t],(t+1)%3?' ':'\n');
#endif
last=father[last];
}
for (int i=len-1;i>=0;i--)
printf("%c",str_out[i]);
printf("\n");
return;
}

void test()
{
for (int i=0;i<tail;i++)
{
for (int t=0;t<9;t++)
printf("%d%c",queue[i][t],(t+1)%3?' ':'\n');
printf("father=%d,operate=%d\n\n",father[i],operate[i]);
}
return;
}


version 2

Accepted    1928kB  0ms 4690 B  G++


#define TEST
#undef TEST

#define MAX_LEN 362880
#define TARGET_STATE 46233

#include<stdio.h>
#include<iostream>

using namespace std;

const char operate_name[4]={'u','d','l','r'};
const int change[9][4]={{-1, 3,-1, 1},{-1, 4, 0, 2},{-1, 5, 1,-1},
{ 0, 6,-1, 4},{ 1, 7, 3, 5},{ 2, 8, 4,-1},
{ 3,-1,-1, 7},{ 4,-1, 6, 8},{ 5,-1, 7,-1}};

const char operate_rname[4]={'d','u','r','l'};
const int rchange[9][4]={{ 3,-1, 1,-1},{ 4,-1, 2, 0},{ 5,-1,-1, 1},
{ 6, 0, 4,-1},{ 7, 1, 5, 3},{ 8, 2,-1, 4},
{-1, 3, 7,-1},{-1, 4, 8, 6},{-1, 5,-1, 7}};
/*
* 0 1 2
* 3 4 5
* 6 7 8
*/

int queue[MAX_LEN+1][9];
int blank[MAX_LEN+1],father[MAX_LEN+1],operate[MAX_LEN+1];
int head,tail,rhead,rtail;

char str_out[MAX_LEN];
int len;

bool visit[MAX_LEN],rvisit[MAX_LEN];

int cantor_launched(int a[]);
void read_and_init();
void dbfs();
void print(int last);
void rprint(int last);

void test()
{
printf("\n___________TEST___________\n");
for (int i=0;i<tail;i++)
{
printf("\n");
for (int t=0;t<9;t++)
printf("%d%c",queue[i][t],(t+1)%3?' ':'\n');
printf("father=%5d,operate=%5d\n",father[i],operate[i]);
}
for (int i=rtail;i<=MAX_LEN;i++)
{
printf("\n");
for (int t=0;t<9;t++)
printf("%d%c",queue[i][t],(t+1)%3?' ':'\n');
printf("father=%5d,operate=%5d\n",father[i],operate[i]);
}
return;
}

int main()
{
#ifdef TEST
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
read_and_init();
dbfs();
#ifdef TEST
test();
#endif
return 0;
}

int cantor_launched(int a[])
{
int anti_num,frac=1,ans=0;
for (int i=8;i>=0;i--)
{
anti_num=0;
for (int j=i;j<9;j++)
if (a[j]<a[i])
anti_num++;
ans+=frac*anti_num;
frac*=(9-i);
}
return ans;
}

void read_and_init()
{
char ch;
for (int i=0;i<9;i++)
{
cin>>ch;
if (ch=='X' || ch=='x')
{
blank[0]=i;
queue[0][i]=0;
}
else
queue[0][i]=ch-'0';
}
head=0;
tail=0;
father[0]=0;
operate[0]=0;
visit[cantor_launched(queue[0])]=true;
for (int i=0;i<8;i++)
queue[MAX_LEN][i]=i+1;
queue[MAX_LEN][8]=0;
rhead=MAX_LEN;
rtail=MAX_LEN;
father[MAX_LEN]=MAX_LEN;
operate[MAX_LEN]=0;
blank[MAX_LEN]=8;
rvisit[TARGET_STATE]=true;
return;
}

void dbfs()
{
int new_blank;
while (head<=tail || rhead>=rtail)
{
if (head<=tail){
if (rvisit[cantor_launched(queue[head])])
{
print(head);
for (int j=rtail;j<=rhead;j++)
if (cantor_launched(queue[head])==cantor_launched(queue[j]))
{
rprint(j);
break;
}
return;
}
for (int d=0;d<4;d++)
{
new_blank=change[blank[head]][d];
if (new_blank!=-1)
{
tail++;
for (int i=0;i<9;i++)
queue[tail][i]=queue[head][i];
queue[tail][new_blank]=0;
queue[tail][blank[head]]=queue[head][new_blank];
if (visit[cantor_launched(queue[tail])])
tail--;
else
{
visit[cantor_launched(queue[tail])]=true;
blank[tail]=new_blank;
father[tail]=head;
operate[tail]=d;
}
}
}
head++;}
if (rhead>=rtail){
if (visit[cantor_launched(queue[rhead])])
{
for (int j=head;j<=tail;j++)
if (cantor_launched(queue[rhead])==cantor_launched(queue[j]))
{
print(j);
break;
}
rprint(rhead);
return;
}
for (int d=0;d<4;d++)
{
new_blank=rchange[blank[rhead]][d];
if (new_blank!=-1)
{
rtail--;
for (int i=0;i<9;i++)
queue[rtail][i]=queue[rhead][i];
queue[rtail][new_blank]=0;
queue[rtail][blank[rhead]]=queue[rhead][new_blank];
if (rvisit[cantor_launched(queue[rtail])])
rtail++;
else
{
rvisit[cantor_launched(queue[rtail])]=true;
blank[rtail]=new_blank;
father[rtail]=rhead;
operate[rtail]=d;
}
}
}
rhead--;}
}
printf("unsolvable\n");
return;
}

void print(int last)
{
#ifdef TEST
printf("Steps pointers move:\nhead=%d,tail=%d,rhead=%d,rtail=%d\n",
head,tail,MAX_LEN-rhead,MAX_LEN-rtail);
printf("%d\n",last);
#endif
while (father[last]!=last)
{
str_out[len++]=operate_name[operate[last]];
last=father[last];
#ifdef TEST
printf("\n");
for (int t=0;t<9;t++)
printf("%d%c",queue[last][t],(t+1)%3?' ':'\n');
printf("father=%5d,operate=%5d\n",father[last],operate[last]);
#endif
}
for (int i=len-1;i>=0;i--)
printf("%c",str_out[i]);
#ifdef TEST
printf("\n");
#endif
return;
}

void rprint(int last)
{
#ifdef TEST
printf("%d\n",last);
#endif
len=0;
while (father[last]!=last)
{
str_out[len++]=operate_name[operate[last]];
#ifdef TEST
printf("\n");
for (int t=0;t<9;t++)
printf("%d%c",queue[last][t],(t+1)%3?' ':'\n');
printf("father=%5d,operate=%5d\n",father[last],operate[last]);
#endif
last=father[last];
}
str_out[len]=0;
printf("%s\n",str_out);
return;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: