Robot Motion
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 11065   Accepted: 5378

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)

Source

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<iostream>
  4. using namespace std;
  5. int row , col , o ;
  6. char map[][] ;
  7. int a[][] ;
  8. int x , y ;
  9.  
  10. void loop (char dir)
  11. {
  12. switch (dir)
  13. {
  14. case 'N' : printf ("%d step(s) before a loop of %d step(s)\n" , a[x][y] - , a[x + ][y] - a[x][y] + ) ; break ;
  15. case 'E' : printf ("%d step(s) before a loop of %d step(s)\n" , a[x][y] - , a[x][y - ] - a[x][y] + ) ; break ;
  16. case 'S' : printf ("%d step(s) before a loop of %d step(s)\n" , a[x][y] - , a[x - ][y] - a[x][y] + ) ; break ;
  17. case 'W' : printf ("%d step(s) before a loop of %d step(s)\n" , a[x][y] - , a[x][y + ] - a[x][y] + ) ; break ;
  18. }
  19. }
  20. void solve ()
  21. {
  22. x = , y = o ;
  23. bool flag = ;
  24. char temp ;
  25. a[x][y] = ;
  26. while (x >= && x <= row && y >= && y <= col) {
  27. switch (map[x][y])
  28. {
  29. case 'N' : x-- ; if (a[x][y]) flag = ;
  30. if (!flag)
  31. a[x][y] = a[x + ][y] + ;
  32. else
  33. temp = 'N' ; break ;
  34. case 'E' : y++ ; if (a[x][y]) flag = ;
  35. if (!flag)
  36. a[x][y] = a[x][y - ] + ;
  37. else
  38. temp = 'E' ; break ;
  39. case 'S' : x++ ; if (a[x][y]) flag = ;
  40. if (!flag)
  41. a[x][y] = a[x - ][y] + ;
  42. else
  43. temp = 'S' ; break ;
  44. case 'W' : y-- ; if (a[x][y]) flag = ;
  45. if (!flag)
  46. a[x][y] = a[x][y + ] + ;
  47. else
  48. temp = 'W' ; break ;
  49. }
  50. if (flag) {
  51. loop (temp) ;
  52. break ;
  53. }
  54. }
  55. if (!flag)
  56. printf ("%d step(s) to exit\n" , a[x][y] - ) ;
  57. }
  58. int main ()
  59. {
  60. // freopen ("a.txt" , "r" , stdin) ;
  61. while (~ scanf ("%d%d%d" , &row , &col , &o)) {
  62. if (row == && col == && o == )
  63. break ;
  64. memset (a , , sizeof(a)) ;
  65. for (int i = ; i <= row ; i++)
  66. for (int j = ; j <= col ; j++)
  67. cin >> map[i][j] ;
  68.  
  69. solve () ;
  70. /* for (int i = 1 ; i <= row ; i++) {
  71. for (int j = 1 ; j <= col ; j++) {
  72. printf ("%d " , a[i][j]) ;
  73. }
  74. puts ("") ;
  75. }
  76. printf ("\n\n") ;*/
  77. }
  78. return ;
  79. }

Robot Motion(imitate)的更多相关文章

  1. poj1573 Robot Motion

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

  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. Robot Motion 分类: POJ 2015-06-29 13:45 11人阅读 评论(0) 收藏

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

  5. POJ 1573 Robot Motion

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

  6. Poj OpenJudge 百练 1573 Robot Motion

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

  7. POJ1573——Robot Motion

    Robot Motion Description A robot has been programmed to follow the instructions in its path. Instruc ...

  8. hdoj 1035 Robot Motion

    Robot Motion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  9. HDU-1035 Robot Motion

    http://acm.hdu.edu.cn/showproblem.php?pid=1035 Robot Motion Time Limit: 2000/1000 MS (Java/Others)   ...

随机推荐

  1. 连贯接口(fluent interface)的Java实现及应用。

    几年前在单元测试时使用mockito和junit(使用hamcrest提供的比较方法)的时候,就用到过这样类似的语法: mockito: when(mock.someMethod("some ...

  2. 学习笔记——Maven实战(九)打包的技巧

    “打包“这个词听起来比较土,比较正式的说法应该是”构建项目软件包“,具体说就是将项目中的各种文件,比如源代码.编译生成的字节码.配置文件.文档,按照规范的格式生成归档,最常见的当然就是JAR包和WAR ...

  3. 转 Windows server 2008 搭建VPN服务

    VPN英文全称是“Virtual Private Network”,就是“虚拟专用网络”.   虚拟专用网络就是一种虚拟出来的企业内部专用线路.这条隧道可以对数据进行几倍加密达到安全使用互联网的目的. ...

  4. brew-cask 之本地更新 node

    本文同步自我的个人博客:http://www.52cik.com/2015/11/04/brew-cask-local.html 今天 Node v4.2.2 (LTS) 发布,什么是 LTS 呢,百 ...

  5. grootJS ui控件定义

    index13.html <html><head> <title>ui控件定义</title> <script src="jquery- ...

  6. Orchard 刨析:Caching

    关于Orchard中的Caching组件已经有一些文章做了介绍,为了系列的完整性会再次对Caching组件进行一次介绍. 缓存的使用 在Orchard看到如下一段代码: 可以看到使用缓存的方法Get而 ...

  7. css渐变

    常这样去定义一个渐变的div: 渐变1:(双色) background: -moz-linear-gradient(top, #456d6c %, #32b66a %); %,#456d6c), co ...

  8. [USACO2005][poj2229]Sumsets(递推)

    http://poj.org/problem?id=2229 分析: 显然的递推 若n为奇数,那么肯定是在n-1的基础上前面每个数+1,即f[n]=f[n-1] 若n为偶数 当第一位数字是1的时候,等 ...

  9. Struts2 标签分类

  10. java 关键字 transient

    一个对象实现了Serilizable 接口,该对象就可以被序列化. 然而在实际开发工程中,我们会遇到,这个类的有些属性不需要序列化,比如包含用户的敏感信息(如密码),为了安全起见,不希望在网络操作(主 ...