您的位置:首页 > 其它

POJ1077&&HDU1043(八数码,IDA*+曼哈顿距离)

2016-08-25 22:20 190 查看

Eight

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 30127   Accepted: 13108   Special Judge

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

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.

Sample Input

2  3  4  1  5  x  7  6  8

Sample Output

ullddrurdllurdruldr

//2016.8.25
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>

using namespace std;

int a[10][10], b[10], d[1000], sx, sy, deep;
bool ok;
map<int, bool> vis;
char dir[4] = {'u', 'd', 'l', 'r'};
int dx[4] = {-1, 1, 0, 0};//分别对应上下左右四个方向
int dy[4] = {0, 0, -1, 1};

bool solve()//求逆序对判断是否有解,偶数有解,奇数无解
{
int cnt = 0;
for(int i = 1; i <= 9; i++)
for(int j = 0; j < i; j++)
if(b[i] && b[j]>b[i])
cnt++;
return !(cnt%2);
}

int Astar()//启发函数,假设每个数字可以从别的数字头上跨过去,计算到达自己应该到的位置所需要的步数,即计算该数到达其正确位置的曼哈顿距离,h为各点曼哈顿距离之和
{
int h = 0;
for(int i = 1; i <= 3; i++)
for(int j = 1; j <= 3; j++)
if(a[i][j]!=0)
{
int nx = (a[i][j]-1)/3;
int ny = (a[i][j]-1)%3;
h += (abs(i-nx-1)+abs(j-ny-1));
}
return h;
}

int toInt()//把矩阵转换为int型数字
{
int res = 0;
for(int i = 1; i <= 3; i++)
for(int j = 1; j <= 3; j++)
res = res*10+a[i][j];
return res;
}

void IDAstar(int x, int y, int step)
{
if(ok)return ;
int h = Astar();
if(!h && toInt()==123456780)//找到答案
{
for(int i = 0; i < step; i++)
cout<<dir[d[i]];
cout<<endl;
ok = 1;
return ;
}
if(step+h>deep)return ;//现实+理想<现状,则返回,IDA*最重要的剪枝
int now = toInt();
if(vis[now])return ;//如果状态已经搜过了,剪枝,避免重复搜索
vis[now] = true;
for(int i = 0; i < 4; i++)
{
int nx = x+dx[i];
int ny = y+dy[i];
if(nx>=1&&nx<=3&&ny>=1&&ny<=3)
{
d[step] = i;
swap(a[x][y], a[nx][ny]);
IDAstar(nx, ny, step+1);
swap(a[x][y], a[nx][ny]);
d[step] = 0;
}
}
return;
}

int main()
{
char ch;
while(cin >> ch)
{
ok = false;
deep = 0;
int cnt = 0;
for(int i = 1; i <= 3; i++)
{
for(int j = 1; j <= 3; j++)
{
if(i==1&&j==1);
else cin >> ch;
if(ch == 'x')
{
a[i][j] = 0;
sx = i;
sy = j;
}else
a[i][j] = ch - '0';
b[cnt++] = a[i][j];
}
}
if(!solve())
{
cout<<"unsolvable"<<endl;
continue;
}
while(!ok)
{
vis.clear();
IDAstar(sx, sy, 0);
deep++;//一层一层增加搜索的深度
}
}

return 0;
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: