• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            The Fourth Dimension Space

            枯葉北風(fēng)寒,忽然年以殘,念往昔,語(yǔ)默心酸。二十光陰無(wú)一物,韶光賤,寐難安; 不畏形影單,道途阻且慢,哪曲折,如渡飛湍。斬浪劈波酬壯志,同把酒,共言歡! -如夢(mèng)令

            POJ 1077 八數(shù)碼問(wèn)題

            瞻仰下八數(shù)碼,可惜效率還不行啊,看到那么多0MS的,打擊啊。。。這題如果要0MS,必須是A*吧,呵呵 可惜還不會(huì)呀。。。

            #include<iostream>
            #include
            <algorithm>
            #include
            <cstring>
            using namespace std;

            struct node
            {
                
            int dir;
                
            int pre;
                
            int step;
                
            char x[10];
            }
            l[400000];


            char x[15];
            char ansx[15]="123456780";
            int perm[] = {1,1,2,6,24,120,720,5040,40320};//n! 
            int d[] = {-1,-313};//四個(gè)方向的下標(biāo)變換,左上右下。
            bool move[][4= {0,0,1,11,0,1,11,0,0,10,1,1,11,1,1,11,1,0,10,1,1,01,1,1,01,1,0,0}
            //各個(gè)位置的可行變換
            int v[362881];//數(shù)組判重
            int hash()//用逆序數(shù)和變進(jìn)制進(jìn)行hash 

                
            int h = 0
                
            for(int i = 1;i<9;i++)
                

                    
            int count = 0
                    
            for(int j=0;j<i;j++
                        
            if(x[j] > x[i])count ++
                    h 
            += count * perm[i]; 
                }
             
                
            return h; 
            }
             

            void input()
            {

                
            char t[5];
                
            for(int i=0;i<9;i++)
                
            {
                    scanf(
            "%s",t);
                    x[i]
            =t[0];
                    
            if(x[i]=='x')
                        x[i]
            ='0';
                }

            }



            int pos;
            void search(char x[])
            {
                
            for(int i=0;i<9;i++)
                    
            if(x[i]=='0')
                    
            {
                        pos
            =i;
                        
            break;
                    }


            }

            char ans[4000000]={0};
            void GetDir(int h)
            {
                memset(ans,
            0,sizeof(ans));
                
            int i;
                
            int n=l[h].step;
                
            for(i=n;i>=1;i--)
                
            {
                    
                    
            if(l[h].dir==0)
                        ans[i]
            ='l';
                    
            else if(l[h].dir==1)
                        ans[i]
            ='u';
                    
            else if(l[h].dir==2)
                        ans[i]
            ='r';
                    
            else if(l[h].dir==3)
                        ans[i]
            ='d';
                    h
            =l[h].pre;
                }

            }




            int main()
            {
                

                
            int head,tail;
                memset(v,
            0,sizeof(int)*362881);
                head
            =tail=1;
                input();
                
            //
                int code=hash();
                v[code]
            =1;
                l[head].step
            =0;
                l[head].pre
            =-1;
                strcpy(l[head].x,x);
                
            //initial

                
            while(head<=tail)
                
            {
                    
            if(strcmp(l[head].x,ansx)==0)
                        
            break;//此時(shí)head為所求解
                    search(l[head].x);
                    
            for(int i=0;i<4;i++)
                    
            {
                        
            if(move[pos][i]==0)
                            
            continue;
                        strcpy(x,l[head].x);
                        
            int np=pos+d[i];
                        swap(x[pos],x[np]);
                        
            int code=hash();
                        
            if(!v[code])
                        
            {
                            
                            tail
            ++;
                            v[code]
            =1;
                            l[tail].step
            =l[head].step+1;
                            l[tail].pre
            =head;
                            l[tail].dir
            =i;
                            strcpy(l[tail].x,x);
                        }


                    }

                    head
            ++;
                }

                
            if(head>tail)
                    printf(
            "unsolvable\n");
                
            else
                
            {
                    GetDir(head);
                    printf(
            "%s\n",ans+1);
                }



                
            return 0;
            }



            另外那個(gè)十五數(shù)碼,能用程序搞出來(lái)么?

            這里的哈希函數(shù)是用能對(duì)許多全排列問(wèn)題適用的方法。取n!為基數(shù),狀態(tài)第n位的逆序值為哈希值第n位數(shù)。對(duì)于空格,取其(9-位置)再乘以8!。例如,1 3 7 2 4 6 8 5 8 的哈希值等于:
            0*0! + 0*1! + 0*2! + 2*3! + 1*4! + 1*5! + 0*6! + 3*7! + (9-8)*8! = 55596 <9!
             具體的原因可以去查查一些數(shù)學(xué)書(shū),其中1 2 3 4 5 6 7 8 9 的哈希值是0 最小,8 7 6 5 4 3 2 1 0 的哈希值是(9!-1)最大,而其他值都在0 到 (9!-1) 中,且均唯一。

            posted on 2010-05-03 19:26 abilitytao 閱讀(1805) 評(píng)論(6)  編輯 收藏 引用

            評(píng)論

            # re: POJ 1077 八數(shù)碼問(wèn)題 2010-05-03 21:37 abilitytao

            @TimTopCoder
            thx, maybe I have to learn the A* algorithm for a deep research..  回復(fù)  更多評(píng)論   

            # re: POJ 1077 八數(shù)碼問(wèn)題 2010-05-03 22:08 aaa

            八數(shù)碼問(wèn)題這樣搞,
            這個(gè)算法也太傻了吧。  回復(fù)  更多評(píng)論   

            # re: POJ 1077 八數(shù)碼問(wèn)題 2010-05-03 23:46 abilitytao

            @aaa
            非常SB!呵呵  回復(fù)  更多評(píng)論   

            # re: POJ 1077 八數(shù)碼問(wèn)題 2010-05-04 11:32 麗可酷

            八數(shù)碼問(wèn)題這樣搞  回復(fù)  更多評(píng)論   

            # re: POJ 1077 八數(shù)碼問(wèn)題 2010-07-28 14:17 knight4hy

            看了半天發(fā)現(xiàn)是非常簡(jiǎn)單的廣搜,可我是搜A*進(jìn)來(lái)的  回復(fù)  更多評(píng)論   

            # re: POJ 1077 八數(shù)碼問(wèn)題[未登錄](méi) 2010-07-28 22:23 abilitytao

            @knight4hy
            那你可以繼續(xù)搜 直到搜到我寫(xiě)A*的那篇。。。  回復(fù)  更多評(píng)論   


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            亚洲国产精品综合久久网络| 国产福利电影一区二区三区久久老子无码午夜伦不 | 久久电影网一区| 国产精品久久亚洲不卡动漫| 99久久精品国产麻豆| 青青青青久久精品国产h| 久久久久久精品无码人妻| 性欧美丰满熟妇XXXX性久久久| 91久久精品视频| 精品综合久久久久久98| 亚洲精品美女久久久久99| 久久伊人精品青青草原高清| 日批日出水久久亚洲精品tv| 久久这里只有精品首页| 久久久久国产亚洲AV麻豆| 99re这里只有精品热久久| 久久精品国产亚洲av瑜伽| 97久久久久人妻精品专区 | 国产精品久久网| 欧美久久久久久| 国产精品熟女福利久久AV| 国内精品伊人久久久影院| 人人狠狠综合久久亚洲高清| 久久水蜜桃亚洲av无码精品麻豆 | 青青热久久国产久精品| 97久久精品无码一区二区天美| 久久久黄色大片| 欧洲国产伦久久久久久久| 91久久精品电影| 中文字幕亚洲综合久久2| 久久99国产综合精品女同| 久久久久久国产精品免费无码 | 国产69精品久久久久777| 亚洲国产精品无码久久青草| 国产精品内射久久久久欢欢| 88久久精品无码一区二区毛片 | 精品久久人人爽天天玩人人妻| 日本高清无卡码一区二区久久| 国产精品成人99久久久久91gav| 久久99精品久久久久久hb无码 | 伊人久久久AV老熟妇色|