zoukankan      html  css  js  c++  java
  • J

    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. 

     

     

    InputThere 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. 
    OutputFor 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 

    Sample Output

    10 step(s) to exit
    3 step(s) before a loop of 8 step(s)

    题目意思:
    有一个机器人,和一堆指令,机器人从一个指令开始出发,如果离开地图输出移动的步数,如果进入了一个循环,这输出进循环前的步数和循环的步数

    解法:
    一步步来就可以了,不难
     1 #include <iostream>
     2 #include <cstring>
     3 #include <queue>
     4 using namespace std;
     5 
     6 char Map[20][20];
     7 int visit[20][20];
     8 int x,y;
     9 int x0,y0;
    10 
    11 struct S
    12 {
    13     int x,y;
    14     int cont;
    15 };
    16 
    17 void bsf()
    18 {
    19     S s[400];
    20 
    21     int ti = 0;
    22     s[0].x = x0;
    23     s[0].y = y0;
    24     s[0].cont = 0;
    25     visit[x0][y0] = 0;
    26 
    27     while(1)
    28     {
    29         S temp;
    30         temp.x = s[ti].x;
    31         temp.y = s[ti].y;
    32         temp.cont = s[ti].cont + 1;
    33         if(Map[temp.x][temp.y] == 'N')
    34             temp.x--;
    35         else if(Map[temp.x][temp.y] == 'S')
    36             temp.x++;
    37         else if(Map[temp.x][temp.y] == 'W')
    38             temp.y--;
    39         else if(Map[temp.x][temp.y] == 'E')
    40             temp.y++;
    41 
    42         if(temp.x < 1||temp.x > x||temp.y < 1||temp.y > y)
    43         {
    44             cout<<temp.cont<<" step(s) to exit"<<endl;
    45             break;
    46         }
    47         if(visit[temp.x][temp.y] != -1)
    48         {
    49             cout<<visit[temp.x][temp.y]<<" step(s) before a loop of "<<temp.cont-visit[temp.x][temp.y]<<" step(s)"<<endl;
    50             break;
    51         }
    52         s[++ti] = temp;
    53         visit[temp.x][temp.y] = temp.cont;
    54     }
    55 
    56 
    57 }
    58 
    59 int main()
    60 {
    61     while(cin>>x>>y)
    62     {
    63         memset(Map,'0',sizeof(Map));
    64         memset(visit,-1,sizeof(visit));
    65         if(x==0&&y==0)
    66             break;
    67         cin>>y0;
    68         x0 = 1;
    69         for(int i =1;i <= x;i++)
    70             for(int j = 1;j <= y;j++)
    71                 cin>>Map[i][j];
    72         bsf();
    73     }
    74 
    75     return 0;
    76 }
  • 相关阅读:
    Git学习
    flask学习5 错误页面
    flask学习4-会话
    flask学习3
    线性代数复习
    flask学习2
    flask学习1
    windows下安装easy_install,pip,及flask入门教程链接
    MacOS Terminal调用Python代码
    [java] 类变量初始化顺序
  • 原文地址:https://www.cnblogs.com/a2985812043/p/7226960.html
Copyright © 2011-2022 走看看