ACM TOPIC ———— Robot Motion

Source: Internet
Author: User

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)

It is not difficult to read the topic.

The main idea is: give the R row C column, and then from the first row of the position I began to walk each position has marked e W S N, respectively, to the east, westward, south, north and then, there are two situations, one can go out, one is into the dead loop. Request Output!

Solution One: (Direct simulation)

#include <stdio.h>Charstr[ -][ -]; intMain () {intR, C,inch;  while(SCANF (" %d%d%d", &r, &c, &inch) !=EOF) {          if(r = =0&& c = =0&&inch==0)               Break;          GetChar ();  for(inti =0; I < R; i++) gets (Str[i]); intTR, TC, loop; intStep =0; intMark =1; TR=0; TC =inch-1;  while(1) {              if(STR[TR][TC] = ='E') {Step++; STR[TR][TC]= Step +'0'; if(TC = = C1)                       Break; ElseTC= TC +1; }              Else if(STR[TR][TC] = ='W') {Step++; STR[TR][TC]= Step +'0'; if(TC = =0)                       Break; ElseTC= TC-1; }              Else if(STR[TR][TC] = ='S') {Step++; STR[TR][TC]= Step +'0'; if(tr = = R1)                       Break; ElseTR= tr +1; }              Else if(STR[TR][TC] = ='N') {Step++; STR[TR][TC]= Step +'0'; if(tr = =0)                       Break; ElseTR= TR-1; }              Else{Mark=0; Loop= STR[TR][TC]-'0'-1; printf ("%d step (s) before a loop of%d step (s) \ n", Loop, Step-loop);  Break; }          }          if(Mark = =1) printf ("%d step (s) to exit\n", step); }      return 0; }  


Solution Two: (DFS)

#include <stdio.h>#include<stdlib.h>#include<string.h>Charmap[ the][ the] ;//used to record graphicsintdes[ the][ the] ;//used to record the number of steps taken at that point and to determine if a loop is formedintA, B, L, Count, F, H;//A, B records the number of rows entered, count records the cycle frequency, H, the number of steps before the loopvoidDFS (intMintNintsp) {    if(M <0|| M >= A | | N <0|| n >= B)//judge whether to go out{f=1 ; Count=sp; return ; }    if(Des[m][n])//determine if the loop{f=0 ; Count=Des[m][n]; H= SP-Des[m][n]; return ; } Des[m][n]= SP;//assign a number of steps to Des    if(Map[m][n] = ='N') DFS (M-1, N, sp+1) ; if(Map[m][n] = ='S') DFS (M+1, N, sp+1 ) ; if(Map[m][n] = ='E') DFS (M, n+1, sp+1 ) ; if(Map[m][n] = ='W') DFS (M, n-1, sp+1 ) ;}intMain () { while(SCANF ("%d%d", &a, &b), a | |b) {scanf ("%d", &l); Memset (DES,0,sizeof(DES));        GetChar (); Count=0 ; F=0 ; H=0 ;  for(inti =0; i < A; i++ )        {             for(intj =0; J < b; J + +) scanf ("%c", &Map[i][j]);        GetChar (); } DFS (0, L-1,1 ) ; if(f) {printf ("%d step (s) to exit\n", count-1 ) ; }        Elseprintf ("%d step (s) before a loop of%d step (s) \ n", count-1, h); }    return 0;}


ACM TOPIC ———— Robot Motion

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.