Robot Motion
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 7488 Accepted Submission (s): 3431
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 Input
Sample Output
Step (s) to exit3 step (s) before a loop of 8 step (s)
I change your times, not his mother still let brother AC. Although this is a simple simulation problem, but after all, brother life for the first time, thousands of twists and pains ah.
#include <iostream>using namespace std;struct ls{int x;int y;} Cnt[203];void Go (char (*p) [11],int n,int m,int &co) {//for (int r=1;r<=n;r++) for (int e=1;e<=m;e++)//cout<<p[r][e]<< ""; int ro=1,i=0,count=0;fo R (int v=0;v<2*n*m;v++)//This is the last time to modify the place, that is the last time the wrong place, just imagine if you want to {//Find a loop in the matrix if it exists, because walking The maximum number of steps in the range n*m, maximum cycle 2*n*m. if (p[ro][co]== ' S ') {count++; cout<< "count=" <<count<< "!" <<endl; cout<< "count=" <<count<< "!" <<endl;if (Ro==n)//Determine if the corresponding row and column are boundary values. {Cnt[++i].x=ro; Before these two sentences are written i++ result is two 1,x,y assignment is not the same structure//after the second sentence is changed to I, found or not the same structure. The first one was then changed to ++i Cnt[i].y=co;break;} Else{cnt[++i].x=ro; Record each step of the coordinate cnt[i].y=co;ro++; cout<<cnt[i].x<< "<<cnt[i].y<<" "<<endl;}} else if (p[ro][co]== ' E ') {count++; cout<< "count=" <<count<< "!" <<endl;if (co==m) {cnt[++i].x=ro;cnt[i].y=co;break;} else{cnt[++i].x=ro;cnt[i].y=co;co++; cout<<cnt[i].x<< "<<cnt[i].y<<" "<<endl;}} else if (p[ro][co]== ' N ') {count++; cout<< "count=" <<count<< "!" <<endl;if (ro==1) {cnt[++i].x=ro;cnt[i].y=co;break;} else{cnt[++i].x=ro;cnt[i].y=co;ro--; cout<<cnt[i].x<< "<<cnt[i].y<<" "<<endl;}} else if (p[ro][co]== ' W ') {count++; cout<< "count=" <<count<< "!" <<endl;if (co==1) {cnt[++i].x=ro;cnt[i].y=co;break;} else{cnt[++i].x=ro;cnt[i].y=co;co--; cout<<cnt[i].x<< "<<cnt[i].y<<" "<<endl;}}} for (int t=1;t<=i;t++)//cout<<cnt[t].x<< "" <<cnt[t].y<< "!" <<endl;int flag=0,round,k,j;for (j=1;j<=i;j++) Note that the structure begins with the subscript from 1{for (k=j+1;k<=i;k++) {if (CNT[J].X==CNT[K].X&&CNT[J].Y==CNT[K].Y)//Violent search for the minimum number of cycles round, if found to exist That is, jumping out of the inner layer circle{flag=1;round=k-j; Break;}} if (flag==1)//must add this conditional statement break;} if (flag) cout<<j-1<< "step (s) before a loop of" <<round<< "step (s)" <<endl;else COUT&L t;<count<< "Step (s) to exit" <<ENDL;} int main () {int n,m,k;while (CIN>>N>>M,N+M)//The topic is n,m different time is zero, there is no requirement for K {cin>>k;char ls[11][11];for (int q= 1;q<=n;q++) for (int w=1;w<=m;w++) cin>>ls[q][w];go (ls,n,m,k);} return 0;}
Hangzhou Electric HDU 1035 Robot Motion