HDU 1043 Eight (A* + HASH + 康托展开)
Eight
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13956 Accepted Submission(s): 3957 Special Judge
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.
1 2 3 x 4 6 7 5 8
is described by this list:
1 2 3 x 4 6 7 5 8
#include <iostream>
#include <cmath>
#include <string>
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int SIZE = ;
const int GOAL = ;
const int HASH[] = {,,,,,,,,};
const int UP_DATE[][] = {{,-},{,},{-,},{,}};
int PATH[];
int PRE[];
struct Node
{
int map[SIZE][SIZE];
int x,y;
int h,g;
int hash;
bool operator <(const Node a) const
{
return h != a.h ? h > a.h : g > a.g;
}
}; bool solve_able(const Node & r);
bool check(const int,const int);
void cal_hash(Node & r);
void cal_h(Node & r);
void search(const Node & r);
void show(void);
int main(void)
{
Node first;
char s[]; while(gets(s))
{
int k = ;
memset(PRE,-,sizeof(PRE));
memset(PATH,-,sizeof(PATH));
for(int i = ;i <= ;i ++)
for(int j = ;j <= ;j ++)
{
if(s[k] >= '' && s[k] <= '')
first.map[i][j] = s[k] - '';
else if(s[k] == 'x')
{
first.map[i][j] = ;
first.x = i;
first.y = j;
}
else
j --;
k ++;
}
if(!solve_able(first))
{
printf("unsolvable\n");
continue;
}
cal_hash(first);
if(first.hash == GOAL)
{
puts("");
continue;
}
PATH[first.hash] = -;
first.g = ;
cal_h(first);
search(first);
} return ;
} bool solve_able(const Node & r)
{
int sum = ,count = ;
int temp[]; for(int i = ;i <= ;i ++)
for(int j = ;j <= ;j ++)
{
temp[count] = r.map[i][j];
count ++;
}
for(int i = ;i < ;i ++)
for(int j = i + ;j < ;j ++)
if(temp[j] < temp[i] && temp[j] && temp[i])
sum ++;
return !(sum & );
} bool check(const int x,const int y)
{
if(x >= && x <= && y >= && y <= )
return true;
return false;
} void cal_hash(Node & r)
{
int sum = ,count = ,box;
int temp[]; for(int i = ;i <= ;i ++)
for(int j = ;j <= ;j ++)
{
temp[count] = r.map[i][j];
count ++;
}
for(int i = ;i < ;i ++)
{
box = ;
for(int j = i + ;j < ;j ++)
if(temp[j] < temp[i])
box ++;
sum += (box * HASH[i]);
}
r.hash = sum;
} void search(Node const & r)
{
Node cur,next; priority_queue<Node> que;
que.push(r);
while(!que.empty())
{
cur = que.top();
que.pop();
for(int i = ;i < ;i ++)
{
next = cur;
next.x = cur.x + UP_DATE[i][];
next.y = cur.y + UP_DATE[i][];
if(!check(next.x,next.y))
continue;
swap(next.map[cur.x][cur.y],next.map[next.x][next.y]);
cal_hash(next); if(PATH[next.hash] == -)
{
PATH[next.hash] = i;
PRE[next.hash] = cur.hash;
next.g ++;
cal_h(next);
que.push(next);
}
if(next.hash == GOAL)
{
show();
return ;
}
}
} } void cal_h(Node & r)
{
int ans = ;
for(int i = ;i <= ;i ++)
for(int j = ;j <= ;j ++)
if(r.map[i][j])
ans += abs(i - ((r.map[i][j] - ) / + )) + abs(j - ((r.map[i][j] - ) % + ));
r.h = ans;
} void show(void)
{
string ans;
int hash = GOAL; ans.clear();
while(PRE[hash] != -)
{
switch(PATH[hash])
{
case :ans += 'l';break;
case :ans += 'r';break;
case :ans += 'u';break;
case :ans += 'd';break;
}
hash = PRE[hash];
}
for(int i = ans.size() - ;i >= ;i --)
printf("%c",ans[i]);
cout << endl;
}
HDU 1043 Eight (A* + HASH + 康托展开)的更多相关文章
- HDU 1430 魔板(康托展开+BFS+预处理)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- hdu.1430.魔板(bfs + 康托展开)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- 双向广搜+hash+康托展开 codevs 1225 八数码难题
codevs 1225 八数码难题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Yours和zero在研究A*启 ...
- HDU 1043 八数码(A*搜索)
在学习八数码A*搜索问题的时候须要知道下面几个点: Hash:利用康托展开进行hash 康托展开主要就是依据一个序列求这个序列是第几大的序列. A*搜索:这里的启示函数就用两点之间的曼哈顿距离进行计算 ...
- POJ 1077 && HDU 1043 Eight A*算法,bfs,康托展开,hash 难度:3
http://poj.org/problem?id=1077 http://acm.hdu.edu.cn/showproblem.php?pid=1043 X=a[n]*(n-1)!+a[n-1]*( ...
- HDU 1043 & POJ 1077 Eight(康托展开+BFS+预处理)
Eight Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30176 Accepted: 13119 Special ...
- HDU 1043 Eight(双向BFS+康托展开)
http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...
- HDU 1043 Eight(反向BFS+打表+康托展开)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 题目大意:传统八数码问题 解题思路:就是从“12345678x”这个终点状态开始反向BFS,将各 ...
- HDU 1043 & POJ 1077 Eight(康托展开+BFS | IDA*)
Eight Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30176 Accepted: 13119 Special ...
随机推荐
- 第二百八十五天 how can I 坚持
今天好平凡啊. 晚上给徐斌打电话说忘带钥匙了,一块吃了个饭. 回到家,什么都不想做,好消沉. 玩了几局象棋,很多东西只是玩玩,但还是会认真,认真就会输,好惨. 最近在关注万科幸福里,可是.首付付不起啊 ...
- 第二百四十天 how can I 坚持
在家待了一天,晚上出去买了个帽子,还有买了点排骨炖着吃了... 玩了好多局游戏. 想搞个直播,不知道能不能玩的起来. 水平太菜了,明天去小米之家玩玩. 睡觉.
- VC的文件操作
各种关于文件的操作在程序设计中是十分常见,如果能对其各种操作都了如指掌,就可以根据实际情况找到最佳的解决方案,从而在较短的时间内编写出高效的代码,因而熟练的掌握文件操作是十分重要的.本文将对Visua ...
- 当LinkButton无效时,光标不显示为手型
在Flex组件LinkButton里,我们可以用useHandCursor属性来控制是否使用手型光标.现在我们要实现在LinkButton的enable=false时,useHandCursor=fa ...
- DuiVision开发教程(17)-对话框
DuiVision的对话框类是CDlgBase. 代码中假设须要创建一个对话框,一般建议使用DuiSystem类中封装的若干对话框相关的函数来操作,包括创建对话框.删除对话框.依据对话框名获取对话框指 ...
- 很值得学习的java 画图板源码
很值得学习的java 画图板源码下载地址:http://download.csdn.net/source/2371150 package minidrawpad; import java.awt.*; ...
- 发现一个不错的十六进制编辑器-HxD
十六进制编辑器我觉得是个必需的工具,遥想当年用文曲星的时候,Pacmanager.Ewayeditor…… 之前都在用WinHex,好用,但是是个收费软件,老用着破解版心里还是不舒服 后来发现这个名叫 ...
- 一个不错的JavaScript解析浏览器路径方法(转)
JavaScript中有时需要用到当前的请求路径等涉及到url的情况,正常情况下我们可以使用location对象来获取我们需要的信息,本文从另外一个途径来解决这个问题,而且更加巧妙 方法如下: fun ...
- delphi 插入表格HTML代码
<table width="174" height="76" border="1" align="center" ...
- android152 笔记 2
27.谈谈UI中, Padding和Margin有什么区别? Padding 文字对边框, margin是控件对父窗体. 28. widget相对位置的完成在activity的哪个生命周期阶段实现.控 ...