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*搜索:这里的启示函数就用两点之间的曼哈顿距离进行计算 ...
随机推荐
- inheritance super overrides printMethod in Superclass override重写父方法
Core Java Web Page http://horstmann.com/corejava.html [ inheritance ] package v1ch05.inheritance; im ...
- Struts2中Action接收参数
Struts2中Action接收参数的方法主要有以下三种: Struts2中Action接收参数的方法主要有以下三种: 1.使用Action的属性接收参数: a.定义:在Action类中定义属 ...
- 浅谈js执行机制
关于js执行机制,老早之前就一直想写篇文章做个总结,因为和js执行顺序的面试题碰到的特别多,每次碰到总是会去网上查,没有系统地总结,搞得每次碰到都是似懂非懂的感觉,这篇文章就系统的总结一下js执行机制 ...
- CodeForces - 55D Beautiful numbers —— 数位DP
题目链接:https://vjudge.net/problem/CodeForces-55D D. Beautiful numbers time limit per test 4 seconds me ...
- Jackson 对象与json数据互转工具类JacksonUtil
1,User对象 package com.st.json; import java.util.Date; /** * @Description: JSON序列化和反序列化使用的User类 * @aut ...
- Linux系统中的运行级别
什么是运行级呢?简单的说,运行级就是操作系统当前正在运行的功能级别. 它让一些程序在一个级别启动,而另外一个级别的时候不启动. Linux系统的有效登录模式有0~9共十种,不过沿用UNIX系统的至多6 ...
- close() was never explicitly called on database
在用SQLiteDatabase的时候如果碰到说database或者cursor没有关闭,可以在使用完之后加上: if (!cursor.isClosed()) { cursor.close(); } ...
- iOS NSInteger/NSUInteger与int/unsigned int、long/unsigned long之间的区别!
在iOS开发中经常使用NSInteger和NSUInteger,而在其他的类似于C++的语言中,我们经常使用的是int.unsigned int.我们知道iOS也可以使用g++编译器,那么它们之间是否 ...
- Watir: Watir webdriver对JS 弹出框的操作现在非常简单。
以下代码支持Firefox,IE,Chrome require 'watir-webdriver' #require "watir-webdriver/extensions/alerts&q ...
- bzoj1207 [HNOI2004]打鼹鼠——LIS
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1207 这题和求LIS有点像,打这一只鼹鼠一定可以从打上一只鼹鼠转移过来: 所以不用考虑机器人 ...