Hdu 1043 Eight (八数码问题)
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1043
题目描述:
3*3的格子,填有1到8,8个数字,还有一个x,x可以上下左右移动,问最终能否移动到12345678x的状态?
hint:每一个3*3的格子从上到右,从左到右,一行一行读。
解题思路:
比较简单的八数码问题,大一暑假老师讲过,一直手懒脑懒并没有亲自尝试过。因为是多实例,先从12345678x的状态bfs出来所有可以到达的状态,并且记录下来路径。八数码最重要的就是保存状态,如果这个题目用十进制保存状态的话,至少要到达10^9的数量级。可以利用康拓展开hash搜索到的状态,空间可以缩小到9!(ง •̀_•́)ง
#include <stdio.h>
#include <string.h>
#include <string>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std; #define maxn 370000
struct node
{
string path;//路径
int s[]; //state
int loc; //9的位置
int ct; //康拓hash
};
int fac[] = {, , , , , , , *, **, ***};
//0!,1!,2!....9!
int dir[][] = {,, ,-, -,, ,}; // r, l, u, d
int vis[maxn];
char di[] = "lrdu"; //l, r, d, u
string path[maxn];
void init ()
{
memset (vis, , sizeof(vis));
} int cantor (int a[]) //康拓hash
{
int sum = ;
for (int i=; i<; i++)
{
int m = ;
for (int j=i+; j<; j++)
if (a[j] < a[i])
m ++;
sum += m * (fac[-i-]);
}
return sum + ;
} void bfs ()
{
node cur, next;
queue <node> Q; for(int i=; i<; i++)
cur.s[i] = i + ;
cur.s[] = ;
cur.ct = cantor (cur.s);
cur.loc = ; Q.push (cur);
vis[cur.ct] = ;
path[cur.ct] = ""; while (!Q.empty())
{
cur = Q.front();
Q.pop (); for (int i=; i<; i++)
{
int x = cur.loc / + dir[i][];
int y = cur.loc % + dir[i][];
if (x< || x> || y< || y>)
continue;
next = cur;
next.loc = x * + y;
next.s[cur.loc] = next.s[next.loc];
next.s[next.loc] = ;
next.ct = cantor (next.s);
if (!vis[next.ct])
{
vis[next.ct] = ;
path[next.ct] = di[i] + path[cur.ct];
Q.push (next);
}
}
} } int main ()
{
init ();
bfs ();
char s[];
int a[]; while (scanf ("%s", s) != EOF)
{
if (s[] == 'x')
a[] = ;
else
a[] = s[] - ''; for (int i=; i<; i++)
{
scanf ("%s", s);
if (s[] == 'x')
a[i] = ;
else
a[i] = s[] - '';
}
int m = cantor (a);
if (vis[m])
cout<<path[m]<<endl;
else
puts ("unsolvable");
}
return ;
}
Hdu 1043 Eight (八数码问题)的更多相关文章
- HDU 1043 Eight 八数码问题 A*算法(经典问题)
HDU 1043 Eight 八数码问题(经典问题) 题意 经典问题,就不再进行解释了. 这里主要是给你一个状态,然后要你求其到达\(1,2,3,4,5,6,7,8,x\)的转移路径. 解题思路 这里 ...
- hdu 1043 Eight (八数码问题)【BFS】+【康拓展开】
<题目链接> 题目大意:给出一个3×3的矩阵(包含1-8数字和一个字母x),经过一些移动格子上的数后得到连续的1-8,最后一格是x,要求最小移动步数. 解题分析:本题用BFS来寻找路径,为 ...
- HUD 1043 Eight 八数码问题 A*算法 1667 The Rotation Game IDA*算法
先是这周是搜索的题,网站:http://acm.hdu.edu.cn/webcontest/contest_show.php?cid=6041 主要内容是BFS,A*,IDA*,还有一道K短路的,.. ...
- 【双向广搜+逆序数优化】【HDU1043】【八数码】
HDU上的八数码 数据强的一B 首先:双向广搜 先处理正向搜索,再处理反向搜索,直至中途相遇 visit 和 队列都是独立的. 可以用一个过程来完成这2个操作,减少代码量.(一般还要个深度数组) 优化 ...
- hdu 1043 Eight 经典八数码问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 The 15-puzzle has been around for over 100 years ...
- HDU 1043 Eight(八数码)
HDU 1043 Eight(八数码) 00 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Descr ...
- Eight POJ - 1077 HDU - 1043 八数码
Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...
- HDU 1043 Eight (BFS·八数码·康托展开)
题意 输出八数码问题从给定状态到12345678x的路径 用康托展开将排列相应为整数 即这个排列在全部排列中的字典序 然后就是基础的BFS了 #include <bits/stdc++.h ...
- HDU 1043 八数码(A*搜索)
在学习八数码A*搜索问题的时候须要知道下面几个点: Hash:利用康托展开进行hash 康托展开主要就是依据一个序列求这个序列是第几大的序列. A*搜索:这里的启示函数就用两点之间的曼哈顿距离进行计算 ...
随机推荐
- 【网络协议】IP协议、ARP协议、RARP协议
IP数据报 IP是TCP/IP协议族中最核心的协议,全部的TCP.UDP.ICMP.IGMP数据都以IP数据报的格式传输.IP仅提供尽力而为的传输服务.假设发生某种错误.IP会丢失该数据.然后发送IC ...
- LeetCode(11)题解: Container With Most Water
https://leetcode.com/problems/container-with-most-water/ 题目: Given n non-negative integers a1, a2, . ...
- POJ 1703 Find them, Catch them(种类并查集)
题目链接 这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解. #include <cstring> #include <cstdio> #include <s ...
- td 中设置超出宽度显示省略号失效
td测试内容超出显示省略号时,结果没有显示省略号,而是一直往后显示,且超出了td大小,强行挤大了table. 原因是因为td默认不换行. 解决方法: 1.强制td换行. IE加上word-break: ...
- Why you shouldn’t connect your mobile application to a database
BY CRAIG CHAPMAN · PUBLISHED 2015-07-02 · UPDATED 2015-07-02 Working at Embarcadero, I frequently ...
- UVA10480 Sabotage —— 最小割最大流
题目链接:https://vjudge.net/problem/UVA-10480 题解: 实际就是求最小割集. 1.什么是网络流图的“割”?答:一个边的集合,使得网络流图删除这些边之后,点被分成两部 ...
- 【Java】通过移除空行和注释来压缩 JavaScript 代码
1. [代码]JavaScriptCompressor.java/** * This file is part of the Echo Web Application Framework (herei ...
- Objective-C 中Socket常用转换机制(NSData,NSString,int,Uint8,Uint16,Uint32,byte[])
最近项目中要用到socket通讯,由于涉及到组包问题,所以需要数据类型之间的来回转换,现在分享出来 如果想要请教Socket的问题请留言,我会随时回答的 1. int类型转16进制hexstring ...
- HDU - 1874 畅通工程续(最短路径)
d.已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离. s.最短路径 c.Dijkstra单源最短路 /* Dijkstra单源最短路 权值必须是非负 单源最短路径,Dijkstra算法 ...
- WCF寄宿到Windows Service[1]
WCF寄宿到Windows Service 返回 在前面创建一个简单的WCF程序,我们把WCF的服务寄宿到了Host这个控制台项目中了.下面将介绍如何把WCF的服务寄宿到Windows服务中(源代码) ...