zoukankan      html  css  js  c++  java
  • USACO2.4.1The 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
    解题思路:水水的模拟。用两个变量标记一下牛和John的移动方向,移动操作与方向相对应,遇到障碍就转向。这个题目主要就是考虑没有解的状态,地图大小为10*10=100。牛和人每次移动有4个选择,上下左右,对于每个点最多走4次,即从上下左右四个方向分别走一次。如果牛和人移动100*4=400次还没相遇,那么永远不可能相遇,输出0即可。
    View Code
     1 /*
     2 ID:spcjv51
     3 PROG:ttwo
     4 LANG:C
     5 */
     6 #include<stdio.h>
     7 #include<string.h>
     8 int main(void)
     9 {
    10     freopen("ttwo.in","r",stdin);
    11     freopen("ttwo.out","w",stdout);
    12     char map[12][12];
    13     int cowsteps,i,j,x1,x2,y1,y2,flag,dc,dj;
    14     for(i=0; i<=11; i++)
    15         for(j=0; j<=11; j++)
    16             map[i][j]='*';
    17     for(i=1; i<=10; i++)
    18     {
    19         for(j=1; j<=10; j++)
    20         {
    21             scanf("%c",&map[i][j]);
    22             if(map[i][j]=='C')
    23             {
    24                 x1=i;
    25                 y1=j;
    26                 map[i][j]='.';
    27             }
    28             if(map[i][j]=='F')
    29             {
    30                 x2=i;
    31                 y2=j;
    32                 map[i][j]='.';
    33             }
    34         }
    35         getchar();
    36     }
    37     cowsteps=0;
    38     flag=1;
    39     dc=1;
    40     dj=1;
    41     while(cowsteps<=400&&flag)
    42     {
    43         if(map[x1][y1]=='*')
    44         {
    45             if(dc==1)
    46             x1++;
    47             if(dc==2)
    48             y1--;
    49             if(dc==3)
    50             x1--;
    51             if(dc==4)
    52             y1++;
    53             dc=dc%4+1;
    54 
    55         }
    56         if(map[x2][y2]=='*')
    57         {
    58             if(dj==1)
    59             x2++;
    60             if(dj==2)
    61             y2--;
    62             if(dj==3)
    63             x2--;
    64             if(dj==4)
    65             y2++;
    66             dj=dj%4+1;
    67         }
    68          if(x1==x2&&y1==y2)
    69         {
    70             printf("%d\n",cowsteps);
    71             flag=0;
    72             break;
    73         }
    74        if(dc==1) x1--;
    75        if(dc==2) y1++;
    76        if(dc==3) x1++;
    77        if(dc==4) y1--;
    78        if(dj==1) x2--;
    79        if(dj==2) y2++;
    80        if(dj==3) x2++;
    81        if(dj==4) y2--;
    82        cowsteps++;
    83     }
    84 
    85     if(flag) printf("0\n");
    86     return 0;
    87 }
     
  • 相关阅读:
    linux 常用函数
    现在什么都不是浮云
    laosao
    2012年
    工作任务
    代码整理
    如何做一个男人
    很重要的2点
    录像预录的设计及实现
    弄了一个小网站
  • 原文地址:https://www.cnblogs.com/zjbztianya/p/2910784.html
Copyright © 2011-2022 走看看