HDU 1035-Robot Motion

来源:互联网 发布:英雄联盟网络转播权 编辑:程序博客网 时间:2024/06/04 00:27

Robot Motion

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11461    Accepted Submission(s): 5431


题目链接:点击打开链接



Problem 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 



题意:
有个机器人走格子,它是听着指令走的,在一个矩阵里每个格子都有命令,N 代表上,S 代表下,E 代表右,W
代表左,现在给你三个数,分别是矩阵的行数和列数,以及机器人最初在第一行的第几个位置,机器人会执行一系列命令,然后会有两种情况,第一种情况是机器人走出格子,第二种情况是机器人走入循环,不断执行循环里的命令。如果走出循环就输出用了多少步,如果有循环,那么输出在进入循环之前的步数和循环里的步数。

分析:
看这种题,第一想到的就是 bfs ,但是写的过程中发现太麻烦了,直接用一个标记数组  vis 就解决了。

#include<stdio.h>#include<iostream>#include<string>#include<string.h>using namespace std;int main(){    int n,m,k;    while(~scanf("%d%d%d",&n,&m,&k)&&(n+m+k))    {        k--;        char s[15][15],vis[15][15];        int x=0,time=0;        memset(vis,0,sizeof(vis));///初始化将vis数组刷为0        getchar();        for(int i=0;i<n;i++)///输入字符的矩阵,注意回车        {            for(int j=0;j<m;j++)            {                scanf("%c",&s[i][j]);            }            getchar();        }        while(1)        {            time++;            if(s[x][k]=='S'&&!vis[x][k])            {///如果该点是S并且未走过该点,将步数存入该点的vis数组,向下走                vis[x][k]=time;                x++;            }            else if(s[x][k]=='W'&&!vis[x][k])            {///同理                vis[x][k]=time;                k--;            }            else if(s[x][k]=='E'&&!vis[x][k])            {///同理                vis[x][k]=time;                k++;            }            else if(s[x][k]=='N'&&!vis[x][k])            {///同理                vis[x][k]=time;                x--;            }            if(x<0||x==n||k<0||k==m)            {///如果走出边界,就输出步数                printf("%d step(s) to exit\n",time);                break;            }            else if(vis[x][k])            {///如果走到被标记的点,说明成环了                printf("%d step(s) before a loop of %d step(s)\n",vis[x][k]-1,time+1-vis[x][k]);                break;            }        }    }    return 0;}