您的位置:首页 > 其它

HDU 1043 Eight

2015-10-08 23:17 323 查看

Eight

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

Total Submission(s): 16501    Accepted Submission(s): 4542
Special Judge


[align=left]Problem Description[/align]
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.

 

[align=left]Input[/align]
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

 

[align=left]Output[/align]
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.

 

[align=left]Sample Input[/align]

2  3  4  1  5  x  7  6  8

 

[align=left]Sample Output[/align]

ullddrurdllurdruldr

网上称这样的问题是八数码问题。对于八数码问题,有位大神表示八数码问题有八种境界,如果感兴趣可以点击这里

在这里为了学习有关A*算法,所以使用的是A*算法+哈希+曼哈顿距离

哈希是根据康托展开变形作为建表基础的,表示的是当前位在八个数中的大小位置关系乘以所在位置的权值

简单介绍一下曼哈顿距离,即|x1 - x2| + |y1 - y2|,即在标准坐标系中是一个很好的启发式函数

对于A*算法其实就是一个介于BFS和dijkstra算法的启发式算法。有幸看到一份对A*算法介绍的文章,写的真的很棒,如果想深入理解的话点这里。


当然,这里我们根据A*算法思想,设置优先考虑启发式函数曼哈顿距离的参数,次优先考虑BFS的代价

另外值得一提的是,网上同样给出了,因为无论如何移动或者是交换位置,逆序的奇偶性就不会变,所以我们求出结果的逆序数,如果一开始或者在求解过程中出现逆序数的奇偶性的改变,我们就需要否定这个方案或者输出unsolvable。

代码如下:

/*************************************************************************
> File Name: Eight_2.cpp
> Author: Zhanghaoran
> Mail: chilumanxi@xiyoulinux.org
> Created Time: Wed 07 Oct 2015 04:47:56 PM CST
************************************************************************/

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <string>
#include <vector>
#include <cmath>

using namespace std;

struct Node{
int Map[3][3];
int h, g;
int tx, ty;
bool operator<(const Node x)const{
return h != x.h ? h > x.h : g > x.g;
}
int Hash;
};

Node f;
bool check(int a, int b){
if(a >= 0 && a < 3 && b >= 0 && b < 3)
return true;
else return false;
}

int Hash[9] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320};

int Suc = 322560;

int vis[409115];
int Path[500000];

int di[4][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0}};

int get_hash(Node x){
int temp[9];
int k = 0;

for(int i = 0; i < 3; i ++){
for(int j = 0; j < 3; j ++){
temp[k ++] = x.Map[i][j];
}
}

int res = 0;
for(int i = 0; i < 9; i ++){
k = 0;
for(int j = 0; j < i; j ++){
if(temp[j] > temp[i])
k ++;
}
res += Hash[i] * k;
}

return res;
}

bool judge(Node x){
int temp[9];
int k = 0;
for(int i = 0; i < 3; i ++){
for(int j = 0; j < 3; j ++)
temp[k ++] = x.Map[i][j];
}
int ans = 0;

for(int i = 0; i < 9; i ++){
for(int j = i + 1; j < 9; j ++){
if(temp[j] && temp[i] && temp[i] > temp[j])
ans ++;
}
}

return !(ans & 1);
}

int get_h(Node x){
int ans = 0;
for(int i = 0; i < 3; i ++){
for(int j = 0; j < 3; j ++){
if(x.Map[i][j])
ans += abs(i - (x.Map[i][j] - 1) / 3) + abs(j - (x.Map[i][j] - 1) % 3); //曼哈顿距离
}
}
return ans;
}

void bfs(){
priority_queue<Node>q;
Node temp, last;
q.push(f);
while(!q.empty()){
last = q.top();
q.pop();
for(int i = 0; i < 4; i ++){
temp = last;
temp.tx += di[i][0];
temp.ty += di[i][1];
if(check(temp.tx, temp.ty)){
swap(temp.Map[temp.tx][temp.ty], temp.Map[last.tx][last.ty]);
temp.Hash = get_hash(temp);
if(vis[temp.Hash] == -1 && judge(temp)){
vis[temp.Hash] = i;
temp.g ++;
Path[temp.Hash] = last.Hash;
temp.h = get_h(temp);
q.push(temp);
}
if(temp.Hash == Suc)
return ;
}
}
}
}

void print(){
string str;
str.clear();
int ok = Suc;
while(Path[ok] != -1){
if(vis[ok] == 0)
str += 'r';
else if(vis[ok] == 1)
str += 'l';
else if(vis[ok] == 2)
str += 'd';
else
str += 'u';
ok = Path[ok];
}
for(int i = str.size() - 1; i >= 0; i --){
putchar(str[i]);
}
puts("");
}

int main(void){
char str[100];
while(gets(str) != NULL){
int k = 0;
memset(vis, -1, sizeof(vis));
memset(Path, -1, sizeof(Path));
for(int i = 0; i < 3; i ++){
for(int j = 0; j < 3; j ++){
if((str[k] <= '9' && str[k] >= '0' || str[k] == 'x')){
if(str[k] == 'x'){
f.Map[i][j] = 0;
f.tx = i;
f.ty = j;
}
else
f.Map[i][j] = str[k] - '0';
}
else
j --;
k ++;
}
}
if(!judge(f)){
cout << "unsolvable"<< endl;
continue;
}
f.Hash = get_hash(f);
if(f.Hash == Suc){
puts("");
continue;
}
vis[f.Hash] = 1;
f.g = 0;
f.h = get_h(f);
bfs();
print();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 A