Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 11269   Accepted: 5486

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

  1. 3 6 5
  2. NEESWE
  3. WWWESS
  4. SNWWWW
  5. 4 5 1
  6. SESWE
  7. EESNW
  8. NWEEN
  9. EWSEN
  10. 0 0 0

Sample Output

  1. 10 step(s) to exit
  2. 3 step(s) before a loop of 8 step(s)
  1. #include <iostream>
  2. #include<string.h>
  3. using namespace std;
  4.  
  5. int main() {
  6. int row,col,x,y;
  7.  
  8. int pos[][];
  9. char ins[][];
  10. int step;
  11. int flag=-;
  12. cin>>row>>col>>x;
  13. while(row&&col&&x){
  14. memset(pos,,sizeof(int)*);
  15. y=;
  16. step=;
  17. flag=-;
  18. for(int i=;i<row;i++){
  19. for(int j=;j<col;j++){
  20. cin>>ins[i][j];
  21. }
  22. }
  23. pos[y-][x-]=;
  24. for(int i=;i<row*col;i++){
  25. switch(ins[y-][x-]){
  26. case 'N':
  27. y--;
  28. break;
  29. case 'E':
  30. x++;
  31. break;
  32. case 'S':
  33. y++;
  34. break;
  35. case 'W':
  36. x--;
  37. break;
  38. }
  39.  
  40. if(y>row||y<||x>col||x<){
  41. flag=;
  42. break;
  43. }else if(pos[y-][x-]!=){
  44. flag=;
  45. break;
  46. }else{
  47. ++step;
  48. pos[y-][x-]=step;
  49. }
  50.  
  51. }
  52. if(flag==){
  53. cout<<step<<" step(s) to exit"<<endl;
  54. }else{
  55. cout<<(pos[y-][x-]-)<<" step(s) before a loop of "<<(step-pos[y-][x-]+)<<" step(s)"<<endl;
  56. }
  57. cin>>row>>col>>x;
  58. }
  59. return ;
  60. }

Robot Motion - poj 1573的更多相关文章

  1. 模拟 POJ 1573 Robot Motion

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

  2. POJ 1573 Robot Motion(BFS)

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

  3. Poj OpenJudge 百练 1573 Robot Motion

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

  4. POJ 1573 Robot Motion(模拟)

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

  5. POJ 1573 Robot Motion

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

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

                                                                                                         ...

  7. 【POJ - 1573】Robot Motion

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

  8. POJ 1573:Robot Motion

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

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

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

随机推荐

  1. SQL Server 2008 R2 SP3 and SQL Server 2008 SP4 are now available!

    时间 2014-10-02 00:00:00 SQL Server Team Blog   原文  http://blogs.technet.com/b/dataplatforminsider/arc ...

  2. linux命令行翻页

    在linux上面执行命令,若命令太多屏幕显示不完,通过Shift+pageup/pageDown来查看. putty连接linux后执行就不存在这个问题.

  3. 纯CSS实现网站常用的五角星评分和分数展示交互效果

    最近做的一个项目涉及到评分和展示分数的模块,UI设计师也给了几个切好的图片,实现五角星评分方式很多,本质爱折腾的精神和对性能追求以及便于维护的考虑,搜集和尝试了很多方式,最终采用了纯css驱动的实现方 ...

  4. Chrome的开发必备小技巧

    谷歌Chrome,是当前最流行且被众多web开发人员使用的浏览器.最快六周就更新发布一次以及伴随着它不断强大的开发组件,使得Chrome成为你必备的开发工具.例如,在线编辑CSS,console以及d ...

  5. androd 获得wifi列表

    AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xm ...

  6. Object 类中的 equals方法

    1 相等与同一 如果两个对象具有相同的类型以及相同的属性值,则称这两个对象相等.如果两个引用对象指的是同一个对像,则称这两个变量同一.Object类中定义的equals 函数原型为:public bo ...

  7. 为什么输入shutdown -h -t会报错:command not fount

    如果是直接用普通用户($)的身份进行输入[user@localhost ~]$ shutdown -h -t 是不能执行,因为普通用户没有关闭机器的权限. 然而直接使用[user@localhost ...

  8. 2017.7.1 nginx反向代理服务器域名解析配置(已验证可使用)

    下载地址:http://learning.happymmall.com/ 前提:ftpserver已经开启,并且设置为: 1.获得安装文件 2.修改配置文件 2.1 修改conf/nginx.conf ...

  9. Java Servlet 技术简介 from:IBM Dev

    点评:讲的没有太听懂. from: http://www.ibm.com/developerworks/cn/education/java/j-intserv/index.html#resources

  10. Poj 4227 反正切函数的应用

    Description 反正切函数可展开成无穷级数,有例如以下公式 (当中0 <= x <= 1) 公式(1) 使用反正切函数计算PI是一种经常使用的方法.比如,最简单的计算PI的方法: ...