zoukankan      html  css  js  c++  java
  • Robot Motion

    题目:

    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)
    题意:
    输入一个矩阵,第一行输入矩阵的行数,列数,以及机器人在第一行的列数;
    接着输入一个由NSWE四个字母构成的矩阵,(N:向北,S:向南,E:向东,W:向西)根据字母的指示前进,如果能出去
    就输出出去需要的步数,如果进入循环无法出来则输出机器人在前进多少步之后进入一个需要的前进多少步的循环,即
    (这个循环由多少步组成)
    分析:
    char型的二维数组,用于存储矩阵;
    int型的二维数组,用于存储每到达当前位置所前进的步数;
    int型的二维数组,用于标记到达的位置是否已经到达过;

    AC代码:
    #include<iostream>
    #include<cstring>
    using namespace std;
    char a[11][11];
    int flag[11][11],steps[121][121];
    int main()
    {
        int row,col,start,step;
        int si,sj;
    
        while (cin>>row>>col>>start&&(row!=0||col!=0||start!=0))
        {
            memset(flag,0,sizeof(flag));
            for (int i=1;i<=row;i++)
                for (int j=1;j<=col;j++)
                cin>>a[i][j];
            si=1;
            sj=start;
       //     flag[si][sj]=1;
              step=0;
           while (si>=1&&si<=row&&sj>=1&&sj<=col&&!flag[si][sj])
           {
               flag[si][sj]=1;
               steps[si][sj]=step;
               if (a[si][sj]=='N')
                     si--;
               else if (a[si][sj]=='S')
                    si++;
               else if (a[si][sj]=='E')
                    sj++;
               else if (a[si][sj]=='W')
                    sj--;
                    step++;
           }
           if (flag[si][sj]==0)
            cout << step << " step(s) to exit" << endl;
           else
            cout << steps[si][sj] << " step(s) before a loop of " << step-steps[si][sj] << " step(s)" << endl;
        }
        return 0;
    }
    
    
    
     
  • 相关阅读:
    02-30 线性可分支持向量机
    02-28 scikit-learn库之线朴素贝叶斯
    02-27 朴素贝叶斯
    02-26 决策树(鸢尾花分类)
    047 选项模式
    第二节:师傅延伸的一些方法(复习_总结)
    第一节:登录流程
    第一节:对应拼音编码查询(后续更新)
    前端对象
    Form表单
  • 原文地址:https://www.cnblogs.com/lisijie/p/7229757.html
Copyright © 2011-2022 走看看