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 }


  • 相关阅读:
    mysql-python不支持python3
    解决:python安装mysqldb模块报 EnvironmentError: mysql_config not found
    自动ftp脚本,aix/linux 和 windows
    Poi设置列样式
    sails中创建和使用services
    test
    知识科普:IM聊天应用是如何将消息发送给对方的?(非技术篇)
    IM开发者的零基础通信技术入门(一):通信交换技术的百年发展史(上)
    鹅厂7年终有离开之日,记离职鹅厂最后30天的真实心路历程
    仿微信的IM聊天时间显示格式(含iOS/Android/Web实现)[图文+源码]
  • 原文地址:https://www.cnblogs.com/wilsonjuxta/p/2953747.html
Copyright © 2011-2022 走看看