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

            poj 1077 Eight 【經(jīng)典八數(shù)碼問(wèn)題】


            Accepted 3164K 219MS G++ 3644B



            經(jīng)典題感覺(jué)就是不一樣。

            總是花了很長(zhǎng)時(shí)間浪費(fèi)在一些無(wú)關(guān)緊要的地方,把方向理解錯(cuò)了,正好相反的,調(diào)了N久。

            后來(lái)輸出的時(shí)候,都往反方向輸出A了。

            用康托展開(kāi)做哈希函數(shù),直接BFS就可以了

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

            int fac[10]={1,1,2,6,24,120,720,5040,40320,362880};
            void swap(int &a, int &b)
            {
                 int t=a; a=b; b=t;
            }
            int cantor(int x)
            {
                int a[10]={0};
                for(int i=9; i>=1; i--)
                {
                        a[i]=x%10;
                        x/=10;
                }
                int ans=0;
                for(int i=1; i<=9; i++)
                {
                        int c=0;
                        for(int j=i+1; j<=9; j++)
                                if(a[j]<a[i])c++;
                        ans+=c*fac[a[i]-1];      
                }
                return ans;
            }

            struct type
            {
                   int last;
                   int now;
                   int move;
            }Q[362885];

            bool used[362885]={0};

            bool change(int &x, int ch)//方向理解反了,調(diào)r時(shí)其實(shí)是調(diào)l
            {
                 int status[4][4],temp=x;
                 int row,column;
                 for(int i=3; i>=1; i--)
                         for(int j=3; j>=1; j--)
                         {
                                 status[i][j]=temp%10;
                                 temp/=10;
                                 if(status[i][j]==9)
                                 {  row=i; column=j; }            
                         }
                 switch(ch)
                 {
                           case 1: if(column==1)return false;  //r
                                     swap(status[row][column], status[row][column-1]); break;
                           case 2: if(column==3)return false;  //l
                                     swap(status[row][column], status[row][column+1]);break;
                           case 3: if(row==3)return false;     //u
                                     swap(status[row][column], status[row+1][column]); break;
                           case 4: if(row==1) return false;    //d
                                     swap(status[row][column], status[row-1][column]); break;
                 }
                 temp=0;
                 for(int i=1; i<=3; i++)
                 for(int j=1; j<=3; j++)
                         temp=temp*10+status[i][j];
                 x=temp;
                 return true;
            }

            int main()
            {
                int now=0;
                int goal=0;
                char t;
                for(int i=1; i<=9; i++)
                {
                        cin>>t;
                        if(t=='x')t='9';
                        now=now*10+int(t-'0');
                }       
                int l=1,r=1;
                Q[1].last=0; Q[1].now=now;
                int x=cantor(now);
                used[x]=1;
                bool find=false;
                while(l<=r&&find==false&&r<=362881)
                {
                      int num=Q[l].now, can;
                      int temp;
                      //system("pause");
                      for(int i=1; i<=4; i++)
                      {
                              temp=num;
                              if(change(temp,i))
                              {
                                               can=cantor(temp);
                                               if(!used[can])
                                               {
                                                             used[can]=true;     
                                                             r++;
                                                             Q[r].now=temp;
                                                             Q[r].last=l;
                                                             Q[r].move=i;
                                               }
                                               if(can==goal){find=true; break; }
                              }
                      }
                     l++;
                }         
               
                if(find==false)cout<<"unsolvable"<<endl;
                else
                {
                     int ans[1000];  int len=0;
                    
                     for( ;r!=1; )
                     {
                         
                          len++;
                          ans[len]=Q[r].move;
                          r=Q[r].last;
                     }
                     for(int i=len; i>=1; i--)
                     {
                             switch(ans[i])
                             {
                                             case 2:cout<<'r';break;//調(diào)一下輸出順序A了
                                             case 1:cout<<'l';break;
                                             case 4:cout<<'u';break;
                                             case 3:cout<<'d';break;
                             }
                     }
                 cout<<endl;   
                }
               
                system("pause");
                return 0;
            }

            posted on 2010-08-20 11:04 田兵 閱讀(503) 評(píng)論(0)  編輯 收藏 引用 所屬分類: POJ

            <2010年8月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(2)

            隨筆分類(65)

            隨筆檔案(65)

            文章檔案(2)

            ACM

            搜索

            積分與排名

            最新隨筆

            最新評(píng)論

            閱讀排行榜

            久久人人青草97香蕉| 久久精品国产第一区二区三区 | 国产伊人久久| 国产91久久综合| 一本一本久久a久久综合精品蜜桃| 久久久久久久女国产乱让韩| 72种姿势欧美久久久久大黄蕉| 91精品国产综合久久四虎久久无码一级 | 精品久久久久久国产潘金莲| 久久精品国产亚洲5555| 亚洲AV日韩精品久久久久久久| 久久综合综合久久97色| 亚洲国产日韩欧美综合久久| 午夜不卡久久精品无码免费| 久久免费视频一区| 久久精品国产福利国产秒| 国产成人精品综合久久久久| 国产—久久香蕉国产线看观看| 欧美亚洲色综久久精品国产| 青春久久| 国产高清美女一级a毛片久久w| 无码人妻精品一区二区三区久久| 久久久久久噜噜精品免费直播| 人人狠狠综合久久88成人| 色综合久久夜色精品国产| 久久精品免费大片国产大片| 国产精品美女久久久久网| 亚洲欧美成人综合久久久 | 久久久久噜噜噜亚洲熟女综合| 久久精品国产精品亚洲毛片| 久久免费的精品国产V∧| 国内精品久久久久久久久电影网| 欧美亚洲另类久久综合婷婷| 青青草国产精品久久| 久久久九九有精品国产| 91精品婷婷国产综合久久| 久久精品国产精品青草app| 久久人人爽爽爽人久久久| 久久国产高潮流白浆免费观看| 99精品国产综合久久久久五月天| 久久亚洲sm情趣捆绑调教|