• <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>

            學習心得(code)

            superlong@CoreCoder

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              74 Posts :: 0 Stories :: 5 Comments :: 0 Trackbacks

            公告

            文字可能放在http://blog.csdn.net/superlong100,此處存放代碼

            常用鏈接

            留言簿(4)

            我參與的團隊

            搜索

            •  

            最新隨筆

            最新評論

            • 1.?re: Poj 1279
            • 對于一個凹多邊形用叉積計算面積 后能根據結果的正負來判斷給的點集的時針方向?
            • --bsshanghai
            • 2.?re: Poj 3691
            • 你寫的這個get_fail() 好像并是真正的get_fail,也是說fail指向的串并不是當前結點的子串。為什么要這樣弄呢?
            • --acmer1183
            • 3.?re: HDU2295[未登錄]
            • 這個是IDA* 也就是迭代加深@ylfdrib
            • --superlong
            • 4.?re: HDU2295
            • 評論內容較長,點擊標題查看
            • --ylfdrib
            • 5.?re: HOJ 11482
            • 呵呵..把代碼發在這里很不錯..以后我也試試...百度的編輯器太爛了....
            • --csuft1

            閱讀排行榜

            評論排行榜

            #include <iostream>
            #include 
            <string.h>
            #include 
            <queue>

            using namespace std;

            bool h[2][370000];
            int step;

            typedef 
            struct nod
            {
                
            char s[10];
                nod(){}
                
            void write(){
                    puts(s);
                }
            }cstring;


            queue
            < cstring > q[2];

            int sta[3][3], end[3][3];

            inline 
            void read(int st[][3])  //讀入函數 
            {
                
            for(int i = 0; i < 3; i ++)
                
            for(int j = 0; j < 3; j ++)
                    scanf(
            "%d"&st[i][j]);
            }

            inline 
            int nx(cstring str)      //求逆序數 
            {
                
            int l = strlen(str.s), i, j, num;
                num 
            = 0;
                
            for(i = 0; i < l; i ++)
                {
                    
            if(str.s[i] == '0'continue;
                    
            for(j = 0; j < i; j ++)
                    
            if(str.s[j] > str.s[i]) num ++;
                }
                
            //printf("%d\n",num);
                return num;
            }

            inline 
            int map(cstring str)        //映射 
            {
                
            int i, j, base, cnt, hash;
                
            base  = 1; hash = 0;
                
            for(i = 0; i < 9; i ++)
                {
                    
            if(str.s[i] == '0') str.s[i] = '9';
                    
            if(base == 0base = 1;
                    
            base *= i; cnt = 0;
                    
            for(j = 0; j < i; j ++)
                    
            if(str.s[j] > str.s[i]) cnt ++;
                    hash 
            += base * cnt;
                }
                
            return hash;
            }

            inline 
            void swap(char &a, char &b)   //交換函數 
            {
                
            char c = a; a = b; b = c;
            }

            void change(cstring &tp, int dic) //4種擴展 
            {
                
            int i, j, l = strlen(tp.s);
                
            char tmp;
                
            //up right down left

                
            for(i = 0; i < l; i ++)
                
            if(tp.s[i] == '0'break;
                
            if(dic == 0 && i > 2
                    swap(tp.s[i], tp.s[i 
            - 3]);
                
            else if(dic == 1 && i % 3 != 2
                    swap(tp.s[i], tp.s[i 
            + 1]);
                
            else if(dic == 2 && i < 6)
                    swap(tp.s[i], tp.s[i 
            + 3]);
                
            else if(dic == 3 && i % 3 != 0)
                    swap(tp.s[i], tp.s[i 
            - 1]);
                
            else strcpy(tp.s, " ");

            }

            int extend(int index, cstring st) //擴展函數 
            {
                cstring tp;
                
            int i, j, l = strlen(st.s), hash;  
                
            for(i = 0; i < 4; i ++)
                {
                    strcpy(tp.s ,st.s);
                    change(tp, i);
                    
            if(strcmp(tp.s," "))
                    {
                        
            //tp.write();
                        hash = map(tp);
                        
            if(h[index][hash]) continue;
                        
            if(h[1 - index][hash]) return true;
                        q[index].push(tp);
                        h[index][hash] 
            = 1;
                    }
                }
                
            return false;



            void atos(int arr[3][3], cstring &tp_atos) //把數組變成cstring 
            {
                
            int i, j;
                
            for(i = 0; i < 3; i ++)
                
            for(j = 0; j < 3; j ++)
                    tp_atos.s[i 
            * 3 + j] = arr[i][j] + '0';
                tp_atos.s[
            9= '\0';
            }

            int bfs()
            {
                cstring 
            as, ed;
                
            while(!q[0].empty()) q[0].pop();
                
            while(!q[1].empty()) q[1].pop();
                memset(h,
            0,sizeof(h));
                atos(sta, 
            as);
                atos(end, ed);
                
            if(nx(as% 2 != nx(ed) % 2return -1;//逆序奇偶性不同 
                q[0].push( as );
                q[
            1].push( ed );
                h[
            0][map( as )] = 1;
                h[
            1][map( ed )] = 1;
                
            if(h[0][map( ed )]) return 0;              //兩個相同 
                int index, size;
                step 
            = 0;
                cstring temp;
                
            while!q[0].empty() || !q[1].empty() )
                {
                    index 
            = 0;
                    
            if( q[0].size() > q[1].size() ) index = 1//取size小的先擴展 
                    if( q[index].empty() ) index = 1 - index;
                    size 
            = q[index].size();
                    
            while(size --)
                    {
                        temp 
            = q[index].front();  
                        
            //temp.write();
                        q[index].pop();
                        
            if( extend(index, temp) ) return step + 1;  //擴展 
                    }
                    step 
            ++;
                }
            }

            int main()
            {
                
            int ans = 0;
                read(sta);    
                read(end);
                ans 
            = bfs();
                printf(
            "%d\n",ans);
            }

            posted on 2009-08-22 20:27 superlong 閱讀(139) 評論(0)  編輯 收藏 引用
            亚洲中文字幕无码久久2020 | 久久亚洲精品人成综合网| 国产精品99久久久久久www| 青青草原精品99久久精品66| 久久精品国产一区二区| 99久久成人国产精品免费| 无码人妻少妇久久中文字幕蜜桃| 久久天天躁狠狠躁夜夜av浪潮| 国产精品久久久久久吹潮| 伊人久久大香线蕉综合影院首页| 一本一道久久a久久精品综合| 四虎国产精品免费久久久| 亚洲国产精品无码久久久不卡| 久久久久久久综合狠狠综合| 亚洲国产成人久久精品99 | 国产欧美一区二区久久| 久久久久无码精品国产不卡| 波多野结衣AV无码久久一区| 亚洲国产成人久久综合碰| 一本大道加勒比久久综合| 久久这里只精品国产99热| 热久久这里只有精品| 久久综合狠狠综合久久激情 | 91精品免费久久久久久久久| 久久99精品久久久久久9蜜桃| 久久久久人妻精品一区三寸蜜桃| 久久99精品久久久久久水蜜桃| 欧美性大战久久久久久| 要久久爱在线免费观看| 久久久久久精品免费免费自慰| 精品国产乱码久久久久久呢| 久久国产乱子伦免费精品| 色综合久久精品中文字幕首页 | 久久久久99精品成人片 | 久久久久久噜噜精品免费直播 | 欧美熟妇另类久久久久久不卡| 99久久99久久久精品齐齐 | 久久精品这里只有精99品| 久久频这里精品99香蕉久| 国产V亚洲V天堂无码久久久| 国产成人香蕉久久久久|