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 ...
随机推荐
- html5+css3中的background: -moz-linear-gradient 用法
在CSS中background: -moz-linear-gradient 让网站背景渐变的属性,目前火狐3.6以上版本和google浏览器支持这个属性. background: -moz-linea ...
- maven 控制台乱码
在pom.xml加一条配置 <project> …… <properties> <argLine>-Dfile.encoding=UTF-8</argLine ...
- 06 java中常量以及常量池
1.举例说明 变量 常量 字面量 int a=10; float b=1.234f; String c="abc"; final long d=10L; a,b,c为变量,d为常量 ...
- iOS事件机制(一)
运用的前提是掌握 掌握的本质是理解 本篇内容将围绕iOS中事件及其传递机制进行学习和分析.在iOS中,事件分为三类: 触控事件(单点.多点触控以及各种手势操作) 传感器事件(重力.加速度传感器等) 远 ...
- CodeForces 173C Spiral Maximum (想法、模拟)
Spiral Maximum Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Sub ...
- jquery easyui的layout
1.创建一个布局:layout是一个容器,它有5个区域:north(北),south(南),east(东),west(西):center(中)-----------学习中文网地址 <body c ...
- [iOS 多线程 & 网络 - 2.1] - 解析json
A.iOS中json的基本使用 1.解析json数据 (1)json反序列化 对象{}格式 {key : value, key : value,...} 的键值对的结构可以反序列化为OC中的NSDic ...
- 在JS和.NET中使用JSON (以及使用Linq to JSON定制JSON数据)
转载原地址: http://www.cnblogs.com/mcgrady/archive/2013/06/08/3127781.html 阅读目录 JSON的两种结构 认识JSON字符串 在JS中如 ...
- ucGUI 12864 从打点起
ucGUI是纯C写的的,移植需要定义点阵数,颜色数,和画点函数 以下是ucGUI 12864下的移植 基于ST7920控制的12864液晶用于字符显示很方便的,但网友说用它显示图形并不合适,原因就 ...
- VMware搭建12.0搭建Mac OS10.11详细过程
1.软件准备 1.1VMware12.0 1.2VMware增强包 1.3Mac OS10.11 cdr(相当于dmg) 1.4securable.exe 2.软件破解 2.1VMware输入序列号破 ...