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

            a tutorial on computer science

              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              21 隨筆 :: 0 文章 :: 17 評(píng)論 :: 0 Trackbacks
               據(jù)說不作此題人生不完整。好吧。很久以前就做過了,寫過BFS,A*,和雙搜。A*用了200+ms,汗,BFS都比他快。正好這幾天在看搜索估價(jià)函數(shù)之類的東西,就把這道經(jīng)典題拿出來,再做一遍,突然發(fā)現(xiàn),估價(jià)函數(shù)+迭代加深搜索就是IDA*算法,好吧。以前傻傻看黑書的時(shí)候,理解不了A* ,覺得巨麻煩(現(xiàn)在也覺得挺麻煩),現(xiàn)在寫起來IDA*,覺得還挺簡(jiǎn)潔,并且比較通用,而且這玩意又好寫又比較通用,就詳細(xì)研究了一下。看了別人的一個(gè)IDA*的算法,覺得寫的很簡(jiǎn)潔很工整,就參詳了一下,然后改造成了自己的,A掉了1077題。樓教主寫的那個(gè)百度之星的版本的Allyes.com,還沒有詳細(xì)看,覺得有點(diǎn)復(fù)雜。有機(jī)會(huì)要好好研究下。
               我感覺IDA*的主要的價(jià)值在于,在深度搜索的時(shí)候,可以把問題的條件放寬,只要找到一個(gè)接近并且小于實(shí)際解的估價(jià)函數(shù),就可以非常快的得到解了。而尋求估價(jià)函數(shù)的過程,非常的有意思,可以轉(zhuǎn)換成各種經(jīng)典問題。下面是代碼,我覺得還算工整吧。
            #include <cstdio>
            #include <cstring>


            char board[3][3];

            int dest_goal[9][2] = {{0,0},{0,1},{0,2},{1,0},{1,1},{1,2},{2,0},{2,1},{2,2}}; 

            int val[4][2] = {{0,1},{1,0},{-1,0},{0,-1}};  //r,l,d,u

            char op[4] ={'r','d','u','l'};

            char solution[100];

            int find;

            int testdata,low_bound;

            int abs(int i)
            {
              if(i>0) return i; return -i;
            }

            void swap(char& a,char& b)
            {
              char c = a; a= b;b = c;
            }

            int getH()
            {
               int i,j;
               int nRet = 0;
               for(i=0;i<3;i++)
                for(j=0;j<3;j++)
                {
                  if(board[i][j] != 'x')
                    nRet += abs(i - dest_goal[board[i][j] - '1'][0]) + abs(j - dest_goal[board[i][j] - '1'][1] );
                }
               return nRet;
            }

            int bound(int x,int y)
            {
              if(x<0||y<0||x>=3||y>=3)  return 0;
              return 1;
            }

            int min(int a,int b)
            {
                if(a>b) return b; return a; 
            }

            int dfs(int x,int y,int step,int maxstep)
            {
               if(getH() == 0 || find == 1)
               {
                 find = 1;
                 return step;
               }

               if(step + getH() > maxstep)
                 return step + getH();
              
               int i;
               int now_bound = 100000;
               for(i=0;i<4;i++)
               {
                 int nx = x + val[i][0];
                 int ny = y + val[i][1];
                 if(bound(nx,ny))
                 {
                   swap(board[x][y],board[nx][ny]);
                   solution[step] = op[i]; 
                   int next_bound = dfs(nx,ny,step+1,maxstep);
                   now_bound = min(now_bound,next_bound);
                   if(find == 1)
                     return now_bound;
                   swap(board[x][y],board[nx][ny]);
                 }
               }
               return now_bound;
            }
            int main()
            {
               freopen("in_1077.txt","r",stdin);
               freopen("out.txt","w",stdout);
               int i,j,sx,sy;
               char c;
               for(i=0;i<3;i++)
                for(j=0;j<3;j++)
                {
                   while(scanf("%c",&board[i][j]) && board[i][j] == ' ')
                     NULL;
                   if(board[i][j] == 'x')
                   {
                    sx = i;
                    sy = j;
                   }
                }
               
               find = 0;
               low_bound = getH();
               while(low_bound < 100 && !find) //如果啟發(fā)函數(shù)比較好(很靠近結(jié)果并且小于結(jié)果),基本上很少次迭代就可以出結(jié)果。時(shí)間浪費(fèi)在迭代次數(shù)上 
               {
                 low_bound = dfs(sx,sy,0,low_bound);
               }
               
               if(find)
                 {
                   solution[low_bound] = '\0';
                   printf("%s\n",solution);
                 }
               else if(find == 0)
                 printf("unsolvable\n");
               return 0;
            }
            posted on 2012-04-07 22:57 bigrabbit 閱讀(3220) 評(píng)論(1)  編輯 收藏 引用

            評(píng)論

            # re: IDA*算法-POJ1077 2012-04-10 09:16 tb
            學(xué)習(xí)算法了   回復(fù)  更多評(píng)論
              

            # re: IDA*算法-POJ1077[未登錄] 2013-09-07 12:35 Peter
            搔年,copy也是要?jiǎng)幽X子的,比如說這一句

            while(low_bound < 100 && !find) //如果啟發(fā)函


            為什么你的最大上界要設(shè)置成 low_bound < 100 呢??  回復(fù)  更多評(píng)論
              


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久99精品国产麻豆蜜芽| 国产精品亚洲美女久久久| 性欧美丰满熟妇XXXX性久久久 | 波多野结衣久久精品| 亚洲午夜无码久久久久小说| 综合网日日天干夜夜久久| www久久久天天com| 久久夜色撩人精品国产| www久久久天天com| 成人综合久久精品色婷婷| 女人香蕉久久**毛片精品| 亚洲国产成人久久一区WWW| 97久久久精品综合88久久| 亚洲国产精品综合久久网络| 国产精品99久久久久久人| 天堂无码久久综合东京热| 久久成人影院精品777| 亚洲精品乱码久久久久久久久久久久| 一本大道加勒比久久综合| 亚洲精品无码久久久久去q| 日本加勒比久久精品| 国产91久久精品一区二区| 亚洲精品乱码久久久久66| 日批日出水久久亚洲精品tv| 亚洲国产成人久久精品动漫| 国产精品一区二区久久| 国产精品免费看久久久| 色偷偷88888欧美精品久久久| 伊人久久大香线蕉精品不卡| 国产91久久综合| 国产精品99久久久久久猫咪| 国产午夜免费高清久久影院| 精品久久久久久中文字幕人妻最新| 久久精品国产亚洲AV忘忧草18| 久久久久亚洲AV成人网| 91久久精品视频| 久久这里有精品视频| 国产精品免费久久久久久久久| 日韩一区二区久久久久久| 久久国产乱子伦精品免费强| 66精品综合久久久久久久|