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的更多相关文章

  1. HU 参考错误修正:/SCWM/RCORR_HUREF

    HU 参考错误修正:report: /SCWM/RCORR_HUREF HU 参考的ODO/ID的凭证号及行项目号不正确的修正程序.

  2. 【BZOJ】1043: [HAOI2008]下落的圆盘(计算几何基础+贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1043 唯一让我不会的就是怎么求圆的周长并QAAQ... 然后发现好神!我们可以将圆弧变成$[0, 2 ...

  3. 形状特征提取-Hu不变矩(转载)

    [原文部分转载]:http://blog.csdn.net/wrj19860202/archive/2011/04/16/6327094.aspx 在连续情况下,图像函数为 ,那么图像的p+q阶几何矩 ...

  4. 51nod 1043 幸运号码(数位dp)

    题目链接:51nod 1043 幸运号码 题解:dp[i][j]表示 i 个数和为 j 的总数(包含0开头情况) dp[i][j] = dp[i-1][j-k] i & 1 :这里用滚动数组节 ...

  5. 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]*( ...

  6. 二分--1043 - Triangle Partitioning

    1043 - Triangle Partitioning PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit:  ...

  7. hdu 1043 Eight 经典八数码问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 The 15-puzzle has been around for over 100 years ...

  8. 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 ...

  9. 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 ...

  10. hihocoder 1043 完全背包

    #1043 : 完全背包 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 且说之前的故事里,小Hi和小Ho费劲心思终于拿到了茫茫多的奖券!而现在,终于到了小Ho领取奖励的 ...

随机推荐

  1. Web前端学习第三天(cookie 二)

    HttpOniy Cookie机制 在http层面传输cookie,当设置HttpOnly标志后,客户端脚本就无法读写该cookie.可以用此防止xss攻击获取cookie <?php setc ...

  2. Linux可以生产uImage

    默认kernel只生产Image和zImage,若想让kernel生产uImage,需要用到mkimage,这个是uboot可以提供的,位于uboot/tool/目录下,将其加入到环境变量即可.

  3. C++ operator 学习

    1.概述 1.1what operator 是c++的一个关键字,和运算符如(=)一起使用,表示一个运算符重载函数 1.2why 对于C++提供的所有操作符,通常只支持对于基本数据类型和标准库中提供的 ...

  4. robotframework中的清除输入框输入值

    业务需求 当该输入框输入之后,联动某一个按钮高亮,输入框为空的时候,该按钮置灰 需要将输入框清空,清空的办法 1.直接将输入框赋值为${empty} 如:input Text ${loactor} $ ...

  5. java数据结构之HashSet和HashMap(java核心卷Ⅰ读书笔记)

    增加 删除 remove方法,可以删除指定的一个元素. 查找 ********************* **************************** HashSet既不可以用 0 1 2 ...

  6. FirewallD 快速使用文档

    FirewallD简介 FirewallD是CentOS7系列上代替iptables管理netfilter的配置工具,提供图形化和命令行,使用python开发(新版中计划使用c++重写),提供图形化和 ...

  7. devexpress gridview代码设置

    39 //绑定DataTable 40 gridControl1.DataSource = dt; 41 gridView1.OptionsCustomization.AllowColumnMovin ...

  8. 详解java定时任务---Timer篇

    一.简介      在java的jdk中提供了Timer.TimerTask两个类来做定时任务. Timer是一种定时器工具,用来在一个后台线程计划执行指定任务,而TimerTask一个抽象类,它的子 ...

  9. Git 基础和原理

    Git 究竟是怎样的一个系统呢? 请注意接下来的内容非常重要,若你理解了 Git 的思想和基本工作原理,用起来就会知其所以然,游刃有余. 在开始学习 Git 的时候,请努力分清你对其它版本管理系统的已 ...

  10. centos7 时间自动同步

    设置开机自动同步Internet时间,并作定时同步任务1.修改时区 rm -rf /etc/localtime ln -s /usr/share/zoneinfo/Asia/Shanghai /etc ...