zoukankan      html  css  js  c++  java
  • poj 1573Robot Motion

    Description


    A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are

    N north (up the page)
    S south (down the page)
    E east (to the right on the page)
    W west (to the left on the page)

    For example, suppose the robot starts on the north (top) side of Grid 1 and starts south (down). The path the robot follows is shown. The robot goes through 10 instructions in the grid before leaving the grid.

    Compare what happens in Grid 2: the robot goes through 3 instructions only once, and then starts a loop through 8 instructions, and never exits.

    You are to write a program that determines how long it takes a robot to get out of the grid or how the robot loops around.

    Input

    There will be one or more grids for robots to navigate. The data for each is in the following form. On the first line are three integers separated by blanks: the number of rows in the grid, the number of columns in the grid, and the number of the column in which the robot enters from the north. The possible entry columns are numbered starting with one at the left. Then come the rows of the direction instructions. Each grid will have at least one and at most 10 rows and columns of instructions. The lines of instructions contain only the characters N, S, E, or W with no blanks. The end of input is indicated by a row containing 0 0 0.

    Output

    For each grid in the input there is one line of output. Either the robot follows a certain number of instructions and exits the grid on any one the four sides or else the robot follows the instructions on a certain number of locations once, and then the instructions on some number of locations repeatedly. The sample input below corresponds to the two grids above and illustrates the two forms of output. The word "step" is always immediately followed by "(s)" whether or not the number before it is 1.

    Sample Input

    3 6 5
    NEESWE
    WWWESS
    SNWWWW
    4 5 1
    SESWE
    EESNW
    NWEEN
    EWSEN
    0 0 0

    Sample Output

    10 step(s) to exit
    3 step(s) before a loop of 8 step(s)
    
    和2632有类似之处,此题也只要按照机器人每一步上所要求的方向行走即可,简单模拟。
    View Code
     1 #include <stdio.h>
     2 #include <string.h>
     3 struct rob
     4 {
     5     int dis;
     6     int cnt;
     7 }map[20][20];
     8 int dx[]={-1,0,1,0};
     9 int dy[]={0,-1,0,1};
    10 int charge(char c)
    11 {
    12     if(c=='N') return 0;
    13     if(c=='W') return 1;
    14     if(c=='S') return 2;
    15     if(c=='E') return 3;
    16 }
    17 int main()
    18 {
    19     int n,m,s,i,j;
    20     int a,b;
    21     int x,y,flag,num,z;
    22     char str[20];
    23     while(scanf("%d%d%d",&n,&m,&s)&&(n+m+s))
    24     {
    25         flag=1;
    26         memset(map,-1,sizeof(map));
    27         for(i=1;i<=n;i++)
    28         {
    29             scanf("%s",str+1);
    30             for(j=1;str[j];j++)
    31                 map[i][j].dis=charge(str[j]);
    32         }
    33         /*for(i=1;i<=n;i++)
    34         {
    35             for(j=1;j<=m;j++)
    36                 printf("%d",map[i][j]);
    37             printf("\n");        
    38         }*/
    39         x=1,y=s;
    40         num=map[x][y].cnt=0;
    41         while(flag)
    42         {
    43             a=x,b=y;
    44             x+=dx[map[a][b].dis];
    45             y+=dy[map[a][b].dis];
    46             if(map[x][y].cnt!=-1)
    47             {
    48                 flag=2;
    49                 z=num+1-map[x][y].cnt;
    50                 num=map[x][y].cnt;
    51                 break;
    52             }
    53             if(x==0||x>n||y==0||y>m)
    54             {
    55                 flag=0;
    56                 break;
    57             }
    58             map[x][y].cnt=++num;
    59         }
    60         if(flag==0)
    61             printf("%d step(s) to exit\n",num+1);
    62         else if(flag==2)
    63             printf("%d step(s) before a loop of %d step(s)\n",num,z);
    64     }
    65     return 0;
    66 }


  • 相关阅读:
    关于回调地狱
    node.js 关于跨域和传递给前台参数
    关于js的当前日期的格式化,和两个日期之间的相减的天数
    CSS 关于让页面的高度达到电脑屏幕的底部
    前端 为什么我选择用框架而不是Jquery
    关于webpack打包图片的路径问题
    关于webpack打包js和css
    微信小程序网络请求的setDate
    WinSocket聊天程序实例(多线程)
    Orcal的JDBC数据连接方式
  • 原文地址:https://www.cnblogs.com/wilsonjuxta/p/2953747.html
Copyright © 2011-2022 走看看