• <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 流牛ζ木馬 閱讀(2973) 評論(0)  編輯 收藏 引用

            <2009年6月>
            31123456
            78910111213
            14151617181920
            21222324252627
            2829301234
            567891011

            導航

            統計

            公告

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

            常用鏈接

            留言簿(6)

            隨筆檔案

            相冊

            搜索

            最新隨筆

            最新評論

            閱讀排行榜

            評論排行榜

            国产精品久久久天天影视香蕉| 精品人妻伦一二三区久久| 国产成人久久久精品二区三区| 久久亚洲精品无码VA大香大香| 久久精品国产清自在天天线| 精品无码久久久久久久动漫 | 日日狠狠久久偷偷色综合免费| 国产高清美女一级a毛片久久w| 久久久久久久久久久久中文字幕| 亚洲色婷婷综合久久| 亚洲va中文字幕无码久久不卡| 久久受www免费人成_看片中文| 中文字幕精品久久| 亚洲国产精品18久久久久久| 国内精品综合久久久40p| 久久成人小视频| 亚洲精品tv久久久久久久久 | 久久青青草原精品影院| 久久九九亚洲精品| 久久精品国产99国产电影网| 人妻无码久久精品| 国产精品久久久久a影院| 日产精品久久久一区二区| 久久亚洲精品视频| 国产精自产拍久久久久久蜜| 久久人人爽人人爽人人片AV麻豆 | 亚洲愉拍99热成人精品热久久 | 亚洲午夜久久久久久久久电影网| 日韩久久久久久中文人妻 | 国产99久久九九精品无码| 久久久久99精品成人片| 免费久久人人爽人人爽av| 久久精品一本到99热免费| 久久精品亚洲男人的天堂| 97精品依人久久久大香线蕉97| 久久免费国产精品一区二区| 久久人人爽人人爽人人片AV东京热 | 久久精品中文字幕一区| 久久精品免费观看| 亚洲精品白浆高清久久久久久| 国产99久久九九精品无码|