uva10085 The most distant state

4 篇文章 0 订阅
3 篇文章 0 订阅

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


这个是每次都在head之后加入新节点,比我的要不自然一点,但无疑更加简洁。。。当然,这题也可以用康拖展开。


完整代码如下:

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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值