哈理工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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

哈理工oj 1079 I can do it【贪心】

哈理工oj 1079,hrbust 1079
  • mengxiang000000
  • mengxiang000000
  • 2016年03月19日 10:14
  • 705

哈理工 oj 1161 Leyni

Leyni Time Limit: 3000 MS Memory Limit: 65536 K Total Submit: 466(94 user...
  • lsgqjh
  • lsgqjh
  • 2015年05月22日 20:25
  • 853

哈理工oj 1946 你猜

你猜 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 1626(1007 user...
  • eletroram
  • eletroram
  • 2017年10月07日 12:09
  • 653

哈理工hrbust OJ 2225 解题报告 【递推】

粉刷栅栏 Time Limit: 500 MS Memory Limit: 32768 K Total Submit: 58(19 users) Total Accepted:...
  • qq_33184171
  • qq_33184171
  • 2015年12月22日 10:43
  • 487

哈理工OJ 1710 a + b = c(尺取法)

题目链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1710a + b = c Time Li...
  • qq_32866009
  • qq_32866009
  • 2017年04月12日 20:59
  • 389

hrbust 1286/哈理工oj 1286 迷宫与宝藏【较难bfs】

迷宫与宝藏 Time Limit: 2000 MS Memory Limit: 65536 K Total Submit: 141(26 user...
  • mengxiang000000
  • mengxiang000000
  • 2016年02月07日 16:20
  • 461

哈尔滨理工大学软件学院OJ 1451 经商(大树/ 斐波那契数列)

#include #include #include using namespace std; struct node { long long a[3][3]; }T; const int M...
  • qq_32473657
  • qq_32473657
  • 2016年11月21日 20:53
  • 672

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

hrbust 1020,哈理工oj 1020
  • mengxiang000000
  • mengxiang000000
  • 2016年03月10日 10:27
  • 359

哈理工oj Hrbustacm 1287 数字去重和排序II(STL 或着 hash 拉链法)

水题 STL map也能过,但是为了练习拉链hash /*======================================================================...
  • lsgqjh
  • lsgqjh
  • 2015年08月26日 18:43
  • 851

哈理工 1073 病毒

病毒 Time Limit: 1000 MS Memory Limit: 65536 K Total Submit: 1154(334 users...
  • lsgqjh
  • lsgqjh
  • 2015年05月17日 16:03
  • 774
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:哈理工OJ 1020 Robot Motion(模拟+队列【类似小搜索】)
举报原因:
原因补充:

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