POJ_1573

来源:互联网 发布:netbeans for php下载 编辑:程序博客网 时间:2024/06/07 22:30

Description

A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are

N north (up the page)
S south (down the page)
E east (to the right on the page)
W west (to the left on the page)

For example, suppose the robot starts on the north (top) side of Grid 1 and starts south (down). The path the robot follows is shown. The robot goes through 10 instructions in the grid before leaving the grid.

Compare what happens in Grid 2: the robot goes through 3 instructions only once, and then starts a loop through 8 instructions, and never exits.

You are to write a program that determines how long it takes a robot to get out of the grid or how the robot loops around.
Input

There will be one or more grids for robots to navigate. The data for each is in the following form. On the first line are three integers separated by blanks: the number of rows in the grid, the number of columns in the grid, and the number of the column in which the robot enters from the north. The possible entry columns are numbered starting with one at the left. Then come the rows of the direction instructions. Each grid will have at least one and at most 10 rows and columns of instructions. The lines of instructions contain only 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 in the input there is one 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 follows the instructions on a certain number of locations once, and then the instructions on some number of locations repeatedly. The sample input below corresponds to the two grids above and illustrates the two forms of output. The word “step” is always immediately followed by “(s)” whether or not the number before it is 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)
就是简单的模拟,我错了几次,是边界条件没有初始化好.
代码:

#include <iostream>#include <algorithm>#include <cstdio>using namespace std;char map[15][15];//整个机器人要走的地图int arr[15][15];//用来标记机器人的路线,1表示没有经过,-1表示经过,0表示已经出来了,处于边界的外面了int brr[15][15];//用来表示机器人从开始位置到第i行第j列走到步数int n,m,beg;//n,m分别表示行和列,beg表示机器人是从第一行的哪一列开始的//dfs来模拟机器人的路线void dfs(int x,int y,int step){    if(arr[x][y] == 0)    {        cout << step << " step(s) to exit" <<endl;        return ;    }    else if(arr[x][y] == -1)    {        cout<< brr[x][y] << " step(s) before a loop of " << step-brr[x][y] << " step(s)" <<endl;        return ;    }    else if (arr[x][y] == 1)    {        brr[x][y] = step;        arr[x][y] = -1;        if(map[x][y] == 'W')            dfs(x,y-1,step+1);              else if(map[x][y] == 'S')            dfs(x+1,y,step+1);        else if(map[x][y] == 'E')            dfs(x,y+1,step+1);        else if(map[x][y] == 'N')            dfs(x-1,y,step+1);    }}int main(){    while(cin>>n>>m>>beg)    {        if(n==0 || m == 0 || beg == 0)            break;        //初始化地图        for(int i=1;i<=n;i++)            for(int j=1;j<=m;j++)                cin>>map[i][j];        for(int i=1;i<=n;i++)            for(int j=1;j<=m;j++)                arr[i][j]=1;//地图的初始化都是1,表示机器人没有经过,机器人经过则变成了-1        for(int i=n+1,j=0;j<=m+1;j++)            arr[i][j]=0;        for(int i=m+1,j=0;j<=n+1;j++)            arr[j][i]=0;        dfs(1,beg,0);        /*        for(int i=0;i<=n;i++)        {            for(int j=0;j<=m;j++)                cout<<brr[i][j];            cout<<endl;        }        */    }    return 0;}
原创粉丝点击