HDU 1035 Robot Motion(水题,模拟)

来源:互联网 发布:淘宝转化率一般是多少 编辑:程序博客网 时间:2024/05/22 04:36

Robot Motion

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


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 5NEESWEWWWESSSNWWWW4 5 1SESWEEESNWNWEENEWSEN0 0
 

Sample Output
10 step(s) to exit3 step(s) before a loop of 8 step(s)
 

Source
Mid-Central USA 1999

题目大意:输入n、m、k,表示n行m列的矩阵,其中N表示往北(上)走,S表示往南(下)走,E表示往东(右)走,W表示往西(左)走,机器人从(0,k)出发,求其走出该矩阵所需要的步数,若是形成了闭合路径,则打印不成环的步数和成环的步数。

这道题是一道很简单的模拟题,但是我用了很长时间。一开始把step初始化为0,当遇到边界问题时一直wa,后来把step初始化为1,并对成环和不成环的情况进行数据处理,终于ac;
存储矩阵时我把整个矩阵往右下平移,这样就空出来了边界,不用再考虑边界问题;
然后就是根据题意模拟即可。

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#define N 22using namespace std;char a[N][N];int flag[N][N];int main(){    int n,m,k;    while(cin>>n>>m>>k){        if(!n&&!m)            break;        memset(a,'0',sizeof(a));//一定记得对数组初始化,我看讨论里面好多人都因为没有初始化wa        memset(flag,0,sizeof(flag));        for(int i=0;i<n;i++){            for(int j=0;j<m;j++){                cin>>a[i+1][j+1];//将矩阵向右下平移            }        }        int step=1,loop=0,temp=0;//初始step为1        int x=1,y=k;        while(a[x][y]!='0'){//当遇到边界时跳出循环            if(a[x][y]=='N'&&!flag[x][y]){                flag[x][y]=step;                x--;            }            else if(a[x][y]=='S'&&!flag[x][y]){                flag[x][y]=step;                x++;            }            else if(a[x][y]=='E'&&!flag[x][y]){                flag[x][y]=step;                y++;            }            else if(a[x][y]=='W'&&!flag[x][y]){                flag[x][y]=step;                y--;            }            else if(flag[x][y]){                step--;//成环时多了一步要减去                loop=step-flag[x][y]+1;//判断环的步数                temp=1;                break;            }            step++;        }        if(temp)            printf("%d step(s) before a loop of %d step(s)\n",step-loop,loop);        else            printf("%d step(s) to exit\n",--step);//已经出界了要将步数减一    }    return 0;}




原创粉丝点击