• <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>
              C++博客 :: 首頁 :: 新隨筆 ::  ::  :: 管理

            pku1077

            Posted on 2010-08-21 19:47 Kevin_Zhang 閱讀(284) 評論(0)  編輯 收藏 引用 所屬分類: 搜索

            轉載自
            http://hi.baidu.com/happynp/blog/item/06820fcaf41d4683c817681c.html

            Tag:BFS+Hashtable

            #include <iostream>
            using namespace std;

            const unsigned int M = 490001
            #define NIL -1
            #define SWAP(x,y) (x)^=(y)^=(x)^=(y)

            int dir[4][2= {
                  
            10// D
                 -10// U
                  0,-1// L
                  01 // R 
            }
            ;

            int revDir[4= 1032 };
            char dirStr[] = {"dulr"};
            char solution[M];

            typedef 
            struct Node{
                  
            int v; // 狀態原值 
                  int r; // 到下一狀態值的變化方向
            }
            Node;

            Node Hash_s[M], Hash_e[M];
            Node Q_s[M], Q_e[M];
            int Qs_h, Qs_t, Qe_h, Qe_t;
            Node ns, ne;
            int cnt[100];

            inline 
            int hashcode(int v)
            {
                  
            return v % M;
            }


            bool Insert(Node hash[], const Node &nod)
            {
                  
            int key = hashcode(nod.v);
                  
            if( hash[key].v == 0 ){
                        hash[key] 
            = nod;
                        
            return true;
                  }

                  
            else{
                        
            if(hash[key].v == nod.v)
                              
            return false;
                        
            while( hash[ hashcode(++key) ].v ) ;
                        hash[ hashcode(key)] 
            = nod;
                        
            return true;
                  }

            }


            Node 
            * Query(Node hash[], const Node &nod)
            // false / true : Not / Yes exist in the hash table
            // if Yes, theNod return the existed Node
            {
                  
            int key = hashcode(nod.v);
                  
            if( hash[key].v == 0 ){
                        
            return NULL;
                  }

                  
            else{
                        
            if(hash[key].v == nod.v){
                              
            return &hash[key];
                        }

                        
            while( hash[ hashcode(++key) ].v ) {
                              
            if( hash[ hashcode(key)].v == nod.v ){
                                    
            return &hash[key];
                              }

                        }

                        
            return NULL;
                  }

            }


            int ToArry(int v, int (*p)[3])

                  
            int i, pos;
                  
            for(i=8; i>=0; i--{
                        p[ i 
            / 3 ][ i % 3 ] = v % 10;
                        v 
            /= 10;
                        
            if( p[ i / 3 ][ i % 3 ] == 0 )
                              pos 
            = i;
                  }
             
                  
            return pos; // 'x' 的位置
            }


            int ToValue(int (*p)[3])
            {
                  
            int v = 0;
                  
            for(int i=0; i<=8; i++{
                        v 
            = v * 10 + p[ i / 3 ][ i % 3 ];
                  }
             
                  
            return v;
            }


            void GetOptimPath(int arr[], Node hash[], Node curNod, bool starToEnd, int &nIndex)
            {
                  Node 
            *pNod; 
                  
            int curArr[3][3];
                  
            for( ; curNod.r != NIL; )
                  
            {
                        arr[nIndex
            ++= starToEnd ? curNod.r : revDir[curNod.r];
                        
            const int loca = ToArry(curNod.v, curArr); 
                        
            int row = loca / 3;
                        
            int col = loca % 3;
                        
            int nxtrow = row + dir[revDir[curNod.r]][0];
                        
            int nxtcol = col + dir[revDir[curNod.r]][1];
                        SWAP(curArr[row][col], curArr[nxtrow][nxtcol]);
                        
            int nxtv = ToValue(curArr);
                        curNod.v 
            = nxtv;
                        pNod 
            = Query(hash, curNod);
                        curNod.r 
            = pNod->r; 
                  }

            }


            void bfs()
            {
                  Qs_h 
            = Qs_t = 0;
                  Qe_h 
            = Qe_t = 0;
                  Q_s[Qs_t
            ++= ns;
                  Q_e[Qe_t
            ++= ne;
                  Insert(Hash_s, ns);
                  Insert(Hash_e, ne);
                  
                  
            int nIndex = 0;
                  
            int now;
                  Node curNod, nxtNod;
                  
            int curArr[3][3], nxtArr[3][3];
                  
            while(1){
                        
            if( ( Qs_h == Qs_t || Qe_h == Qe_t ) )
                              
            break;
                        now 
            = Qs_t;
                        
            while( Qs_h < now ){
                              curNod 
            = Q_s[Qs_h++];
                              
            const int loca = ToArry(curNod.v, curArr);
                              
            int row = loca / 3;
                              
            int col = loca % 3;
                              
            for(int i=0; i<4; i++){
                                    memcpy(nxtArr, curArr, 
            sizeof(curArr));
                                    
            int nxtrow = row + dir[i][0];
                                    
            int nxtcol = col + dir[i][1];
                                    
            if!( nxtrow >= 0 && nxtrow < 3 && nxtcol >= 0 && nxtcol < 3 ) ) continue;
                                    SWAP(nxtArr[row][col], nxtArr[nxtrow][nxtcol]);
                                    
            int nxtv = ToValue(nxtArr);
                                    nxtNod.v 
            = nxtv;
                                    nxtNod.r 
            = i;
                                    
            if( Query( Hash_e, nxtNod) != NULL )// find the solution
                                          Node commNod(nxtNod); 
                                          GetOptimPath(cnt, Hash_s, commNod, 
            true, nIndex);
                                          
            forint i=0; i < nIndex / 2; i++ )
                                                SWAP(cnt[i], cnt[nIndex
            -1-i]);
                                          commNod 
            = *Query(Hash_e, nxtNod); 
                                          GetOptimPath(cnt, Hash_e, commNod, 
            false, nIndex);
                                          
            goto Solved;
                                    }

                                    
            if( Insert( Hash_s, nxtNod) ){
                                          Q_s[Qs_t
            ++= nxtNod;
                                    }

                              }

                        }

                        now 
            = Qe_t;
                        
            while( Qe_h < now ){
                              curNod 
            = Q_e[Qe_h++];
                              
            const int loca = ToArry(curNod.v, curArr); 
                              
            int row = loca / 3;
                              
            int col = loca % 3;
                              
            for(int i=0; i<4; i++){
                                    memcpy(nxtArr, curArr, 
            sizeof(curArr));
                                    
            int nxtrow = row + dir[i][0];
                                    
            int nxtcol = col + dir[i][1];
                                    
            if!( nxtrow >= 0 && nxtrow < 3 && nxtcol >= 0 && nxtcol < 3 ) ) continue;
                                    SWAP(nxtArr[row][col], nxtArr[nxtrow][nxtcol]);
                                    
            int nxtv = ToValue(nxtArr);
                                    nxtNod.v 
            = nxtv;
                                    nxtNod.r 
            = i;
                                    
            if( Query( Hash_s, nxtNod) != NULL )
                                          Node commNod(nxtNod); 
                                          GetOptimPath(cnt, Hash_e, commNod, 
            false, nIndex);
                                          
            forint i = 0; i < nIndex / 2; i++ )
                                                SWAP(cnt[i], cnt[nIndex
            -1-i]);
                                          commNod 
            = *Query(Hash_s, nxtNod); 
                                          GetOptimPath(cnt, Hash_s, commNod, 
            true, nIndex);
                                          
            for( i = 0; i < nIndex / 2; i++ )
                                                SWAP(cnt[i], cnt[nIndex
            -1-i]);
                                          
            goto Solved; 
                                    }

                                    
            if( Insert( Hash_e, nxtNod) ){
                                          Q_e[Qe_t
            ++= nxtNod;
                                    }

                              }

                        }

                  }

            Solved: 
                  
            int i=0;
                  
            for(i=0; i<nIndex; i++{
                        solution[i] 
            = dirStr[cnt[i]];
                  }

                  solution[i] 
            = '\0';
                  
            return
            }


            void Input()
            {
                  
            char ch;
                  
            int i = 0, arr[3][3= 1,2,3,4,5,6,7,8,0 };
                  ne.v 
            = ToValue(arr);
                  ne.r 
            = NIL;
                  
            for(i=0; i < 9; i++){
                        
            do{
                              scanf(
            "%c"&ch); 
                        }

                        
            while!( ( ch >= '1' && ch <= '8' ) || ( ch == 'x' ) ) ) ; 
                        
            if( ch == 'x' ) 
                              arr[ i 
            / 3 ][ i % 3 ] = 0;
                        
            else 
                              arr[ i 
            / 3 ][ i % 3 ] = ch - '0';
                  }

                  ns.v 
            = ToValue(arr);
                  ns.r 
            = NIL;
            }


            int main()
            {
                  Input();
                  bfs();
                  printf(
            "%s\n", solution);
                  
            return 0;
            }
              
            久久综合九色综合欧美就去吻| 少妇久久久久久被弄高潮| 久久久久国产| 色欲av伊人久久大香线蕉影院| 久久亚洲国产欧洲精品一| 久久成人小视频| 精品久久久久久无码中文字幕一区| 狠狠精品干练久久久无码中文字幕| 久久香综合精品久久伊人| 伊人久久综在合线亚洲2019| 无码日韩人妻精品久久蜜桃| 久久中文字幕视频、最近更新| 久久国产免费观看精品3| 免费精品久久天干天干| 国产免费久久久久久无码| 久久久久久国产精品无码超碰| 日本加勒比久久精品| 91麻豆精品国产91久久久久久| 亚洲精品白浆高清久久久久久| 亚洲美日韩Av中文字幕无码久久久妻妇| 国内精品久久久人妻中文字幕| 国产香蕉久久精品综合网| 人妻丰满?V无码久久不卡| 青青草原1769久久免费播放| 久久国产色AV免费观看| 亚洲成色www久久网站夜月| 久久精品免费全国观看国产| 久久亚洲视频| 久久一本综合| 亚洲欧洲中文日韩久久AV乱码| 久久精品免费网站网| 国产亚州精品女人久久久久久 | 青青草国产精品久久久久| 婷婷伊人久久大香线蕉AV| 亚洲乱码精品久久久久..| 精品久久久久久中文字幕大豆网| 久久se精品一区精品二区国产| 精品国产综合区久久久久久| 国产精品欧美久久久久无广告| 久久精品国产99久久丝袜| 久久久青草青青国产亚洲免观|