eight - zoj 1217 poj 1077
学习了多位大牛的方法,看看到底能把时耗降到多少?
A*
// zojfulltest: 30000ms
# include <stdio.h>
# include <ctype.h>
# include <stdlib.h>
# include <string.h> # define DATA(x,i) (((x)>>((i)*))&0x7)
# define ZERO(x) ((x)>>)
# define F(x) ((x)&0xFF)
# define D(x) (((x)>>)&0xF)
# define P(x) ((x)>>)
# define MAKE(f,d,p) ((f)|((d)<<)|((p)<<))
# define LESS(x,y) (((x)-(y))>>)
# define SWP(i,j,t) (((t)<<(*(i)))-((t)<<(*(j)))+((j)<<)-((i)<<)) const unsigned int maxd = ;
//dest: 0100 0000 0001 1111 0101 1000 1101 0001
const unsigned int dest = 0x401F58D1;
const unsigned int hashmod = ;
const unsigned int maxnode = 0x1<<;
const unsigned int maxsize = 0x1<<;
const int mv[][] = {{-,-,, },{-,,, },{-,,-, },
{ ,-,, },{ ,,, },{ ,,-, },
{ ,-,,-},{ ,,,-},{ ,,-,-}};
const char mvs[] = "ulrd"; struct state {
unsigned int u, v;
};
state a[maxnode], child;
unsigned int src, depth, totnode, h[][], t[];
unsigned int head[hashmod], next[maxnode];
unsigned int hp[maxsize], hps;
unsigned int stk[maxsize], top; void init(void)
{
memset(h, , sizeof(h));
for (int j, i = ; i != ; ++i) {
for (j = ; j != ; ++j) {
h[i][j] = abs(j/-(i-)/)+abs(j%-(i-)%);
}
}
for (int i = ; i != ; ++i) {
h[][i] = h[][i];
}
}
bool read_src(void)
{
src = ;
for (int ch, i = ; i != ; ) {
ch = getchar();
if (isdigit(ch)) t[i++] = ch-'';
else if (ch == 'x') {
t[i] = ;
src |= ((i++)<<);
} else if (ch == EOF) return false;
}
for (int i = ; i != ; ++i) src |= ((t[i]&0x7)<<(*i));
return true;
}
bool no_answer(void)
{
int inv = -ZERO(src), i, j;
for (i = ; i != ; ++i) {
for (j = i+; j != ; ++j) {
inv += LESS(t[j],t[i]);
}
}
return ((inv)&0x1);
}
void print_sol(int ID)
{
if (ID == ) return ;
print_sol(P(a[ID].v));
putchar( mvs[D(a[ID].v)] );
}
void addnode(void)
{
int k = child.u % hashmod;
for (int w = head[k]; w ; w = next[w]) {
if (a[w].u == child.u) {
if ( LESS(F(child.v), F(a[w].v)) ) {
a[w].v = child.v;
stk[top++] = w;
}
return ;
}
}
a[++totnode] = child;
next[totnode] = head[k]; head[k] = totnode;
if ( F(child.v) == depth ) stk[top++] = totnode;
else hp[hps++] = totnode;
} void solve(void)
{
if (no_answer()) { puts("unsolvable"); return; }
if (src == dest) { puts(""); return ; }
depth = -h[][ZERO(src)];
totnode = ;
hps = top = ;
memset(head, , sizeof(head));
for (int i = ; i != ; ++i) {
depth += h[ DATA(src,i) ][i];
}
child.u = src;
child.v = MAKE(depth, , );
addnode();
unsigned int ID;
for ( ; LESS(depth, maxd); depth += ) {
while (hps) {
ID = hp[--hps];
if ( F(a[ID].v) == depth ) {
stk[top++] = ID;
}
}
while (top) {
ID = stk[--top];
state & cur = a[ID];
int i = ZERO(cur.u), j;
for (int r = ; r != ; ++r) {
if (-!=(j=mv[i][r]) && (D(cur.v)+r)!=) {
int t = DATA(cur.u, j);
child.u = cur.u+SWP(i,j,t);
int f = F(cur.v)++h[t][i]-h[t][j];
child.v = MAKE(f,r,ID);
if (child.u == dest) {
a[++totnode] = child;
print_sol(totnode); puts("");
return ;
}
addnode();
}
}
}
}
} int main()
{
init();
while (read_src()) solve(); return ;
}
BFS + 记忆化
// zojfulltest: 1012ms
# include <stdio.h>
# include <ctype.h>
# include <string.h> # define LESS(x,y) (((x)-(y))>>)
# define DATA(x,i) (((x)>>((i)*))&0x7)
# define ZERO(x) ((x)>>)
# define P(x) ((x)>>)
# define D(x) ((x)&0xF)
# define MAKE(d,p) ((d)|((p)<<))
# define SWP(i,j,t) (((t)<<(*(i)))-((t)<<(*(j)))+((j)<<)-((i)<<)) const int totnode = ;
const int maxsize = ;
const int dest = 0x401F58D1;
const int destID = ;
const int fact[] = {,,,,,,,,};
const int mv[][] = {{-,-,, },{-,,, },{-,,-, },
{ ,-,, },{ ,,, },{ ,,-, },
{ ,-,,-},{ ,,,-},{ ,,-,-}};
const char mvs[] = "ulrd"; struct state {
unsigned int s;
unsigned int c;
} hp[maxsize], child; unsigned int front, rear, src, table[totnode]; unsigned int cantor(unsigned int s)
{
unsigned int ret = ;
unsigned int t[], i;
for (i = ; i != ; ++i) t[i] = DATA(s,i);
for (i = ; DATA(s,i)||i==ZERO(s); ++i) ;
t[i] = ;
for (i = ; i != ; ++i) {
unsigned int inv = ;
for (int j = ; j != i; ++j) {
inv += LESS(t[j],t[i]);
}
ret += inv*fact[i];
}
return ret;
}
void build_tree(void)
{
front = rear = ;
child.s = dest, child.c = destID;
table[destID] = MAKE(,destID);
hp[rear++] = child;
while (LESS(front, rear)) {
state & cur = hp[front++];
int i = ZERO(cur.s), d = table[cur.c], j;
for (int r = ; r != ; ++r) {
if (~(j=mv[i][r]) && (d+r)!=) {
child.s = cur.s+SWP(i,j,DATA(cur.s,j));
child.c = cantor(child.s);
if (table[child.c] == ) {
table[child.c] = MAKE(r, cur.c);
hp[rear++] = child;
}
}
}
}
}
bool read_src(void)
{
src = ;
for (int ch, i = ; i != ; ) {
ch = getchar();
if (isdigit(ch)) src |= (((ch-'')&0x7)<<(*i++));
else if (ch == 'x') src |= ((i++)<<);
else if (ch == EOF) return false;
}
return true;
}
void print_sol(int ID)
{
while (ID != destID) {
putchar(mvs[-D(table[ID])]);
ID = P(table[ID]);
}
}
void solve(void)
{
unsigned int c = cantor(src);
if (!table[c]) printf("unsolvable");
else print_sol(c);
printf("\n");
}
int main()
{
build_tree();
while (read_src()) solve();
}
构造
# include <stdio.h>
# include <ctype.h>
# include <string.h> # define LESS(x, y) (((x)-(y))>>)
# define REP(n) for ((i) = ; src[i] != (n); ++(i)) const char *t0[] = {"rd","d","ld","r","","l","ur","u","ul"};
const char *t1[] = {"","lurd","urdllurd","uldr","","urdlurdllurd","ldruuldr","ldruldruuldr","rdluurdlurdllurd"};
const char *t2[] = {"","urddllur","urdlurddllur","","","rdllur","ldru","dlur","druldlur"};
const char *t3[] = {"","ruld","","","","urdl","dlurdrulurdlldru","drulurdl","rdluurdl"};
const char *t4 = "ldrrulurdl";
const char *t5[] = {"","","","","","rdllur","ldru","dlur","druldlur"};
const char *t6[] = {"","","","","","rdlu","dlurdrulldrurdlu","","rdlurdlu"};
const char *t7 = "dlur";
const char *t8[] = {"","","","","","rd","","dr","rdlurd"};
const char *mvs = "ulrd";
const int mv[][] = {{-,-,, },{-,,, },{-,,-, },
{ ,-,, },{ ,,, },{ ,,-, },
{ ,-,,-},{ ,,,-},{ ,,-,-}
}; unsigned int src[], pos;
char ans[], top; int mr(char ch)
{
switch(ch) {
case 'u':
return ;
case 'l':
return ;
case 'r':
return ;
case 'd':
return ;
}
}
void mov(const char * str)
{
int n = strlen(str);
for (int i = ; i < n; ++i) {
int t = mv[pos][mr(str[i])];
src[pos] = src[t];
src[pos = t] = ;
}
memcpy(ans+top, str, n);
top += n;
}
void solve(void)
{
int i;
top = ;
mov( t0[pos] ); // 0->4
REP();
mov( t1[i] ); // 1->0
REP();
mov( t2[i] ); // 3->3
REP();
mov( t3[i] ); // 2->2
mov( t4 ); // 2->1, 3->2
REP();
mov( t5[i] ); // 7->3
REP();
mov( t6[i] ); // 4->7
mov( t7 ); // 4->3, 7->7
REP();
mov( t8[i] ); // 5->4, 0->8
ans[top] = ;
if ( src[] == ) puts(ans);
}
bool no_answer(void)
{
int inv = -pos;
for (int i = ; i != ; ++i) {
for (int j = i+; j != ; ++j) {
inv += LESS(src[j], src[i]);
}
}
return ((inv)&0x1);
}
bool read_src(void)
{
for (int i = ; i != ; ) {
int ch = getchar();
if (isdigit(ch)) src[i++] = ch-'';
else if (ch == 'x') src[pos = i++] = ;
else if (ch == EOF) return false;
}
return true;
}
int main()
{
while ( read_src() ) {
if ( no_answer() ) puts("unsolvable");
else solve();
}
return ;
}
eight - zoj 1217 poj 1077的更多相关文章
- HDU 1043 & POJ 1077 Eight(康托展开+BFS+预处理)
Eight Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30176 Accepted: 13119 Special ...
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...
- HDU 1043 & POJ 1077 Eight(康托展开+BFS | IDA*)
Eight Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30176 Accepted: 13119 Special ...
- Eight POJ - 1077 HDU - 1043 八数码
Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...
- HDU - 1043 - Eight / POJ - 1077 - Eight
先上题目: Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- 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]*( ...
- hdu 1043 pku poj 1077 Eight (BFS + 康拓展开)
http://acm.hdu.edu.cn/showproblem.php?pid=1043 http://poj.org/problem?id=1077 Eight Time Limit: 1000 ...
- poj 1077 Eight(双向bfs)
题目链接:http://poj.org/problem?id=1077 思路分析:题目要求在找出最短的移动路径,使得从给定的状态到达最终状态. <1>搜索算法选择:由于需要找出最短的移动路 ...
- poj 3100 (zoj 2818)||ZOJ 2829 ||ZOJ 1938 (poj 2249)
水题三题: 1.给你B和N,求个整数A使得A^n最接近B 2. 输出第N个能被3或者5整除的数 3.给你整数n和k,让你求组合数c(n,k) 1.poj 3100 (zoj 2818) Root of ...
随机推荐
- 九度OJ 1120 全排列 -- 实现C++STL中next_permutation()
题目地址:http://ac.jobdu.com/problem.php?pid=1120 题目描述: 给定一个由不同的小写字母组成的字符串,输出这个字符串的所有全排列. 我们假设对于小写字母有'a' ...
- cookie、localStorage、sessionStorage之间的区别
sessionStorage 和 localStorage 是HTML5 Web Storage API 提供的,可以方便的在web请求之间保存数据.有了本地数据,就可以避免数据在浏览器和服务器间不必 ...
- 转 修改oracle用户密码永不过期
1.查看用户的proifle是哪个,一般是default: sql>SELECT username,PROFILE FROM dba_users; 2.查看指定概要文件(如default)的 ...
- vb delphi7、2010 csharp vb.net空白测试程序
工作中难免在网上看到一段不错的代码,希望能够拿来测试一次,为了避免每次测试都要新建一个空白测试程序,索性预先建立好,要用的时候复制一遍,然后打开直接粘贴需要测试的代码进行测试.
- 【python】疯了,掉坑里出不来了
学软件最头疼的事情就是版本换来换去: 各种配置错误,疯了,疯了--
- Silverlight应用程序中调用WCF Ria Services访问数据库图片
WCF Ria Services(通常称为RIA服务),专门设计让Silverlight应用程序访问数据库,网上有关其示例应用都是基于简单的数据显示,其中MSDN网站上有详细的解决方案介绍,地址htt ...
- Oracle 插入数据
6个柜面交易 打印修改--050101 delete from tran_prints where tran_id = (select id from tran where code='050101' ...
- iOS8定位问题
正文:主要解决iOS8以前能定位,但是在iOS8时候无法定位的问题 在iOS8以前,我们的GPS定位是在用户设置的里面显示的是总是使用,但是在iOS8以后,苹果修改了这部分授权,你需要多加入2个pli ...
- 将 Photoshop CC 2015.5 英文界面换成中文, 英文与中文界面互换
注:转载或引用请注明出处 在英文的win server 2012 r2 上安装PS CC 2015.5 时,安装程序自动按成了英文版的PS,那么如何将英文换成中文呢? 网上大多讲的都是将中文换成英文, ...
- [JavaScript] 初中级Javascript程序员必修学习目录
很多人总感觉javascript无法入门,笔者在这里写一下自己的学习过程,以及个人认 为的最佳看书过程,只要各位能按照本人所说步骤走下去,不用很长时间,坚持 个3个月,你的js层级会提高一个档次,无他 ...