POJ1573 Robot Motion(模拟)

来源:互联网 发布:淘宝血滴子买家秀 编辑:程序博客网 时间:2024/05/20 01:12

Robot Motion

Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 11082 Accepted: 5388

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 0

Sample Output

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

题目大意:

机器人跟着指令走,指令有W,N,S,E分别代表西,北,南,东,问机器人是否能走出去(即走出边界),抑或者是走不出去一直绕圈子,如果出得去就输出 n step(s) to exit,否则就输出 n steps(s) before a loop of m step(s),n代表没有循环的那几步,m代表一次循环的步数。


解题思路:

如果机器人能走出去,也就是纵坐标或横坐标越界,那么就是第一种输出的情况,否则就是第二种情况,本题我采用的是队列,根据指令模拟每一步,每次统计步数,如果走得出去那么统计的步数就是答案,如果走不出去一直绕,那么可以第一次我们标记为走过的点,也就是第一次重复走到的点,因为经过该点,机器人必定走不出去。然后用总的统计步数 - 从头走到该点的步数,就是一次循环要的步数,然后就可以输出答案。


代码如下:

#include<iostream>#include<queue>#include<cstring>#include<cstdio>using namespace std;const int maxn = 400;char map[maxn][maxn];int vis[maxn][maxn];int dir[4][2] = {-1,0,1,0,0,1,0,-1};  //分别代表北,南,东,西struct point{    int x,y;};void turn(int m,int n,int start){    int cnt,cnt1,i;    queue<point> que;    point p;    p.x = 0;    p.y = start-1;    que.push(p);    cnt = 0;    cnt1 = 0;    int pos1,pos2;    int a[maxn],b[maxn];    int cnt3;    cnt3 = 0;    while(!que.empty())    {        p = que.front();        a[cnt3] = p.x;        b[cnt3] = p.y;        cnt3++;        que.pop();        if(p.x < 0 || p.y < 0 || p.x > m-1 || p.y > n-1) //越界的情况,第一种情况,代表可以走出去        {            cout<<cnt<<" step(s) to exit"<<endl;            break;        }        if(map[p.x][p.y] == 'W' && vis[p.x][p.y] == 0)  //模拟每种情况        {            if(vis[p.x][p.y] == 0)            {                //cout<<"W:"<<p.x<<" "<<p.y<<endl;                vis[p.x][p.y] = 1;                p.x = p.x + dir[3][0];                p.y = p.y + dir[3][1];                que.push(p);                cnt++;              }            else            {                p.x = p.x + dir[3][0];                p.y = p.y + dir[3][1];            }        }        else if(map[p.x][p.y] == 'S' && vis[p.x][p.y] == 0)        {            if(vis[p.x][p.y] == 0)            {                //cout<<"S:"<<p.x<<" "<<p.y<<endl;                vis[p.x][p.y] = 1;                p.x = p.x + dir[1][0];                p.y = p.y + dir[1][1];                que.push(p);                cnt++;            }            else            {                p.x = p.x + dir[3][0];                p.y = p.y + dir[3][1];            }        }        else if(map[p.x][p.y] == 'E' && vis[p.x][p.y] == 0)        {            if(vis[p.x][p.y] == 0)            {                //cout<<"E:"<<p.x<<" "<<p.y<<endl;                vis[p.x][p.y] = 1;                p.x = p.x + dir[2][0];                p.y = p.y + dir[2][1];                que.push(p);                cnt++;            }            else            {                p.x = p.x + dir[3][0];                p.y = p.y + dir[3][1];            }        }        else if(map[p.x][p.y] == 'N' && vis[p.x][p.y] == 0)        {            if(vis[p.x][p.y] == 0)            {                //cout<<"N:"<<p.x<<" "<<p.y<<endl;                vis[p.x][p.y] = 1;                p.x = p.x + dir[0][0];                p.y = p.y + dir[0][1];                que.push(p);                cnt++;            }            else            {                p.x = p.x + dir[3][0];                p.y = p.y + dir[3][1];            }        }    }    //cout<<"cnt:"<<cnt<<endl;    //cout<<p.x<<" "<<p.y<<endl;    int pos3;    for(i=0;i<cnt3;i++)    {        if(p.x == a[i] && p.y == b[i])   //求第一次重复走过的点        {            pos3 = i;            break;        }    }    if(cnt-pos3 != 0)        cout<<pos3<<" step(s) before a loop of "<<cnt-pos3<<" step(s)"<<endl;}int main(){    int m,n,k,i,j;    //freopen("111","r",stdin);    while(cin>>m>>n>>k && !(m == 0 && n == 0 && k == 0))    {        for(i=0;i<m;i++)        {            for(j=0;j<n;j++)            {                cin>>map[i][j];            }        }        memset(vis,0,sizeof(vis));        turn(m,n,k);    }    return 0;}


0 0
原创粉丝点击