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 ...
随机推荐
- win/linux 下使用 psutil 获取进程 CPU / memory / IO 占用信息
psutil - A cross-platform process and system utilities module for Python 1. 安装 pip 安装即可. windows 下需要 ...
- java.util.Hashtable源码分析
Hashtable实现一个键值映射的表.任何非null的object可以用作key和value. 为了能存取对象,放在表里的对象必须实现hashCode和equals方法. 一个Hashtable有两 ...
- Qt-获取主机网络信息之QNetworkAddressEntry
QNetworkAddressEntry类存储了一个网络接口所支持的一个IP地址,同时还有与之相关的子网掩码和广播地址. 每个网络接口可以包含0个或多个IP地址,这些IP地址可以分别关联一个子网掩码和 ...
- 用C#开发一个WinForm版的批量图片压缩工具
我们在实际项目开发过程中,曾经遇到过一个需求,就是要开发一个对大量图片进行整理(删除掉一些不符合要求的图片).归类(根据格式进行分类,比如jpg格式.bmp格式等).压缩(因为有的图片很大很占空间,看 ...
- python(二)基础
1.lambda表达式(一般格式为冒号前边是参数冒号后边是返回值) 单参数: 多参数: 我们如果实现一个普通的filter过滤函数过滤掉偶数: 而我们如果用lambda表达式就很简便: 2.递归 普通 ...
- Centos系统mysql 忘记root用户的密码
Centos系统mysql 忘记root用户的密码: 第一步:(停掉正在运行的mysql) [root@maomao ~]# /etc/init.d/mysqld stop Stopping MySQ ...
- Be Pythonic ,Google Python Style Guide
为了更规范的写代码,变得更专业 分号 1 不在句末添加分号,不用分号在一行写两句代码 行长度 2 每行不超过80字符,python会隐式行连接圆括号,中括号,花括号中的字符,如多参数方法调用可以写为多 ...
- WPF 控件
****ScrollViewer**** 前台: <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalSc ...
- 【JPA】两种不同的实现jpa的配置方法
两种不同的实现jpa的配置方法 第一种: com.mchange.v2.c3p0.ComboPooledDataSource datasource.connection.driver_class=co ...
- 一步步学习NHibernate(4)——多对一,一对多,懒加载(1)
请注明转载地址:http://www.cnblogs.com/arhat 通过上一章的学习,我们学会如何使用NHibernate对数据的简单查询,删除,更新和插入,那么如果说仅仅是这样的话,那么NHi ...