• <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 閱讀(141) 評論(0)  編輯 收藏 引用
            久久99精品久久久久久hb无码| 青青热久久国产久精品| 日本人妻丰满熟妇久久久久久| 浪潮AV色综合久久天堂| 91亚洲国产成人久久精品网址 | 久久久精品人妻无码专区不卡| 国产午夜精品理论片久久| 99久久做夜夜爱天天做精品| 久久亚洲欧美国产精品| 久久青青草原亚洲av无码| 久久国产精品99国产精| 蜜臀久久99精品久久久久久| 奇米影视7777久久精品| 久久91这里精品国产2020| 久久国产精品77777| 久久伊人五月丁香狠狠色| 青青青国产精品国产精品久久久久 | 久久av高潮av无码av喷吹| 亚洲综合伊人久久大杳蕉| 国产精品免费久久久久久久久 | 国产美女久久久| 99久久精品国产一区二区| 久久综合伊人77777| 久久精品国产精品亚洲精品| 婷婷伊人久久大香线蕉AV| 亚洲v国产v天堂a无码久久| 免费观看成人久久网免费观看| 日本人妻丰满熟妇久久久久久| 99久久香蕉国产线看观香| 青青青青久久精品国产h久久精品五福影院1421 | 亚洲精品无码久久久久| 人妻少妇精品久久| 理论片午午伦夜理片久久 | 久久精品无码午夜福利理论片| 精品久久久一二三区| 一本色综合久久| 国产精品久久久久a影院| 亚洲国产成人久久综合区| 伊人久久国产免费观看视频| 狠狠色丁香婷婷久久综合五月| 久久无码精品一区二区三区|