HU-1043
http://acm.hdu.edu.cn/showproblem.php?pid=1043
由于HDU这里的涉及多组数据,而每组数据按照一般的bfs()做法,即遍历每一种可行方案都保存一次路径,直到找到解就停下来,需要100+ms,这种做法放在多组数据中可能会超时,因此,不如我们只花100+ms来只做一次遍历,预先把所有能到达的状态找到并把路径记录下来,存放到char path[ ][ ]中,然后来一组数据就直接判断是否有这样的状态再进行输出(前面的也就是打表啦Σ(っ °Д °;)っ),大体上和一般的bfs()做法一样,可以使用队列,也可以使用循环自己模拟,主要问题是需要保存的状态是一个3 x 3 的矩阵,如果想要对每一种不同的序列都进行标记去重的话,选择康托展开那样的会很方便,就是说,通过某种线性变换运算(口糊),将这个 矩 阵 唯一的变成一个 对应的 实数,然后只用一维数组 (bool vis[ ])就可以进行状态标记了,嗯。具体康托展开什么的网上的讲解也有很多,本质是十进制转换为八进制,下面是在HDU上的ac代码:
#include <stdio.h>
#include <math.h>
#include <queue>
#include <string.h>
using namespace std;
int arr[3][3];
int vis[400000];
char path[400000][40];
int lev[] = {1,1,2,6,24,120,720,5040,40320,362880};
int dx[] = {-1,1,0,0};
int dy[] = {0,0,-1,1};
char dir[] = "durl";
int len;
struct node{
int s[9];
int loc;
int state;
int fa;
char d;
}n[400000];
int Inverse_cantor(int s[9])
{
int sum = 0;
for(int i = 0;i<9;i++)
{
int num = 0;
for(int j = i + 1;j<9;j++)
if(s[j] < s[i])
num++;
sum += num*lev[9 - i -1];
}
return sum + 1;
}
void find_path(node end)
{
int state = end.state;
int f = end.fa;
len = 0;
path[state][len++] = end.d;
while(f)
{
path[state][len++] = n[f].d;
f = n[f].fa;
}
}
void bfs(){
memset(vis, 0,sizeof vis);
node nex;
int head = 0,tail = 0;
for(int i = 0;i<8;i++)
n[0].s[i] = i+1;
n[0].s[8] = 0;
n[0].loc = 8;
n[0].state = 46234;
vis[46234] = 1;
while(head<=tail)
{
int x = n[head].loc / 3;
int y = n[head].loc % 3;
for(int i = 0;i<4;i++)
{
int tx = x + dx[i];
int ty = y + dy[i];
if(tx < 0 || tx>2||ty<0||ty>2) continue;
nex = n[head];
nex.loc = tx*3 + ty;
nex.s[n[head].loc] = nex.s[nex.loc];
nex.s[nex.loc] = 0;
nex.fa = head;
nex.d = dir[i];
nex.state = Inverse_cantor(nex.s);
if(!vis[nex.state])
{
vis[nex.state] = 1;
find_path(nex);
n[++tail] = nex;
}
}
head++;
}
}
int main()
{
bfs();
char ch[3];
node cur;
while(~scanf("%s",ch))
{
if(!strcmp(ch,"x"))
{
cur.s[0] = 0,cur.loc = 0;
}
else
{
cur.s[0] = ch[0] - '0';
}
for(int i = 1;i<9;i++)
{
scanf("%s",ch);
if(!strcmp(ch,"x"))
{
cur.s[i] = 0,cur.loc = i;
}
else
{
cur.s[i] = ch[0] - '0';
}
}
cur.state = Inverse_cantor(cur.s);
if(vis[cur.state])
printf("%s\n",path[cur.state]);
else
printf("unsolvable\n");
}
return 0;
}
HU-1043的更多相关文章
- HU 参考错误修正:/SCWM/RCORR_HUREF
HU 参考错误修正:report: /SCWM/RCORR_HUREF HU 参考的ODO/ID的凭证号及行项目号不正确的修正程序.
- 【BZOJ】1043: [HAOI2008]下落的圆盘(计算几何基础+贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1043 唯一让我不会的就是怎么求圆的周长并QAAQ... 然后发现好神!我们可以将圆弧变成$[0, 2 ...
- 形状特征提取-Hu不变矩(转载)
[原文部分转载]:http://blog.csdn.net/wrj19860202/archive/2011/04/16/6327094.aspx 在连续情况下,图像函数为 ,那么图像的p+q阶几何矩 ...
- 51nod 1043 幸运号码(数位dp)
题目链接:51nod 1043 幸运号码 题解:dp[i][j]表示 i 个数和为 j 的总数(包含0开头情况) dp[i][j] = dp[i-1][j-k] i & 1 :这里用滚动数组节 ...
- 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]*( ...
- 二分--1043 - Triangle Partitioning
1043 - Triangle Partitioning PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit: ...
- hdu 1043 Eight 经典八数码问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 The 15-puzzle has been around for over 100 years ...
- hihoCoder 1043 完全背包 (dp)
http://hihocoder.com/problemset/problem/1043 动态转移方程 :for v=cost..V f[v]=max(f[v],f[v-c[i]]+w[i]); #i ...
- 1043. Is It a Binary Search Tree (25)
the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1043 and the ...
- hihocoder 1043 完全背包
#1043 : 完全背包 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 且说之前的故事里,小Hi和小Ho费劲心思终于拿到了茫茫多的奖券!而现在,终于到了小Ho领取奖励的 ...
随机推荐
- vue day8 table page
@{ ViewBag.Title = "Home Page"; Layout = null; } <!DOCTYPE html> <html> <he ...
- HTC Vive设备拥有陀螺仪。
//设置设备陀螺仪的开启/关闭状态,使用陀螺仪功能必须设置为 true Input.gyro.enabled = true; //获取设备重力加速度向量 Vector3 deviceGravity = ...
- Sublime3安装及配置
1.官网下载 2.安装后输入密钥 ----- BEGIN LICENSE -----sgbteamSingle User LicenseEA7E-11532598891CBB9 F1513E4F 1A ...
- 【缓存】介绍和使用场景 MEMCACHE REDIS
缓存缓存就是在内存中存储的数据备份,当数据没有发生本质改变的时候,我们就不让数据的查询去数据库进行操作,而去内存中取数据,这样就大大降低了数据库的读写次数,而且从内存中读数据的速度比去数据库查询要快一 ...
- 用理论告诉你 三极管和MOS管的区别在哪
在电路设计当中假设我们想要对电流中止控制,那就少不了三极管的帮助.我们俗称的三极管其全称为半导体三极管,它的主要作用就是将微小的信号中止放大.MOS管与三极管有着许多相近的地方,这就使得一些新手不断无 ...
- mini-treeselect的动态赋值
<div id="faultTree" allowdrag="false" allowdrop="true" class=" ...
- [踩坑系列]URLEncode 中对 空格的编码有 “+”和“%20”两种
URL中的空格有时候被编码成%20,有时候被编码成加号+,曾经迷糊过一段时间,后来查了下资料才搞明白. 一个URL的基本组成部分包括协议(scheme),域名,端口号,路径和查询字符串(路径参数和锚点 ...
- 1、链表之增、删、查实现(C语言)
一.功能描述: 可以创建节点并添加到链表中.查看链表中的所有节点.并可以删除特定的节点 二.代码实现 1.主函数main主要实现的是从后台键入不同的字符,执行对应的函数来实现特定的操作代码如下: in ...
- Pytest高级进阶之Fixture
From: https://www.cnblogs.com/feiyi211/p/6626314.html 一. fixture介绍 fixture是pytest的一个闪光点,pytest要精通怎么能 ...
- C#工具类:Json操作帮助类(转载)
原文转载自C#工具类:Json操作帮助类_IT技术小趣屋. Json序列化和反序列化在程序开发中时常会遇到,在C#中可以使用很多种方法实现对数据的Json序列化和反序列化,封装一个Json操作工具类来 ...