Robot Motion
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 11324   Accepted: 5512

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)

突然爱上了模拟题,要是所有题都像这样的题目只需模拟一下走的方向,而不考虑算法的该有多好。。。哈哈

从头开始走,记录第几步走在了哪一个格子上,如果要走的格子大于0了,说明走重复了。这题比较水。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; enum{ E,N,W,S };
int map_f[20][20];
char call[20][20];
int move_x[5]={0,-1,0,1};
int move_y[5]={1,0,-1,0};
int X,Y,start,i,cur_x,cur_y,flag; void solve()
{
int temp; cur_x = 1;
cur_y = start;
map_f[cur_x][cur_y]=1; while(1)
{
if(flag)
break;
if(call[cur_x][cur_y]=='E')
{
temp=map_f[cur_x][cur_y];
cur_x = cur_x + move_x[E];
cur_y = cur_y + move_y[E]; if(cur_x<=0 || cur_y<=0 || cur_x>X || cur_y>Y)
{
flag=1;
cout<<temp<<" step(s) to exit"<<endl;
}
else if(map_f[cur_x][cur_y])
{
flag=1;
cout<<map_f[cur_x][cur_y]-1<<" step(s) before a loop of "<<temp-map_f[cur_x][cur_y]+1<<" step(s)"<<endl;
}
else
{
map_f[cur_x][cur_y]=temp+1;
}
}
else if(call[cur_x][cur_y]=='N')
{
temp=map_f[cur_x][cur_y];
cur_x = cur_x + move_x[N];
cur_y = cur_y + move_y[N]; if(cur_x<=0 || cur_y<=0 || cur_x>X || cur_y>Y)
{
flag=1;
cout<<temp<<" step(s) to exit"<<endl;
}
else if(map_f[cur_x][cur_y])
{
flag=1;
cout<<map_f[cur_x][cur_y]-1<<" step(s) before a loop of "<<temp-map_f[cur_x][cur_y]+1<<" step(s)"<<endl;
}
else
{
map_f[cur_x][cur_y]=temp+1;
}
}
else if(call[cur_x][cur_y]=='W')
{
temp=map_f[cur_x][cur_y];
cur_x = cur_x + move_x[W];
cur_y = cur_y + move_y[W]; if(cur_x<=0 || cur_y<=0 || cur_x>X || cur_y>Y)
{
flag=1;
cout<<temp<<" step(s) to exit"<<endl;
}
else if(map_f[cur_x][cur_y])
{
flag=1;
cout<<map_f[cur_x][cur_y]-1<<" step(s) before a loop of "<<temp-map_f[cur_x][cur_y]+1<<" step(s)"<<endl;
}
else
{
map_f[cur_x][cur_y]=temp+1;
}
}
else if(call[cur_x][cur_y]=='S')
{
temp=map_f[cur_x][cur_y];
cur_x = cur_x + move_x[S];
cur_y = cur_y + move_y[S]; if(cur_x<=0 || cur_y<=0 || cur_x>X || cur_y>Y)
{
flag=1;
cout<<temp<<" step(s) to exit"<<endl;
}
else if(map_f[cur_x][cur_y])
{
flag=1;
cout<<map_f[cur_x][cur_y]-1<<" step(s) before a loop of "<<temp-map_f[cur_x][cur_y]+1<<" step(s)"<<endl;
}
else
{
map_f[cur_x][cur_y]=temp+1;
}
}
} } int main()
{ while(cin>>X>>Y>>start)
{
if(!(X+Y+start))
break; memset(map_f,0,sizeof(map_f));
flag=0; for(i=1;i<=X;i++)
{
cin>>call[i]+1;
}
solve();
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1573:Robot Motion的更多相关文章

  1. 【POJ - 1573】Robot Motion

    -->Robot Motion 直接中文 Descriptions: 样例1 样例2 有一个N*M的区域,机器人从第一行的第几列进入,该区域全部由'N' , 'S' , 'W' , 'E' ,走 ...

  2. 模拟 POJ 1573 Robot Motion

    题目地址:http://poj.org/problem?id=1573 /* 题意:给定地图和起始位置,robot(上下左右)一步一步去走,问走出地图的步数 如果是死循环,输出走进死循环之前的步数和死 ...

  3. POJ 1573 Robot Motion(BFS)

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12856   Accepted: 6240 Des ...

  4. Poj OpenJudge 百练 1573 Robot Motion

    1.Link: http://poj.org/problem?id=1573 http://bailian.openjudge.cn/practice/1573/ 2.Content: Robot M ...

  5. POJ 1573 Robot Motion(模拟)

    题目代号:POJ 1573 题目链接:http://poj.org/problem?id=1573 Language: Default Robot Motion Time Limit: 1000MS ...

  6. Robot Motion 分类: POJ 2015-06-29 13:45 11人阅读 评论(0) 收藏

    Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11262 Accepted: 5482 Descrip ...

  7. POJ 1573 Robot Motion

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12978   Accepted: 6290 Des ...

  8. poj 1573 Robot Motion【模拟题 写个while循环一直到机器人跳出来】

                                                                                                         ...

  9. poj1573 Robot Motion

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12507   Accepted: 6070 Des ...

随机推荐

  1. 三、java基础-方法含义_重载_递归

    1.java中方法: 方法的含义: 就是一个代码片段,可以完后某个特定的功能,可以重复利用: 定义方法的语法: [方法的修饰符列表]   方法的返回值类型   方法名{    java语句; } 注意 ...

  2. SpringCloud学习之Zuul路由转发、拦截和熔断处理(七)

    Spring Cloud Zuul 服务网关是微服务架构中一个不可或缺的部分.通过服务网关统一向外系统提供REST API的过程中,除了具备服务路由.均衡负载功能之外,它还具备了权限控制等功能. Sp ...

  3. 在Gridview控件中根据Field Name来取得对应列索引

    下面方法,只能在Gridview的BoundField进行操作,而在TemplateField模版中去找的话,就无能为力了,因TemplateField模版没有DataField属性.  public ...

  4. 「CF1039D」You Are Given a Tree

    传送门 Luogu 解题思路 整体二分. 的确是很难看出来,但是你可以发现输出的答案都是一些可以被看作是关键字处于 \([1, n]\) 的询问,而答案的范围又很显然是 \([0, n]\),这不就刚 ...

  5. 前端学习笔记系列一:14 vue3.X中alias的配置

    第一步: 第二步: // vue.config.js module.exports = { configureWebpack: { resolve: { alias: { 'assets': '@/a ...

  6. 搜索await page.waitForSelector(allResultsSelector);

    /** * Copyright 2017 Google Inc. All rights reserved. * * Licensed under the Apache License, Version ...

  7. 【QSBOJ】组成三位数

    题目描述 将1,2,…,9共9个数分成三组,分别组成三个三位数,且使这三个三位数构成1:2:3的比例,试求出所有满足条件的三个三位数. 例如:三个三位数192,384,576满足以上条件. 输出 输出 ...

  8. (转)浅谈 Linux 内核无线子系统

    前言 Linux 内核是如何实现无线网络接口呢?数据包是通过怎样的方式被发送和接收呢? 刚开始工作接触 Linux 无线网络时,我曾迷失在浩瀚的基础代码中,寻找具有介绍性的材料来回答如上面提到的那些高 ...

  9. Spring源码深度解析-《源码构建》

    1.gradle构建eclipse项目时,gradle-5.0版本构建失败,gradle-3.3构建成功!Why 2.导入spring-framework-3.2.x/spring-beans之前先导 ...

  10. 基于vue-router的移动端网页的路由管理

    本篇代码示例:github 前提:不关注移动端浏览器的前进事件 涵盖功能: 1,管理路由的历史记录 2,切页动画的实现 3,处理流程类页面的回退事件 描述:    流程类页面的回退事件的解释: 以注册 ...