静静的水瓶

^*_*^

逝者如斯
网志分类
· 所有网志 (8)
· 好友连接 (0)
· 天下乱侃 (0)
· ACM/ICPC (5)
· 网络转载 (2)
· 心情日记 (1)
搜索本站
友情链接
· 我的歪酷
· 思念的青苹果
· 虚掩的门
· 淡蓝的天空

订阅 RSS

0007207

歪酷博客


« 上一篇: 2005年百度之星总决赛(题目及代码~~) 亚军Iamcs的代码 下一篇: 八数码实验报告 »
taney @ 2006-08-04 17:12

#i nclude <iostream>
#i nclude <cstdio>
#i nclude <memory>

using namespace std;

struct map
{
int Gap;
int Board[9];
};

const int max_len = 20000;
const int hash_size = 59999;

int SLen, DLen, SCursor, DCursor, SStep, DStep;
int SQ[max_len], DQ[max_len];
char SGaps[max_len], DGaps[max_len];
char Hash[hash_size];
int HashKeys[hash_size];
map SMap, DMap;
int SKey, DKey;

inline char& hash(int V)
{
int Key = V * 4 % hash_size;
V++;
while (HashKeys[Key] != 0 && HashKeys[Key] != V)
{
Key++;
if (Key == hash_size) Key = 0;
}
HashKeys[Key] = V;
return Hash[Key];
}

inline int convert(int P[])
{
int I;
int Key = 0;
for (I = 8; I >= 0; I--)
if (P[I] != 0)
{
Key <<= 3;
Key |= P[I] - 1;
}
return Key;
}

bool search(int& FCursor, int& FLen, int Q[], char Gaps[], int Dir)
{
int Cursor = FCursor;
int Len = FLen;
int Limit = Len;
while (Cursor < Limit)
{
int Key = Q[Cursor];
char Gap = Gaps[Cursor];
int Delta = Gap + Gap + Gap;
if (Gap < 6)
{
int Key2 = Key & ~(511 << Delta) | ((Key & (63 << Delta)) << 3) | ((Key & (7 << (Delta + 6))) >> 6);
char Gap2 = Gap + 3;
char& H = hash(Key2 * 9 + Gap2);
if ((H & Dir) != Dir)
if ((H |= Dir) == 3) return true;
else
{
Q[Len] = Key2;
Gaps[Len++] = Gap2;
}
}
if (Gap >= 3)
{
int Key2 = Key & ~(511 << (Delta - 9)) | ((Key & (63 << (Delta - 6))) >> 3) | ((Key & (7 << (Delta - 9))) << 6);
char Gap2 = Gap - 3;
char& H = hash(Key2 * 9 + Gap2);
if ((H & Dir) != Dir)
if ((H |= Dir) == 3) return true;
else
{
Q[Len] = Key2;
Gaps[Len++] = Gap2;
}
}
if (Gap != 0 && Gap != 3 && Gap != 6)
{
int Key2 = Key;
char Gap2 = Gap - 1;
char& H = hash(Key2 * 9 + Gap2);
if ((H & Dir) != Dir)
if ((H |= Dir) == 3) return true;
else
{
Q[Len] = Key2;
Gaps[Len++] = Gap2;
}
}
if (Gap != 2 && Gap != 5 && Gap != 8)
{
int Key2 = Key;
char Gap2 = Gap + 1;
char& H = hash(Key2 * 9 + Gap2);
if ((H & Dir) != Dir)
if ((H |= Dir) == 3) return true;
else
{
Q[Len] = Key2;
Gaps[Len++] = Gap2;
}
}
Cursor++;
}
FCursor = Cursor;
FLen = Len;
return false;
}

void read_map(map& Map)
{
int I;
for (I = 0; I < 9; I++)
{
scanf(" %d", &Map.Board[I]);
if (Map.Board[I] == 0) Map.Gap = I;
}
}

void perform()
{
freopen("start.txt", "r", stdin);
read_map(SMap);
freopen("goal.txt", "r", stdin);
read_map(DMap);
SKey = convert(SMap.Board);
DKey = convert(DMap.Board);
if (SMap.Gap == DMap.Gap && SKey == DKey)
{
printf("0\n");
return;
}
hash(SKey * 9 + SMap.Gap) |= 1;
hash(DKey * 9 + DMap.Gap) |= 2;
SLen = SCursor = DLen = DCursor = 0;
SQ[SLen] = SKey;
SGaps[SLen++] = SMap.Gap;
DQ[DLen] = DKey;
DGaps[DLen++] = DMap.Gap;
SStep = 0;
DStep = 0;
while (SStep < 16 || DStep < 16)
{
if (SCursor < SLen && DCursor < DLen && SStep <= DStep ||
DCursor >= DLen && SCursor < SLen)
if (search(SCursor, SLen, SQ, SGaps, 1))
{
printf("%d\n", SStep + DStep + 1);
return;
}
else
SStep++;
else if (DCursor < DLen)
if (search(DCursor, DLen, DQ, DGaps, 2))
{
printf("%d\n", SStep + DStep + 1);
return;
}
else
DStep++;
else
break;
}
printf("-1\n");
}

int main()
{
perform();
return 0;
}


评论 / 个人网页 / 扔小纸条
*昵称

已经注册过? 请登录

Email
网址
*评论