Robot Motion
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 12845 |
|
Accepted: 6234 |
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)
Source
Mid-Central USA 1999
Original title Link: http://poj.org/problem?id=1573
Test instructions: There is a n*m area, the robot from the first row of the column entered, the region all by ' N ', ' S ', ' W ', ' E ', go to an area of the time can only follow the direction specified by the region of the next step, ask you whether the robot can walk out of the area, if not, enter the number of steps
Does DFS or BFS have any effect on this problem?
Yes, yes, BFS 0ms!!. DFS ms!!!
Search I'm a slag, look at two to the data, POJ.
BFS AC Code:
#include <iostream> #include <cstdio> #include <cstring> #include <queue>using namespace std; struct node{int x, y; Node (int x=0,int y=0): X (x), Y (y) {}; The notation of x};char a[15][15];bool vis[15][15];int step[15][15];int n,m,s;void BFS () {queue<node>q; memset (vis,false,sizeof (VIS)); step[0][s-1]=0; Q.push (Node (0,s-1)); int stepp=-1; while (!q.empty ()) {node Now=q.front (); Q.pop (); stepp++; if (now.x<0| | now.x>=n| | now.y<0| | now.y>=m) {printf ("%d step (s) to exit\n", Stepp); return; } if (Vis[now.x][now.y]) {printf ("%d step (s) before a loop of%d step (s) \ n", step[now.x][now.y],st EPP-STEP[NOW.X][NOW.Y]); return; } vis[now.x][now.y]=true; Step[now.x][now.y]=stepp; if (a[now.x][now.y]== ' S ') {Q.push (node (NOW.X+1,NOW.Y)); } else if (a[now.x][now.y]== ' N ') {Q.pusH (node (NOW.X-1,NOW.Y)); } else if (a[now.x][now.y]== ' W ') {Q.push (node (now.x,now.y-1)); } else if (a[now.x][now.y]== ' E ') {Q.push (node (now.x,now.y+1)); }}}int Main () {while (cin>>n>>m>>s,n,m,s) {for (int i=0; i<n; i++) cin> >a[i]; BFS (); } return 0;} /**3 3 1swwssneen*/
DFS AC code: 15MS
#include <iostream> #include <cstdio> #include <cstring>using namespace Std;char a[15][15];bool vis[ 15][15];int step[15][15];int n,m,s,stepp;void DFS (int x,int y,int t) {stepp++; if (x<0| | x>=n| | y<0| | y>=m) {printf ("%d step (s) to exit\n", Stepp); return; } if (Vis[x][y]) {printf ("%d step (s) before a loop of%d step (s) \ n", Step[x][y],stepp-step[x][y]); return; } vis[x][y]=true; Step[x][y]=stepp; int xx,yy; if (a[x][y]== ' S ') {//x+=1; xx=x+1; Yy=y; } else if (a[x][y]== ' N ') {//x-=1 xx=x-1; Yy=y; } else if (a[x][y]== ' W ') {//y-=1; Xx=x; Yy=y-1; } else if (a[x][y]== ' E ') {//y+=1; Xx=x; yy=y+1; } DFS (xx,yy,stepp+1);} int main () {while (cin>>n>>m>>s,n,m,s) {for (int i=0; i<n; i++) cin>>a[i ]; Stepp=-1; memset (vis,false,sizeof (VIS)); DFS (0,s-1,0); } return 0;}
POJ 1573 Robot Motion "is search, do not tangle is DFS or BFS"