学习了多位大牛的方法,看看到底能把时耗降到多少?

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 ;
}

fulltest.txt

eight - zoj 1217 poj 1077的更多相关文章

  1. HDU 1043 & POJ 1077 Eight(康托展开+BFS+预处理)

    Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30176   Accepted: 13119   Special ...

  2. ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法

    题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...

  3. HDU 1043 & POJ 1077 Eight(康托展开+BFS | IDA*)

    Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30176   Accepted: 13119   Special ...

  4. Eight POJ - 1077 HDU - 1043 八数码

    Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...

  5. HDU - 1043 - Eight / POJ - 1077 - Eight

    先上题目: Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

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

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

  8. poj 1077 Eight(双向bfs)

    题目链接:http://poj.org/problem?id=1077 思路分析:题目要求在找出最短的移动路径,使得从给定的状态到达最终状态. <1>搜索算法选择:由于需要找出最短的移动路 ...

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

随机推荐

  1. 九度OJ 1056--最大公约数 1439--Least Common Multiple 【辗转相除法】

    题目地址:http://ac.jobdu.com/problem.php?pid=1056 题目描述: 输入两个正整数,求其最大公约数. 输入: 测试数据有多组,每组输入两个正整数. 输出: 对于每组 ...

  2. object-fit: 炒鸡方便的图片居中方法

    今天在项目中遇到图片居中的问题,嗯,之前也有写过解决这个问题的文章,有n种方法.不过今天要说一个新的方案:object-fit ,嗯,这个才是真的方便的方案啊. 先看预览: object-fit 只能 ...

  3. 用 BPL 封装数据连接

    BPL 代码: uDM.pas unit uDM; interface uses SysUtils, Classes, uIntf, DB, ABSMain; type TDM = class(TDa ...

  4. Django视图与网址传参

    目的:采用/add?a=1&b=2  这样get/post方法进行 修改一下mysite/views.py文件 from django.shortcuts import renderfrom ...

  5. CMOS (1)–PMOS与NMOS

    1,名称来源 p,n指示的是生成的沟道类型 2,驱动逻辑0与逻辑1 一般用NMOS驱动逻辑0,用PMOS驱动逻辑1.

  6. JAVA 内存管理总结

    1. java是如何管理内存的 Java的内存管理就是对象的分配和释放问题.(两部分) 分配 :内存的分配是由程序完成的,程序员需要通过关键字new 为每个对象申请内存空间 (基本类型除外),所有的对 ...

  7. ThinkPHP框架安全性能分析

    http://www.freebuf.com/articles/web/59713.html 点击劫持cookie 点击劫持所有链接

  8. Catch Application Exceptions in a Windows Forms Application

    You need to handle the System.Windows.Forms.Application.ThreadException event for Windows Forms. Thi ...

  9. 【软件分享】文本对比工具 Beyond Compare

    转载自公众号:EmbeddDeveloper 对嵌入式感兴趣可以关注原作者博客: http://blog.csdn.net/ybhuangfugui 此处转载为分享用 Ⅰ.摘要 Beyond Comp ...

  10. 贝塞尔曲线算法,js贝塞尔曲线路径点

    //anchorpoints:贝塞尔基点 //pointsAmount:生成的点数 //return 路径点的Array function CreateBezierPoints(anchorpoint ...