• <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 典型的寬度優(yōu)先搜索 BFS

            http://acm.pku.edu.cn/JudgeOnline/problem?id=1915
            發(fā)現(xiàn)用vector來(lái)做寬搜的隊(duì)列,要比自己弄一個(gè)隊(duì)列來(lái)記錄方便得多,呵呵
            程序很簡(jiǎn)單,關(guān)鍵地方我都注釋上了
            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;      // 靈活應(yīng)用vector.push_back(),即放到隊(duì)尾 (比較.push()入棧) ;用index來(lái)控制處理順序
            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)            //如果 某種走法沒(méi)有超過(guò)棋盤(pán)界限 且 那一格沒(méi)有走過(guò)
                    
                        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;   //初識(shí)設(shè)置索引是0
                    index=0;
                    memset(minMoves,
            -1,sizeof(minMoves)); //設(shè)置所有點(diǎn)未走過(guò)
                    minMoves[beginX][beginY]=0;   //設(shè)置起點(diǎn)已走過(guò),步數(shù)是0
                    vec.clear();
                    point tempPoint;
                    tempPoint.x
            =beginX;
                    tempPoint.y
            =beginY;
                    vec.push_back(tempPoint);
                    
            while(index<vec.size()&&!find)   //vec里還有元素未處理完 且 沒(méi)有找到   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 流牛ζ木馬 閱讀(2982) 評(píng)論(0)  編輯 收藏 引用


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


            <2016年3月>
            282912345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導(dǎo)航

            統(tǒng)計(jì)

            公告

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

            常用鏈接

            留言簿(6)

            隨筆檔案

            相冊(cè)

            搜索

            最新隨筆

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久精品?ⅴ无码中文字幕| 欧美精品一区二区久久| 久久久久亚洲AV无码观看| 久久SE精品一区二区| 久久人人爽人人爽人人片AV东京热| 久久久久久亚洲精品影院| 久久精品桃花综合| 国产精品久久久久久| 97久久精品人人做人人爽| 97视频久久久| 国产巨作麻豆欧美亚洲综合久久| 亚洲&#228;v永久无码精品天堂久久 | 久久精品国产精品青草| 国产成人精品久久一区二区三区 | 久久综合久久伊人| 亚洲国产另类久久久精品小说| 国产亚洲精品自在久久| 欧美亚洲日本久久精品| 国内精品久久久久影院一蜜桃| 一本色道久久综合亚洲精品| 色综合久久88色综合天天| 日韩人妻无码精品久久免费一| 久久综合视频网站| 久久99精品久久久久久秒播| av午夜福利一片免费看久久| 久久久久AV综合网成人 | 亚洲精品tv久久久久| 国产成人精品综合久久久| 国产香蕉97碰碰久久人人| 久久精品99无色码中文字幕| 久久强奷乱码老熟女网站| 久久亚洲中文字幕精品一区| 久久久久九国产精品| 亚洲日韩欧美一区久久久久我| 国产99久久久国产精品小说| 国产婷婷成人久久Av免费高清| 91精品国产色综合久久| 久久最新免费视频| 久久线看观看精品香蕉国产| 久久国产精品久久国产精品| 久久综合五月丁香久久激情|