关闭

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

标签: 哈理工OJ
127人阅读 评论(0) 收藏 举报
分类:

题目链接: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;
}
0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:127378次
    • 积分:7039
    • 等级:
    • 排名:第3204名
    • 原创:590篇
    • 转载:1篇
    • 译文:0篇
    • 评论:58条
    最新评论
    Java栏目
    JAVA学习与有关心得