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领取奖励的 ...
随机推荐
- centos7通过yum安装mysql8
1.检查是否安装mariadb rpm -qa | grep mariadb 若有会显示 mariadb-libs-5.5.56-2.el7.x86_64 2.卸载mariadb rpm -e --n ...
- 去重和分类后缀asp、php等路径 用python3写的
我们在做渗透的时候肯定会用上扫描器的,本人一般会用御剑,当然你也会喜欢别的工具. 很多时候,能否渗透成功其实还挺依赖与字典的,如果把后台给扫出来了,恰好还弱口令,那么岂不是美滋滋. 因此,有一个好的字 ...
- 游戏人工智能编程案例精粹(修订版) (Mat Buckland 著)
https://www.jblearning.com/catalog/productdetails/9781556220784 第1章 数学和物理学初探 (已看) 第2章 状态驱动智能体设计 (已看) ...
- rs232接口定义
- 密码疑云 (2)——RSA加密机制需要的数学知识
在公钥密码体制提出不久,人们就找到其中的三种,其中最著名的当属RSA体制.RSA是一种非对称加密体制,在公开密钥加密和电子商业中被广泛使用.RSA是1977年由罗纳德·李维斯特(Ron Rivest) ...
- [转]Java调用Javascript、Python算法总结
最近项目中经常需要将Javascript或者Python中的算法发布为服务,而发布Tomcat服务则需要在Java中调用这些算法,因此就不免要进行跨语言调用,即在Java程序中调用这些算法. 不管是调 ...
- 46 Simple Python Exercises (前20道题)
46 Simple Python Exercises This is version 0.45 of a collection of simple Python exercises construct ...
- DevExpress中GridControl的重新绑定数据后如何刷新 (转)
如果对girdcontrol的datasource新添加数据,重新刷新, gridControl1.DataSource = list; gridView1.RefreshData();
- springboot Cacheable(redis),解决key乱码问题
import org.springframework.context.annotation.Bean; import org.springframework.data.redis.core.Redis ...
- 精读《C++ primer》学习笔记(第一至三章)
第一章: 重要知识点: 类型:一种类型不仅定义了数据元素的内容,还定义了这类数据上可以进行的运算:所以说类定义,实际上就是定义了一种数据类型: >>和<<运算符返回其左侧的运算 ...