• <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)  編輯 收藏 引用
            热re99久久6国产精品免费| 精品一区二区久久| 久久久婷婷五月亚洲97号色| 久久精品欧美日韩精品| 久久久久久久综合综合狠狠| 亚洲国产成人久久笫一页| 亚洲国产视频久久| 99久久伊人精品综合观看| 亚洲色欲久久久综合网东京热| 99久久精品无码一区二区毛片 | 久久亚洲私人国产精品| 狠狠精品久久久无码中文字幕| 伊人久久大香线焦AV综合影院| 日本久久久久久中文字幕| 亚洲va久久久噜噜噜久久狠狠| 午夜福利91久久福利| 久久久久久国产精品免费免费 | 久久这里只有精品首页| 99久久99这里只有免费的精品| 亚洲日本va中文字幕久久| 久久亚洲AV成人无码电影| 97久久精品人妻人人搡人人玩| 一本久久a久久精品vr综合| 亚洲AV无一区二区三区久久 | 内射无码专区久久亚洲| 亚洲欧美成人久久综合中文网 | 亚洲精品无码久久不卡| 精品国产乱码久久久久久呢| 久久这里只精品99re66| 国产精品久久久久AV福利动漫| 91精品国产91久久久久久| 亚洲国产精品成人久久蜜臀| 久久久久久久波多野结衣高潮| 日本久久久精品中文字幕| 丁香色欲久久久久久综合网| 亚洲人成网亚洲欧洲无码久久| 99久久婷婷免费国产综合精品| 久久笫一福利免费导航 | 久久嫩草影院免费看夜色| 久久精品国产精品亚洲毛片| 精品国产青草久久久久福利|