HDU 1035 (Simple Simulation) _ Simulation,

Source: Internet
Author: User
Robot Motion time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 9566 accepted Submission (s): 4434


Problem Description

A robot has been programmed to follow the instructions into its path. Instructions for the next direction the robot are 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" side of Grid 1 and starts south (down). The path the robot follows is shown. The robot goes through 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 instruct Ions, and never exits.

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

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

Output for each grid in the input there was one line of output. Either the robot follows a certain number of instructions and exits the grid on any one of the four sides or else the robot F Ollows the instructions on a certain number of locations once, and then to instructions on some number of locations Repea tedly. 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
Ten step (s) to exit 3 step (s) before a loop of 8 step (s)
Source Mid-Central USA 1999
Recommend
In the given map, according to the upper north and south, Levocetirizine right east, the instructions, robot can get out of the map, out of the map with how many steps to go out of the cycle how many steps ...
The data is small, a simple simulation, see they also useful Dfs write ...

#include <iostream> #include <algorithm> #include <string.h> #include <math.h> #include <
stdio.h> #include <queue> #define INF 0x3f3f3f3f typedef long LL;

using namespace Std; Char maps[15][15];   Analog map int vis[15][15];
    Mark the place that has been traversed, and record the number of steps int main () {int n,m,t;
    int loop,step,i;
        while (~SCANF ("%d%d%d", &n,&m,&t)) {if (!n &&!m &&!t) break;
        int flag=0;
        memset (Vis) (vis,0,sizeof);

        memset (maps,0,sizeof (maps));

        for (i=0;i<n;i++) scanf ("%s", Maps[i]);
        Step=1;
        Loop=0;
        int x,y;
        x=0;  y=t-1;
                Array is stored from 0, so minus one while (1) {if (maps[x][y]== ' N ' &&!vis[x][y]) {
                Vis[x][y]=step;
            x--;
                else if (maps[x][y]== ' S ' &&!vis[x][y]) {vis[x][y]=step;
          x + +;  else if (maps[x][y]== ' E ' &&!vis[x][y]) {vis[x][y]=step;
            y++;
                else if (maps[x][y]== ' W ' &&!vis[x][y]) {vis[x][y]=step;
            y--;
            else if (Vis[x][y])///has passed, it means that the form of a dead loop, Mark, exit.  
                {step--;
                loop=step-vis[x][y]+1;   flag=1;
            Whether the tag has a dead loop break;
            else if (x<0 | | y<0 | | x>=n | | y>=m)///out of the map.
                {step--;
            Break    } step++;
        Every walk, step number plus one.
        } if (flag) printf ("%d step (s) before a loop to%d step (s) \ n", Step-loop,loop);
    else printf ("%d step (s) to exit\n", step);
return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.