• <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>
            posts - 20,comments - 42,trackbacks - 0

            The Rotation Game

            Time Limit: 15000MS

             

            Memory Limit: 150000K

            Total Submissions: 944

             

            Accepted: 218

            Description

            The rotation game uses a # shaped board, which can hold 24 pieces of square blocks (see Fig.1). The blocks are marked with symbols 1, 2 and 3, with exactly 8 pieces of each kind.


            Initially, the blocks are placed on the board randomly. Your task is to move the blocks so that the eight blocks placed in the center square have the same symbol marked. There is only one type of valid move, which is to rotate one of the four lines, each consisting of seven blocks. That is, six blocks in the line are moved towards the head by one block and the head block is moved to the end of the line. The eight possible moves are marked with capital letters A to H. Figure 1 illustrates two consecutive moves, move A and move C from some initial configuration.

            Input

            The input consists of no more than 30 test cases. Each test case has only one line that contains 24 numbers, which are the symbols of the blocks in the initial configuration. The rows of blocks are listed from top to bottom. For each row the blocks are listed from left to right. The numbers are separated by spaces. For example, the first test case in the sample input corresponds to the initial configuration in Fig.1. There are no blank lines between cases. There is a line containing a single `0' after the last test case that ends the input.

            Output

            For each test case, you must output two lines. The first line contains all the moves needed to reach the final configuration. Each move is a letter, ranging from `A' to `H', and there should not be any spaces between the letters in the line. If no moves are needed, output `No moves needed' instead. In the second line, you must output the symbol of the blocks in the center square after these moves. If there are several possible solutions, you must output the one that uses the least number of moves. If there is still more than one possible solution, you must output the solution that is smallest in dictionary order for the letters of the moves. There is no need to output blank lines between cases.

            Sample Input

            1 1 1 1 3 2 3 2 3 1 3 2 2 3 1 2 2 2 3 1 2 1 3 3
            1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3
            0

            Sample Output

            AC
            2
            DDHH
            2

            Source

            Shanghai 2004

             

             

            圖中的#字格的4條豎線,可以按8個(gè)方向倒轉(zhuǎn)里面的數(shù)字方塊,當(dāng)中間的8個(gè)數(shù)字方塊是同一個(gè)數(shù)字時(shí),游戲結(jié)束。這道題就要求搜索一個(gè)最短路徑,倒轉(zhuǎn)8個(gè)方向使得中間8個(gè)數(shù)字相等。這道題目看起來很像一道單純的BFS,看了POJ里面說BFS會(huì)爆內(nèi)存,我沒試,估計(jì)寫得好的也爆不了。但是估計(jì)寫BFS還要涉及到狀態(tài)的判重,程序?qū)懫饋硪膊环奖?。我用了迭代加深的搜索方法。第一次接觸這個(gè)算法,我說一下我的理解:

                   原來我很不理解迭代加深,搜索的深度一層一層地加,在后面的某一深度限制下搜索,必定會(huì)搜到前面深度所能搜到的結(jié)果,這會(huì)造成重復(fù)搜索?;谶@點(diǎn),我一直都認(rèn)為迭代加深搜索的方法很冗余,當(dāng)然也是自己從來都沒有動(dòng)手試過。像這道題就給我的收獲不少,有的時(shí)候選擇一定的策略進(jìn)行搜索,我們是很難確定解所在狀態(tài)空間的深度的,有時(shí)可能解的深度不大,但整個(gè)狀態(tài)空間的深度很大,盲目的dfs搜索在狀態(tài)空間里就有可能會(huì)越陷越深,遲遲出不了解,同時(shí)整個(gè)狀態(tài)空間的寬度也很大,用BFS可能就會(huì)爆空間。迭代加深搜索恰恰是取了一個(gè)折中。利用了dfs的優(yōu)勢,限制了搜索的深度,避免了出現(xiàn)無解的境地。由于深度是逐個(gè)增加的,當(dāng)搜到一個(gè)解后就退出,所以避免了BFS中判重的一步操作,當(dāng)然在迭代深搜的過程中,還可以加入剪枝,可以優(yōu)化程序。但是我的程序在POJ上還不快,還要找找原因,也許還有優(yōu)化。下面是一些關(guān)鍵代碼:


            for(limit=0; ;limit++)
                    
            {
                        
            if(dfs(start,0))
                        
            {
                            
            if(limit == 0)
                            
            {
                                printf(
            "No moves needed\n");
                            }

                            
            else
                            
            {
                                
            for(i=0; i<limit; i++)
                                
            {
                                    printf(
            "%c",'A'+pre[i]);
                                }

                                printf(
            "\n");
                            }

                            printf(
            "%d\n",rlt);
                            
            break;
                        }

                    }

            ///迭代過程
            int dfs(struct S now, int depth)
            {
                
            int c1,c2,c3;
                struct S st;
                test(now,c1,c2,c3);
                
            if(c1 == 8)
                
            {
                    rlt
            =1;
                    
            return 1;
                }

                
            else if(c2 == 8)
                
            {
                    rlt
            =2;
                    
            return 1;
                }

                
            else if(c3 == 8)
                
            {
                    rlt
            =3;
                    
            return 1;
                }

                
            if(depth == limit)
                    
            return 0;
                
            if(8-c1 > limit-depth && 8-c2 > limit-depth && 8-c3 > limit-depth)
                    
            return 0;
                st
            =NewStateA(now);
                pre[depth]
            =0;
                
            if(dfs(st,depth+1))
                    
            return 1;
                st
            =NewStateB(now);
                pre[depth]
            =1;
                
            if(dfs(st,depth+1))
                    
            return 1;
                st
            =NewStateC(now);
                pre[depth]
            =2;
                
            if(dfs(st,depth+1))
                    
            return 1;
                st
            =NewStateD(now);
                pre[depth]
            =3;
                
            if(dfs(st,depth+1))
                    
            return 1;
                st
            =NewStateE(now);
                pre[depth]
            =4;
                
            if(dfs(st,depth+1))
                    
            return 1;
                st
            =NewStateF(now);
                pre[depth]
            =5;
                
            if(dfs(st,depth+1))
                    
            return 1;
                st
            =NewStateG(now);
                pre[depth]
            =6;
                
            if(dfs(st,depth+1))
                    
            return 1;
                st
            =NewStateH(now);
                pre[depth]
            =7;
                
            if(dfs(st,depth+1))
                    
            return 1;
                
            return 0;
            }

            ///深搜
            posted on 2008-04-20 15:44 飛飛 閱讀(2712) 評論(1)  編輯 收藏 引用 所屬分類: ACM/ICPC

            FeedBack:
            # re: 迭代加深搜索
            2008-07-04 20:08 | wzc1989
            謝謝大牛分享?。?!  回復(fù)  更多評論
              
            2020最新久久久视精品爱| 久久人人爽人人爽人人av东京热| 久久www免费人成精品香蕉| 久久精品九九亚洲精品天堂| 亚洲国产成人久久综合碰碰动漫3d| 久久电影网一区| 久久久久九国产精品| 蜜臀av性久久久久蜜臀aⅴ麻豆| 精品综合久久久久久97超人 | 精品国产乱码久久久久久呢| 亚洲国产精品无码久久久秋霞2 | 久久亚洲国产欧洲精品一| 狠狠综合久久综合88亚洲| 国产精品欧美久久久久天天影视| 久久AV高潮AV无码AV| 日本免费一区二区久久人人澡| 一本色道久久88综合日韩精品| 久久久青草青青亚洲国产免观| 污污内射久久一区二区欧美日韩 | 久久久中文字幕| 伊人久久大香线蕉亚洲五月天| 99久久婷婷国产一区二区| 婷婷综合久久中文字幕蜜桃三电影 | 热re99久久6国产精品免费| 久久精品国产精品亜洲毛片| 久久国产欧美日韩精品| 久久久久久亚洲Av无码精品专口| 久久精品国产72国产精福利| 久久综合狠狠综合久久激情 | 久久人人添人人爽添人人片牛牛| 国产精品美女久久久网AV| 久久婷婷五月综合色奶水99啪| 久久99久国产麻精品66| 久久精品极品盛宴观看| 久久这里的只有是精品23| 久久男人AV资源网站| 国产69精品久久久久APP下载 | 久久99这里只有精品国产| 久久久久亚洲av成人无码电影| 国内精品久久久久久久久电影网| 久久综合九色综合97_久久久|