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

            USACO Section 2.4 The Tamworth Two

            The Tamworth Two

            BIO '98 - Richard Forster

            A pair of cows is loose somewhere in the forest. Farmer John is lending his expertise to their capture. Your task is to model their behavior.

            The chase takes place on a 10 by 10 planar grid. Squares can be empty or they can contain:

            • an obstacle,
            • the cows (who always travel together), or
            • Farmer John.

            The cows and Farmer John can occupy the same square (when they `meet') but neither the cows nor Farmer John can share a square with an obstacle.

            Each square is
            represented
            as follows:

            • . Empty square
            • * Obstacle
            • C Cows
            • F Farmer
            Here is a sample grid:
                        *...*.....
                        ......*...
                        ...*...*..
                        ..........
                        ...*.F....
                        *.....*...
                        ...*......
                        ..C......*
                        ...*.*....
                        .*.*......
                        

            The cows wander around the grid in a fixed way. Each minute, they either move forward or rotate. Normally, they move one square in the direction they are facing. If there is an obstacle in the way or they would leave the board by walking `forward', then they spend the entire minute rotating 90 degrees clockwise.

            Farmer John, wise in the ways of cows, moves in exactly the same way.

            The farmer and the cows can be considered to move simultaneously during each minute. If the farmer and the cows pass each other while moving, they are not considered to have met. The chase ends when Farmer John and the cows occupy the same square at the end of a minute.

            Read a ten-line grid that represents the initial state of the cows, Farmer John, and obstacles. Each of the ten lines contains exactly ten characters using the coding above. There is guaranteed to be only one farmer and one pair of cows. The cows and Farmer John will not initially be on the same square.

            Calculate the number of minutes until the cows and Farmer John meet. Assume both the cows and farmer begin the simulation facing in the `north' direction. Print 0 if they will never meet.

            PROGRAM NAME: ttwo

            INPUT FORMAT

            Lines 1-10: Ten lines of ten characters each, as explained above

            SAMPLE INPUT (file ttwo.in)

            *...*.....
            ......*...
            ...*...*..
            ..........
            ...*.F....
            *.....*...
            ...*......
            ..C......*
            ...*.*....
            .*.*......
            

            OUTPUT FORMAT

            A single line with the integer number of minutes until Farmer John and the cows meet. Print 0 if they will never meet.

            SAMPLE OUTPUT (file ttwo.out)

            49
            
            Analysis
            A simulating problem. These problems aims to replay actions by programs. Moreover, the total number of situations is 160000, which is used to make sure whether these two will never meet.
            Code
            /*
            ID: braytay1
            PROG: ttwo
            LANG: C++
            */

            #include 
            <iostream>
            #include 
            <fstream>
            using namespace std;

            char map[10][10];
            struct cows{
                
            int x;
                
            int y;
                
            int face;
            }
            cow,farmer;
            bool in(int x,int y){
                
            if (x<0||x>=10return false;
                
            if (y<0||y>=10return false;
                
            else return true;
            }

            int main(){
                ifstream fin(
            "ttwo.in");
                ofstream fout(
            "ttwo.out");
                
            for (int i=0;i<10;i++){
                    
            for (int j=0;j<10;j++){
                        fin
            >>map[i][j];
                    }

                }

                
            int time=0,met=0,x,y;
                
            for (int i=0;i<10;i++){
                    
            for (int j=0;j<10;j++){
                        
            if (map[i][j]=='C'{
                            cow.x
            =i;
                            cow.y
            =j;
                            cow.face
            =1;
                            map[i][j]
            ='.';
                        }

                        
            if (map[i][j]=='F'{
                            farmer.x
            =i;
                            farmer.y
            =j;
                            farmer.face
            =1;
                            map[i][j]
            ='.';
                        }

                    }

                }

                
            for (;time<=160000;time++){
                    
            if (cow.x==farmer.x&&cow.y==farmer.y){
                        met
            =1;
                        fout
            <<time<<endl;
                        
            return 0;
                    }

                    x
            =cow.x;y=cow.y;
                    
            switch(cow.face){
                        
            case 1:if(map[x-1][y]=='.'&&in(x-1,y)){cow.x--;break;}
                               
            else {
                                   cow.face
            =(cow.face+1)%4;
                                   
            break;
                               }

                        
            case 2:if(map[x][y+1]=='.'&&in(x,y+1)){cow.y++;break;}
                               
            else {
                                   cow.face
            =(cow.face+1)%4;
                                   
            break;
                               }

                        
            case 3:if(map[x+1][y]=='.'&&in(x+1,y)){cow.x++;break;}
                               
            else {
                                   cow.face
            =(cow.face+1)%4;
                                   
            break;
                               }

                        
            case 0:if(map[x][y-1]=='.'&&in(x,y-1)){cow.y--;break;}
                               
            else {
                                   cow.face
            =(cow.face+1)%4;
                                   
            break;
                               }

                    }

                    x
            =farmer.x;y=farmer.y;
                    
            switch(farmer.face){
                        
            case 1:if(map[x-1][y]=='.'&&in(x-1,y)){farmer.x--;break;}
                               
            else {
                                   farmer.face
            =(farmer.face+1)%4;
                                   
            break;
                               }

                        
            case 2:if(map[x][y+1]=='.'&&in(x,y+1)){farmer.y++;break;}
                               
            else {
                                   farmer.face
            =(farmer.face+1)%4;
                                   
            break;
                               }

                        
            case 3:if(map[x+1][y]=='.'&&in(x+1,y)){farmer.x++;break;}
                               
            else {
                                   farmer.face
            =(farmer.face+1)%4;
                                   
            break;
                               }

                        
            case 0:if(map[x][y-1]=='.'&&in(x,y-1)){farmer.y--;break;}
                               
            else {
                                   farmer.face
            =(farmer.face+1)%4;
                                   
            break;
                               }

                    }

                }

                
            if (met==0) fout<<0<<endl;
                
            return 0;
            }

            posted on 2008-08-13 20:59 幻浪天空領(lǐng)主 閱讀(236) 評(píng)論(0)  編輯 收藏 引用 所屬分類: USACO

            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(1)

            隨筆檔案(2)

            文章分類(23)

            文章檔案(22)

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久97精品久久久久久久不卡| 久久中文字幕一区二区| 国产精品久久影院| 亚洲va久久久噜噜噜久久天堂| 亚洲人成无码久久电影网站| 99久久国产综合精品网成人影院| 久久精品夜夜夜夜夜久久| 伊人久久大香线焦AV综合影院| 久久频这里精品99香蕉久| 欧美精品一区二区久久| 欧美成a人片免费看久久| 四虎久久影院| 亚洲国产成人精品女人久久久| 亚洲国产成人久久一区WWW| 深夜久久AAAAA级毛片免费看 | 亚洲中文字幕无码久久2020| 无码乱码观看精品久久| 色综合久久久久综合99| 久久这里只精品99re66| 中文字幕精品久久久久人妻| 思思久久好好热精品国产| 久久精品中文无码资源站| 精品少妇人妻av无码久久| 亚洲伊人久久精品影院| 97精品国产91久久久久久| 国产精品久久亚洲不卡动漫| 日韩一区二区久久久久久| 久久综合狠狠综合久久激情 | 久久精品国产精品亚洲毛片 | 亚洲综合婷婷久久| 久久99精品久久久久久不卡| 久久综合久久伊人| 日韩精品久久久久久免费| 国产精品一久久香蕉国产线看| 久久午夜电影网| 久久精品国产黑森林| 久久人妻少妇嫩草AV蜜桃| 久久久久亚洲AV无码专区体验 | 精品久久久久久久中文字幕 | 91精品国产91热久久久久福利 | 无码精品久久久天天影视|