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

            ACM PKU 1915 Knight Moves 典型的寬度優先搜索 BFS

            http://acm.pku.edu.cn/JudgeOnline/problem?id=1915
            發現用vector來做寬搜的隊列,要比自己弄一個隊列來記錄方便得多,呵呵
            程序很簡單,關鍵地方我都注釋上了
            Source Code

            Problem: 
            1915  User: lnmm 
            Memory: 1560K  Time: 156MS 
            Language: C
            ++  Result: Accepted 

            Source Code 
            #include 
            <iostream>
            #include 
            <vector>
            using namespace std;
            int  mapSize,beginX,beginY,EndX,EndY;     
            int minMoves[301][301];   
            int index;
            bool find;
            struct point  
            {
                
            int x;
                
            int y;
            }
            tempPoint; 
            vector 
            <point> vec;      // 靈活應用vector.push_back(),即放到隊尾 (比較.push()入棧) ;用index來控制處理順序
            void deal(int x,int y,int times)
             
            {
                 
            if(x==EndX&&y==EndY) 
                  

                        find
            =true;
                        
            return;
                 }
              
                
               
                  
            if(x-2>=0&&y-1>=0&&minMoves[x-2][y-1]==-1)            //如果 某種走法沒有超過棋盤界限 且 那一格沒有走過
                    
                        minMoves[x
            -2][y-1]=times+1;
                        tempPoint.x
            =x-2;
                        tempPoint.y
            =y-1;
                        vec.push_back(tempPoint);
                   }

                  
            if(x-2>=0&&y+1<mapSize&&minMoves[x-2][y+1]==-1
                     
            {
                        minMoves[x
            -2][y+1]=times+1;
                        tempPoint.x
            =x-2;
                        tempPoint.y
            =y+1;
                        vec.push_back(tempPoint);
                    }

                  
            if(x+2<mapSize&&y+1<mapSize&&minMoves[x+2][y+1]==-1
                    
            {
                        minMoves[x
            +2][y+1]=times+1;
                        tempPoint.x
            =x+2;
                        tempPoint.y
            =y+1;
                        vec.push_back(tempPoint);
                   }

                  
            if(x+2<mapSize&&y-1>=0&&minMoves[x+2][y-1]==-1)
                   
            {
                        minMoves[x
            +2][y-1]=times+1;
                        tempPoint.x
            =x+2;
                        tempPoint.y
            =y-1;
                        vec.push_back(tempPoint);       
                  }

                  
            if(x-1>=0&&y-2>=0&&minMoves[x-1][y-2]==-1)
                     
            {
                        minMoves[x
            -1][y-2]=times+1;
                        tempPoint.x
            =x-1;
                        tempPoint.y
            =y-2;
                        vec.push_back(tempPoint);
                    }

                  
            if(x-1>=0&&y+2<mapSize&&minMoves[x-1][y+2]==-1
                    
            {
                        minMoves[x
            -1][y+2]=times+1;
                        tempPoint.x
            =x-1;
                        tempPoint.y
            =y+2;
                        vec.push_back(tempPoint);
                   }

                  
            if(x+1<mapSize&&y-2>=0&&minMoves[x+1][y-2]==-1)
                   
            {
                        minMoves[x
            +1][y-2]=times+1;
                       tempPoint.x
            =x+1;
                        tempPoint.y
            =y-2;
                        vec.push_back(tempPoint);
                    }

                  
            if(x+1<mapSize&&y+2<mapSize&&minMoves[x+1][y+2]==-1)
                    
            {
                        minMoves[x
            +1][y+2]=times+1;
                        tempPoint.x
            =x+1;
                        tempPoint.y
            =y+2;
                        vec.push_back(tempPoint);
                   }

            }


            int main()
             
            {
             
            int nCase;
             cin
            >>nCase;
             
            while(nCase--)
              
            {
                    cin
            >>mapSize;
                    cin
            >>beginX>>beginY;
                    cin
            >>EndX>>EndY;
                    find
            =false;   //初識設置索引是0
                    index=0;
                    memset(minMoves,
            -1,sizeof(minMoves)); //設置所有點未走過
                    minMoves[beginX][beginY]=0;   //設置起點已走過,步數是0
                    vec.clear();
                    point tempPoint;
                    tempPoint.x
            =beginX;
                    tempPoint.y
            =beginY;
                    vec.push_back(tempPoint);
                    
            while(index<vec.size()&&!find)   //vec里還有元素未處理完 且 沒有找到   vec.size() range from 0 to vex.size-1
                    {
                    deal(vec[index].x,vec[index].y,minMoves[vec[index].x][vec[index].y]);
                    index
            ++;       
                    }
             
                    cout
            <<minMoves[EndX][EndY]<<endl;
             }
                
                
            return 0;
            }

            posted on 2007-11-16 15:37 流牛ζ木馬 閱讀(2972) 評論(0)  編輯 收藏 引用

            <2007年11月>
            28293031123
            45678910
            11121314151617
            18192021222324
            2526272829301
            2345678

            導航

            統計

            公告

            MY Email/MSN :mars1021@163.com QQ : 27402040 流牛ζ木馬

            常用鏈接

            留言簿(6)

            隨筆檔案

            相冊

            搜索

            最新隨筆

            最新評論

            閱讀排行榜

            評論排行榜

            精品久久久无码中文字幕| 人妻丰满AV无码久久不卡| 国产99久久九九精品无码| 色综合久久久久网| 久久久久无码专区亚洲av| 性做久久久久久久久浪潮| 久久久无码一区二区三区| 国内精品久久久久久久亚洲| 2021国内久久精品| 久久免费精品一区二区| 亚洲精品午夜国产VA久久成人| 久久精品国产99国产电影网| 欧美精品乱码99久久蜜桃| 久久最新精品国产| 日本欧美久久久久免费播放网| 久久国产三级无码一区二区| 91精品国产91久久综合| 中文字幕精品久久| 久久精品不卡| 色综合久久中文综合网| 国内精品久久人妻互换| 亚洲女久久久噜噜噜熟女| 亚洲人成无码久久电影网站| 精品久久久久久无码国产| 国产成人精品免费久久久久| 婷婷五月深深久久精品| 久久久久亚洲av综合波多野结衣| 成人精品一区二区久久| 日本精品久久久久中文字幕| 久久久久人妻一区二区三区vr| 久久九九兔免费精品6| 思思久久精品在热线热| 久久经典免费视频| 一级做a爰片久久毛片看看| 亚洲精品国精品久久99热| 四虎国产精品免费久久| 四虎影视久久久免费| 人人狠狠综合88综合久久| 亚洲国产高清精品线久久 | 久久精品免费大片国产大片| 色综合合久久天天综合绕视看|