</pre><span style="font-family:SimSun;">基本功不够导致浪费了很多时间啊。。。</span><p></p><p><span style="font-family:SimSun;">开始没想清楚,傻乎乎的用dfs结果运行起来栈溢出都出不来结果。其实题目里的最远状态是指从初始状态出发,走最近的路能到达的所有状态中最远的状态,因此,必然要用<span style="font-size: 12px; color: rgb(68, 68, 68);">bfs。</span></span></p><p><span style="font-size: 12px; color: rgb(68, 68, 68);"><span style="font-family:SimSun;">A 9 9=362800,其实s数组不用开到300000就可以,因为在移动过程中,数字序列的逆序数的奇偶性不会改变,当然我还不敢说一定奇偶各占一半(今天翻了下线代书,发现确实各占一半 20140919)。</span></span></p><p><span style="font-size: 12px; color: rgb(68, 68, 68);"><span style="font-family:SimSun;">然后是哈希,额,自己开始按自己的思路写的</span></span></p><p><span style="font-family:Trebuchet MS,Verdana,Helvetica,Arial,sans-serif; font-size:12px; color:#444444"></span></p><p><span style="font-family:Trebuchet MS,Verdana,Helvetica,Arial,sans-serif; font-size:12px; color:#444444"></span></p><p><span style="font-family:Trebuchet MS,Verdana,Helvetica,Arial,sans-serif; font-size:12px; color:#444444"></span></p><pre name="code" class="cpp"> int hashvalue=getvalue(index);
int u=head[hashvalue];
if(u==-1)//可以等于0,因为第一个已经在statee里面
{
head[hashvalue]=index;
next[index]=-1;
return 1;
}
if(memcmp(statee[index],statee[u],9)==0)
return 0;
while(next[u]!=-1)
{
if(memcmp(statee[index],statee[u],9)==0)
return 0;
help=u;
u=next[u];
}
next[index]=index;
next[index]=-1;
return 1;
每次都没判断最后一个情况,应该是:
int hashvalue=getvalue(index);
int u=head[hashvalue];
if(u==-1)//可以等于0,因为第一个已经在statee里面
{
head[hashvalue]=index;
next[index]=-1;
return 1;
}
if(memcmp(statee[index],statee[u],9)==0)
return 0;
int help;
while(u!=-1)
{
if(memcmp(statee[index],statee[u],9)==0)
return 0;
help=u;
u=next[u];
}
next[help]=index;
next[index]=-1;
return 1;
int h = getvalue(index);
int u = head[h];
while (u) {
if (memcmp(statee[u], statee[index], sizeof(statee[index])) == 0) return 0;
u = next[u];
}
next[index] = head[h];
head[h] = index;
return 1;
完整代码如下:
#include<cstdio>
#include<cstring>
//#include<algorithm>
#define STEP 200
#define MAX 300000
#define HASHSIZE 1000003
//using namespace std;
static char input[3][3],statee[MAX][3][3],move[MAX][STEP],direction[4]={'U','R','D','L',};//超内存了,,改用哈希吧
int amountofmove[MAX],front,rear,vector[4][2]={{-1,0},{0,1},{1,0},{0,-1},},head[HASHSIZE],next[MAX];
int getvalue(int cur)
{
int i,j;
int value1=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
value1=(value1*10+statee[cur][i][j])%HASHSIZE;
}
}
return ((value1)%HASHSIZE);
}
int insert(int index)
{
int hashvalue=getvalue(index);
int u=head[hashvalue];
if(u==-1)//可以等于0,因为第一个已经在statee里面
{
head[hashvalue]=index;
next[index]=-1;
return 1;
}
if(memcmp(statee[index],statee[u],9)==0)
return 0;
int help;
while(u!=-1)
{
if(memcmp(statee[index],statee[u],9)==0)
return 0;
help=u;
u=next[u];
}
next[help]=index;
next[index]=-1;
return 1;
}
void fun(int which)
{
int i,j,x,y,xx,yy;
memcpy(statee[rear],statee[front],9);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
if(statee[front][i][j]==0)
{
x=i,y=j;
goto next;//又写错一次。。不过没有影响
}
}
next:
xx=x+vector[which][0],yy=y+vector[which][1];
if(xx>=0&&xx<3&&yy>=0&&yy<3)//写错一次
{
statee[rear][x][y]=statee[rear][xx][yy];
statee[rear][xx][yy]=0;
if(insert(rear))
{
memcpy(move[rear],move[front],STEP);
move[rear][amountofmove[front]]=direction[which];
amountofmove[rear]=amountofmove[front]+1;
rear++;
}
}
}
void bfs()
{
int i,j;
front=0,rear=1;
memset(head,-1,HASHSIZE*4);
memset(next,-1,MAX*4);
memcpy(statee[front],input,9);
amountofmove[front]=0;
insert(front);//这一句少了
while(front<rear)
{
for(i=0;i<4;i++)
fun(i);
front++;
}
int x=1;
//for( x=1;x<20;x++)
//{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d",statee[rear-x][i][j]);
if(j!=2)
printf(" ");
}
printf("\n");
}
for(i=0;i<amountofmove[rear-x];i++)
printf("%c",move[rear-x][i]);
printf("\n\n");
//}
}
int main()
{
int n,i,j;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Puzzle #%d\n",i);
for(j=0;j<9;j++)
scanf("%d",&input[j/3][j%3]);
bfs();
}
return 0;
}