Hdoj 1035 Robot Motion

Source: Internet
Author: User

Robot Motion

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 7974 Accepted Submission (s): 3685


Problem 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.

Inputthere'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.

Outputfor each grid with 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 INPUT3 6 5neeswewwwesssnwwww4 5 1sesweeesnwnweenewsen0 0

Sample Output10 Step (s) to exit3 step (s) before a loop of 8 step (s) at the time of the two places are pits to: 1, the beginning of the horizontal ordinate the reverse 2, the result of the output error later found in the input when the array is starting from 0 and In search judgment, it starts with 1 in the array.
 #include <stdio.h> #include <string.h> #define MAX 110char map[max][ max];//to go map int vis[max][max];//is used to record how many steps are currently taken int n,m,t;int x2,y2;//used to record the current step of the previous step void Dfs (int x1,int y1) {if (x1>n| | x1<1| | y1<1| | Y1>M)//If beyond the map extent proves to have gone out {printf ("%d step (s) to exit\n", Vis[x2][y2]); return;} else if (vis[x1][y1])//If it is not 0 proof that this has been traversed to form ring {printf ("%d step (s) before a loop of%d step (s) \ n", vis[x1][y1]-1,vis[x2][y2]-    VIS[X1][Y1]+1); return;} vis[x1][y1]=vis[x2][y2]+1;//the number of steps currently walking is the previous step plus 1 x2=x1;y2=y1;if (map[x1][y1]== ' W ')//Go left Y1-=1;else if (map[x1][y1]== ' S ')// Go down X1+=1;else if (map[x1][y1]== ' E ')//GO right Y1+=1;else if (map[x1][y1]== ' N ')//Go up X1-=1;dfs (x1,y1);} int main () {int J,i,s,k;int x1,x2,y1,y2;while (scanf ("%d%d", &n,&m) &&n!=0&&m!=0) {scanf ("%d",    &T); for (i=1;i<=n;i++) {GetChar (); for (j=1;j<=m;j++) {scanf ("%c", &map[i][j]);}}    x1=1;y1=t;//beginning X2=x1;y2=y1; memset (vis,0,sizeof (VIS));//array Clear 0 DFS (x1,y1);} return 0;} 

  

Hdoj 1035 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.