您的位置:首页 > 其它

poj 1077-Eight;hdu 1043-Eight

2013-07-19 02:52 507 查看
点击打开链接hud

点击打开链接poj


Eight

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

Total Submission(s): 9141    Accepted Submission(s): 2459
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

这题折腾了我一晚上,八数码问题是一开始就学过的问题,可是没想到这个题在杭电上要求居然这么高,POJ只有一组数据,1AC单向广搜就能过,不过这次是要双向广搜,所以写的很多也很乱,中间还参考了一点北大的课件,,杭电的数据很严,只有双向广搜也过不了,必须加剪枝,就是一开始就判断可不可行,最后才2800MS过的,康托展开忘了,又去百度查的,双向广搜写起来特别麻烦,还很容易错,对于我这种菜鸟,如果要参加比赛估计要带个模板去了,哎

AC代码 POJ 63MS HDU 2796MS

#include<stdio.h>
#include<queue>
#include<string.h>
#include<iostream>
using namespace std;
int father[370000][2];
short hash[370000];
int zeropoint[370000];

int factory[12] = { 0, 1, 2, 6, 24, 120,720, 5040, 40320, 362880 };
int array[10] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
int contor(int num)
{
char str[10];
sprintf(str, "%.9d", num);
int len = 9;
int i, j, res = 0;
for(i = 0; i < len; i++)
{
int count = 0;
for(j = i + 1; str[j]; j++)
{
if(str[i] > str[j])
count ++;
}
res += count * factory[len - i - 1];
}
return res + 1;
}
int getZero(int num)
{
int i = 8;
while(num > 0)
{
int n = num / array[i];
if(n == 0)
return 9 - i - 1;
num %= array[i];
i--;
}
return 8;
}
void printfather(int child, int f)
{
if(father[child][0] == 0)
return;
if(father[child][0] == f)
{
switch(father[child][1])
{
case 1:
printf("u");
break;
case 2:
printf("d");
break;
case 3:
printf("l");
break;
case 4:
printf("r");
break;
}
return;
}
printfather(contor(father[child][0]), f);
switch(father[child][1])
{
case 1:
printf("u");
break;
case 2:
printf("d");
break;
case 3:
printf("l");
break;
case 4:
printf("r");
break;
}
}
void prints(int child)
{
switch(father[child][1])
{
case 1:
printf("d");
break;
case 2:
printf("u");
break;
case 3:
printf("r");
break;
case 4:
printf("l");
break;
}
}
void bfs(int num)
{
queue<int> head, tail;
hash[contor(123456780)] = 2;
hash[contor(num)] = 1;
zeropoint[contor(123456780)] = getZero(123456780);
zeropoint[contor(num)] = getZero(num);
head.push(num);
tail.push(123456780);
char str[10];
while(!head.empty() && !tail.empty ())
{
if(head.size() < tail.size())
{
int curr = head.front();
head.pop();
sprintf(str, "%.9d", curr);
char str2[10];
int zero = zeropoint[contor(curr)];
int numb;
if(zero > 2)
{
strcpy(str2, str);
char swap = str2[zero - 3];
str2[zero - 3] = str2[zero];
str2[zero] = swap;
sscanf(str2,"%d", &numb);
int flag = contor(numb);
if(hash[flag] == 2)
{
printfather(contor(curr), num);
printf("u");
int j;
int c = contor(1234567890);
for(j = flag; j != c ; j = contor(father[j][0]))
{
prints(j);
}
return;
}
else if(hash[flag] == 0)
{
father[flag][0] = curr;
father[flag][1] = 1;
hash[flag] = 1;
zeropoint[flag] = zero - 3;
head.push(numb);
}
}

if(zero < 6)
{
strcpy(str2, str);
char swap = str2[zero + 3];
str2[zero + 3] = str2[zero];
str2[zero] = swap;
sscanf(str2,"%d", &numb);
int flag = contor(numb);
if(hash[flag] == 2)
{
printfather(contor(curr), num);
printf("d");
int j;
int c = contor(1234567890);
for(j = flag; j != c ; j = contor(father[j][0]))
{
prints(j);
}
return;

}
else if(hash[flag] == 0)
{
father[flag][0] = curr;
father[flag][1] = 2;
hash[flag] = 1;
zeropoint[flag] = zero + 3;
head.push(numb);
}
}
if(zero % 3 < 2)
{
strcpy(str2, str);
char swap = str2[zero + 1];
str2[zero + 1] = str2[zero];
str2[zero] = swap;
sscanf(str2,"%d", &numb);
int flag = contor(numb);

if(hash[flag] == 2)
{
printfather(contor(curr), num);
printf("r");
int j;
int c = contor(1234567890);
for(j = flag; j != c ; j = contor(father[j][0]))
{
prints(j);
}
return;
}
else if(hash[flag] == 0)
{
father[flag][0] = curr;
father[flag][1] = 4;
hash[flag] = 1;
zeropoint[flag] = zero + 1;
head.push(numb);
}
}
if(zero % 3 > 0)
{
strcpy(str2, str);
char swap = str2[zero - 1];
str2[zero - 1] = str2[zero];
str2[zero] = swap;
sscanf(str2,"%d", &numb);
int flag = contor(numb);
if(hash[flag] == 2)
{
printfather(contor(curr), num);
printf("l");
int j;
int c = contor(1234567890);
for(j = flag; j != c ; j = contor(father[j][0]))
{
prints(j);
}
return;
}
else if(hash[flag] == 0)
{
father[flag][0] = curr;
father[flag][1] = 3;

hash[flag] = 1;
zeropoint[flag] = zero - 1;
head.push(numb);
}
}
}
else
{
int curr = tail.front();
tail.pop();
// printf("2: %.9d\n", curr);
sprintf(str, "%.9d", curr);
char str2[10];
strcpy(str2, str);
int zero = zeropoint[contor(curr)];
int numb;
if(zero > 2)
{
strcpy(str2, str);
char swap = str2[zero - 3];
str2[zero - 3] = str2[zero];
str2[zero] = swap;
sscanf(str2,"%d", &numb);
int flag = contor(numb);
if(hash[flag] == 1)
{
printfather(flag, num);
printf("d");
int j;
int c = contor(1234567890);
for(j = contor(curr); j != c ; j = contor(father[j][0]))
{
prints(j);
}
return;
}
else if(hash[flag] == 0)
{
father[flag][0] = curr;

father[flag][1] = 1;
hash[flag] = 2;
zeropoint[flag] = zero - 3;
tail.push(numb);
}
}

if(zero < 6)
{
strcpy(str2, str);
char swap = str2[zero + 3];
str2[zero + 3] = str2[zero];
str2[zero] = swap;
sscanf(str2,"%d", &numb);
int flag = contor(numb);
if(hash[flag] == 1)
{
printfather(flag, num);
printf("u");
int j;
int c = contor(1234567890);
for(j = contor(curr); j != c ; j = contor(father[j][0]))
{
prints(j);
}
return;

}
else if(hash[flag] == 0)
{
father[flag][0] = curr;
father[flag][1] = 2;
hash[flag] = 2;
zeropoint[flag] = zero + 3;
tail.push(numb);
}
}
if(zero % 3 < 2)
{
strcpy(str2, str);
char swap = str2[zero + 1];
str2[zero + 1] = str2[zero];
str2[zero] = swap;
sscanf(str2,"%d", &numb);
int flag = contor(numb);

if(hash[flag] == 1)
{
printfather(flag, num);
printf("l");
int j;
int c = contor(1234567890);
for(j = contor(curr); j != c ; j = contor(father[j][0]))
{
prints(j);
}
return;
}
else if(hash[flag] == 0)
{
father[flag][0] = curr;
father[flag][1] = 4;
hash[flag] = 2;
zeropoint[flag] = zero + 1;
tail.push(numb);
}
}
if(zero % 3 > 0)
{
strcpy(str2, str);
char swap = str2[zero - 1];
str2[zero - 1] = str2[zero];
str2[zero] = swap;
sscanf(str2,"%d", &numb);
int flag = contor(numb);
if(hash[flag] == 1)
{
printfather(flag, num);
printf("r");
int j;
int c = contor(1234567890);
for(j = contor(curr); j != c ; j = contor(father[j][0]))
{
prints(j);
}
return;
}
else if(hash[flag] == 0)
{
father[flag][0] = curr;
father[flag][1] = 3;

hash[flag] = 2;
zeropoint[flag] = zero - 1;
tail.push(numb);
}
}
}
}
printf("unsolvable");
}
int main()
{

// freopen("in.txt", "r", stdin);
char str[20];
// gets(str);
while(cin.getline(str, 48))
{
memset(father, 0, sizeof(father));
memset(hash, 0, sizeof(hash));
memset(zeropoint, 0, sizeof(zeropoint));
int i;
char ch;
int number = 0;
int j = 0;
for(i = 0; i < 9; i++, j ++)
{

while(sscanf(str + j, "%c", &ch), ch != 'x' && (ch < '0' || ch > '9'))
j++;
if(ch == 'x')
ch = '0';
number = number * 10 + ch - '0';
}
sprintf(str, "%.9d", number);

int sumGoal = 0;
for(i = 0;i < 8; ++i )
sumGoal += i -1;
int sumOri = 0;
for(i = 0;i < 9 ; ++i ) {
if( str[i] == '0')
continue;
for( int j = 0; j < i; ++j ) {
if( str[j] < str[i] && str[j] != '0' )
sumOri ++;
}
}
if( sumOri %2 != sumGoal %2 ) {
printf("unsolvable\n");
continue;
}

// printf("%d", number);
if(number != 123456780)
bfs(number);
printf("\n");
}
// printf("%d %d\n", contor(23185476), contor(120743658));
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: