【POJ - 1573】 Robot Motion (dfs)

-->Robot Motion

Direct Chinese

Descriptions:

Sample 1

Sample 2

N * M has a region, from the robot into the columns of the first row, all the region of 'N', 'S', 'W', 'E', went to a region only in accordance with the time direction of the specified area to the next step, you ask whether the robot out of the film area, if not, the size of the input start circling the number of steps and turns. Operating instructions are as follows: 

N up 
S down 
E rightward 
W leftward 

e.g., starting from the assumption that the robot 1 grid north (top) side, starting from the south (lower). The path followed by the robot shown in FIG. Before leaving the grid, the robot 10 execute instructions in the grid. 

Comparison of the grid in the event of 2: robot only by three instructions, and then start the cycle by eight instructions, and never quit. 

You will write a program to determine the robot to leave or time required for the robot how to mesh cycle. 

Input

The first row is an integer of three separated by spaces: the number of rows in the grid, the number of columns in the grid and the number of columns to enter the robot from the north. Instruction then the row direction. Each grid will have at least one row, up to 10 rows and some instructions. Command line contains only characters N, S, E, or W, with no spaces. End of the input is indicated by the line contains 000.

Output

For each input line of output. Robot follow certain amount of order and exit grid on any of the four sides, otherwise the robot to follow the instructions on the location of a number, and then repeat the instructions on the instruction certain location. The following example corresponds to the above two input meshes, and shows the two forms of output. After the word "step" followed by "(s)", regardless of whether the latter figure 1.

Sample Input

3 6 5 
NEESWE 
WWWESS 
SNWWWW 
4 5 1 
SESWE 
EESNW 
NWEEN 
EWSEN 
0 0 0

Sample Output

10 step(s) to exit
3 step(s) before a loop of 8 step(s)

Topic Link

https://vjudge.net/problem/POJ-1573

 

Nothing to say, dfs search directly on the line

 

AC Code

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM (X, Y) Memset (X, Y, the sizeof (X))
 #define MAXN 25
 the using  namespace STD;
 int R & lt, C, s; // rows, columns, starting from the first row s 
char MP [MAXN] [MAXN]; // map 
int VIS [MAXN] [MAXN]; // tag is passed 
int STEP [MAXN] [MAXN]; // (X, Y) is the first short 

// (X, Y) under the letter flag to step (x, y) is the first step has steps 
void DFS ( int X, int Y, char OP, int Start) 
{ 
    // out of the labyrinth 
    IF (MP [X] [Y] == ' X- ' | | x <1&&y<1&&x>r&&y>c)
        cout<<start<<" step(s) to exit"<<endl;
    //(x,y)没走过
    else if(!vis[x][y])
    {
        vis[x][y]=1;
        step[x][y]=start+1;
        //四种走法
        if(op=='N')
            return dfs(x-1,y,mp[x-1][y],step[x][y]);
        if(op=='S')
            return dfs(x+1,y,mp[x+1][y],step[x][y]);
        if(op=='E')
            return dfs(x,y+1,mp[x][y+1],step[x][y]);
        if(op=='W')
            return dfs(x,y-1,mp[x][y-1],step[x][y]);
    }
    //(x,y)走过,输出即可
    else if(vis)
        cout<<step[x][y]-1<<" step(s) before a loop of "<<start+1-step[x][y]<<" step(s)"<<endl;
}
int main()
{
    while(cin>>r>>c>>s,r+c+s)
    {
        //初始化
        MEM(mp,'X');
        MEM(vis,0);
        MEM(step,0);
        for(int i=1; i<=r; i++)
            for(int j=1; j<=c; j++)
                cin>>mp[i][j];
        step[1][s]=0;
        dfs(1,s,mp[1][s],step[1][s]);
    }
}

 

Guess you like

Origin www.cnblogs.com/sky-stars/p/11204430.html