POJ 1573-robot Motion (analog)

Source: Internet
Author: User

Robot Motion
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 10968 Accepted: 5319

Description


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

N North (up the page)
S South (down the page)
E East (to "right" on the page)
W West (to the left on the page)

For example, suppose the robot starts on the north (top) side of grids 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 on Grid 2:the robot goes through 3 instructions only once, and then starts a loop through 8 instruct Ions, and never exits.

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

Input

There'll be is one or more grids for robots to navigate. The data for each are in the following form. On the first line is three integers separated by blanks:the number of rows in the grid and the number of columns in the GRI D, and the number of the column in which the robot enters from the north. The possible entry columns is numbered starting with one in the left. Then come the rows of the direction instructions. Each grid would has at least one and at the most rows and columns of instructions. The lines of instructions contain only is 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, the input there is a 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 F Ollows the instructions on a certain number of locations once, and then the instructions on some number of locations Repea tedly. The sample input below corresponds to the grids above and illustrates the both forms of output. The word "step" is all immediately followed by "(s)" Whether or not, the number before it is 1.

Sample Input

3 6 5neeswewwwesssnwwww4 5 1sesweeesnwnweenewsen0 0 0

Sample Output

Step (s) to exit3 step (s) before a loop of 8 step (s)

Test instructions: There are n rows of M-columns, and T-columns enter the number of columns from the north. Each letter is its instruction. If the robot can go out, the output step out of the number, if in a circle, the output of the first to reach the loop before the number of steps, the output of the second is from the beginning to turn the number of steps to complete a lap.

#include <stdio.h> #include <string.h> #include <stdlib.h>int main () {int n,m,t;    int i,j;    Char str[30][30];    Char vis[30][30];    int cnt,loop;    int flag;    int x, y;        while (~SCANF ("%d%d", &n,&m,&t)} {if (n==0&&m==0&&t==0) break;        Flag = 0;        Cnt=1;        loop = 0;        memset (str,0,sizeof (str));        memset (vis,0,sizeof (VIS));        for (i = 0; i<n; i++) scanf ("%s", Str[i]);        x = 0;        y = t-1;                while (1) {if (str[x][y] = = ' N ' &&!vis[x][y]) {vis[x][y] = cnt;            x--;                } else if (str[x][y] = = ' S ' &&!vis[x][y]) {vis[x][y] = cnt;            x + +;                } else if (str[x][y] = = ' E ' &&!vis[x][y]) {vis[x][y] = cnt;            y++; } else if (str[x][y] = = ' W ' &&!vis[x][y])            {Vis[x][y] = cnt;            y--;                } else if (x < 0 | | x >= n| | y<0 | | y>=m) {cnt--;            Break                } else if (vis[x][y]!=0) {cnt--;                loop = cnt-vis[x][y]+1;                flag = 1;            Break        } cnt++;        } if (flag) printf ("%d step (s) before a loop of%d step (s) \ n", Cnt-loop,loop);    else printf ("%d step (s) to exit\n", CNT); } return 0;}


POJ 1573-robot Motion (analog)

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.