哈理工OJ 1020 Robot Motion(模拟+队列【类似小搜索】)

原创 2016年08月29日 20:44:48

题目链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1020

Robot Motion
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submit: 217(102 users) Total Accepted: 110(94 users) Rating: Special Judge: No
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)

思路:用队列来模拟机器人走的过程就好了,很简单的题目。

下面是AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;

struct node
{
    int x,y,step;
};
char a[18][18];
int n,m,k;

int book[18][18],s[18][18];
void solve(int sx,int sy)
{
    memset(book,0,sizeof(book));
    memset(s,0,sizeof(s));
    queue<node>q;
    node fr,ne;
    fr.x=sx,fr.y=sy;
    fr.step=1;
    q.push(fr);
    book[fr.x][fr.y]=1;
    s[fr.x][fr.y]=1;
    while(!q.empty())
    {
        fr=q.front();
        q.pop();
        if(a[fr.x][fr.y]=='N')
        {
            ne.x=fr.x-1;
            ne.y=fr.y;
            //printf("(%d,%d)\n",ne.x,ne.y);
        }
        if(a[fr.x][fr.y]=='S')
        {
            ne.x=fr.x+1;
            ne.y=fr.y;
            //printf("(%d,%d)\n",ne.x,ne.y);
        }
        if(a[fr.x][fr.y]=='E')
        {
            ne.x=fr.x;
            ne.y=fr.y+1;
            //printf("(%d,%d)\n",ne.x,ne.y);
        }
        if(a[fr.x][fr.y]=='W')
        {
            ne.x=fr.x;
            ne.y=fr.y-1;
            //printf("(%d,%d)\n",ne.x,ne.y);
        }
        ne.step=fr.step+1;
        if(ne.x<0||ne.x>=n||ne.y<0||ne.y>=m)
        {
            printf("%d step(s) to exit\n",ne.step-1);
            break;
        }
        else
        {
            if(book[ne.x][ne.y]==1)
            {
                printf("%d step(s) before a loop of %d step(s)\n",s[ne.x][ne.y]-1,ne.step-s[ne.x][ne.y]);
                break;
            }
            s[ne.x][ne.y]=s[fr.x][fr.y]+1;
            book[ne.x][ne.y]=1;
            q.push(ne);
        }
    }
}

int main()
{
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        if(n==0&&m==0&&k==0)
        {
            break;
        }
        for(int i=0;i<n;i++)
        {
            scanf("%s",&a[i]);
        }
        solve(0,k-1);
    }
    return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

hrbust 哈理工oj 1020 Robot Motion【简单模拟】

hrbust 1020,哈理工oj 1020

哈理工OJ 1176 小陈老师、雪人(排序+优先队列模拟)

题目链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1176小陈老师、雪人 Time Limi...

哈理工 oj——队列列列列!!!!!

队列列列列!!!!! Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 337(14...
  • lsgqjh
  • lsgqjh
  • 2015年04月11日 22:55
  • 582

哈理工oj Touring (最短路 dij算法 邻接表 + 队列 )

Touring Time Limit: 1000 MS Memory Limit: 32767 K Total Submit: 257(46 us...
  • lsgqjh
  • lsgqjh
  • 2015年06月15日 23:53
  • 565

哈理工OJ 1522 子序列的和(单调队列)(dp)

子序列的和 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 289(61 user...

哈理工OJ 1621迷宫问题II(优先队列广搜)

迷宫问题II Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 131(42 use...

哈理工OJ 1580 Tell me the length(模拟+找规律)

Tell me the length Time Limit: 1000 MS Memory Limit: 65536 K Total Submit...

哈理工OJ 2255 镇守府海域·镇守府近海(模拟题目)

题目链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=2255镇守府海域·镇守府近海 Time ...

哈理工oj/hrbust 2122 旅行【dij+优先队列优化+邻接表+map】

hrbust 2122,哈理工oj 2122

哈理工OJ 1031 _OOOO_ Problem(简单模拟问题)

OOOO Problem Time Limit: 1000 MS Memory Limit: 65536 K Total Submit: 288(96 users) Total Accepted:...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:哈理工OJ 1020 Robot Motion(模拟+队列【类似小搜索】)
举报原因:
原因补充:

(最多只允许输入30个字)