Robot Motion

Time Limit: 1000 MS Memory Limit: 10000 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status] [Discuss]

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.  
  4. using namespace std;
  5.  
  6. int main(void)
  7. {
  8. int row,col,entry;
  9. char grid[][]; //在规定大小的grid外部至少再定义一圈"门槛"以判断Robot是否离开了grid (最大grid为10x10)
  10.  
  11. for(;;)
  12. {
  13. memset(grid,'O',sizeof(grid)); // 'O' 为大写字母O,意为 Out
  14.  
  15. /*Input*/
  16.  
  17. int i,j;
  18.  
  19. cin>>row>>col>>entry;
  20. if(!(row && col && entry))break;
  21.  
  22. for(i=;i<=row;i++)
  23. for(j=;j<=col;j++)
  24. cin>>grid[i][j];
  25.  
  26. /*Judge Robot get out of the grid or starts a loop in the grid*/
  27.  
  28. int flag[][]={}; //标记Robot经过某点的次数,当有一点为2则说明Robot陷入了以该点为loop起始点的循环
  29. int count;
  30. int r=;
  31. int c=entry;
  32. for(count=;;count++)
  33. {
  34. flag[r][c]++; //注意顺序,先标记,再位移
  35. if(grid[r][c]=='N') // ↑
  36. r--;
  37. else if(grid[r][c]=='S') // ↓
  38. r++;
  39. else if(grid[r][c]=='W') // ←
  40. c--;
  41. else if(grid[r][c]=='E') // →
  42. c++;
  43. else if(grid[r][c]=='O') // Out
  44. {
  45. cout<<count<<" step(s) to exit"<<endl;
  46. break;
  47. }
  48.  
  49. if(flag[r][c]==) //loop
  50. {
  51. row=r; //标记Robot循环起止点
  52. col=c;
  53. int flg=;
  54. for(r=,c=entry,count=;;count++)
  55. {
  56. if(r==row && c==col && flg==) //注意顺序,先寻找循环点再位移(避免Robot刚进入grid就陷入了循环的情况)
  57. {
  58. cout<<count<<" step(s) before a loop of "; //输出进入循环前的步数
  59. count=;
  60. flg++;
  61. }
  62. if(r==row && c==col && count!= && flg==)
  63. {
  64. cout<<count<<" step(s)"<<endl; //输出循环步数
  65. break;
  66. }
  67. if(grid[r][c]=='N') // ↑
  68. r--;
  69. else if(grid[r][c]=='S') // ↓
  70. r++;
  71. else if(grid[r][c]=='W') // ←
  72. c--;
  73. else if(grid[r][c]=='E') // →
  74. c++;
  75. }
  76. break; //跳出count的for循环,不是跳出if(当然break也不能用于跳出if,这里的说明是为了避免误解)
  77. }
  78. }
  79. }
  80. return ;
  81. }

题意:按照表格的提示走~~~

模拟模拟  太恶心啦

http://blog.csdn.net/lyy289065406/article/details/6645434

他的博客真心棒!!!!!

poj1573模拟的更多相关文章

  1. poj1573 模拟

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11270   Accepted: 5487 Des ...

  2. POJ1573(Robot Motion)--简单模拟+简单dfs

    题目在这里 题意 : 问你按照图中所给的提示走,多少步能走出来??? 其实只要根据这个提示走下去就行了.模拟每一步就OK,因为下一步的操作和上一步一样,所以简单dfs.如果出现loop状态,只要记忆每 ...

  3. poj1573&amp;&amp;hdu1035 Robot Motion(模拟)

    转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接: HDU:pid=1035">http://acm.hd ...

  4. POJ-1573 Robot Motion模拟

    题目链接: https://vjudge.net/problem/POJ-1573 题目大意: 有一个N*M的区域,机器人从第一行的第几列进入,该区域全部由'N' , 'S' , 'W' , 'E' ...

  5. POJ1573 Robot Motion(模拟)

    题目链接. 分析: 很简单的一道题, #include <iostream> #include <cstring> #include <cstdio> #inclu ...

  6. App开发:模拟服务器数据接口 - MockApi

    为了方便app开发过程中,不受服务器接口的限制,便于客户端功能的快速测试,可以在客户端实现一个模拟服务器数据接口的MockApi模块.本篇文章就尝试为使用gradle的android项目设计实现Moc ...

  7. 故障重现, JAVA进程内存不够时突然挂掉模拟

    背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...

  8. Python 爬虫模拟登陆知乎

    在之前写过一篇使用python爬虫爬取电影天堂资源的博客,重点是如何解析页面和提高爬虫的效率.由于电影天堂上的资源获取权限是所有人都一样的,所以不需要进行登录验证操作,写完那篇文章后又花了些时间研究了 ...

  9. HTML 事件(四) 模拟事件操作

    本篇主要介绍HTML DOM中事件的模拟操作. 其他事件文章 1. HTML 事件(一) 事件的介绍 2. HTML 事件(二) 事件的注册与注销 3. HTML 事件(三) 事件流与事件委托 4.  ...

随机推荐

  1. Luogu 2157 [SDOI2009]学校食堂 - 状压dp

    Solution 比较好想的dp, 但是坑不少QAQ, 调半天 由于容忍度 $b_i$<= 7, 所以可以考虑将第$i$个人接下来的$b_i$ 个人作为一个维度记录状态. 于是我们定义数组$f[ ...

  2. WIN7成功安装Qt4.8方法,无需VS支持

    下载地址:http://pan.baidu.com/share/link?shareid=159827&uk=4010603727 安装Qt方法 安装准备:1. qt-win-opensour ...

  3. libpcap 库使用(二)

    参考资料: http://www.tcpdump.org/manpages/pcap.3pcap.html 分类介绍了该lib的函数 Opening a capture handle for read ...

  4. 销售vs技术岗,做技术的方法思考

    销售甚至比技术岗位挣得还多,当然,做技术的比较好的拿到的自然也多. 我在想个问题,技术的天然优势是可以不断地积累,包括写code,写博客,做流程,完善流程,自动化流程,或者把某些工作流程化,自动化,托 ...

  5. 【C#】详解C#委托

    目录结构: contents structure [+] 委托语法 泛型委托 委托链 lambda表达式 揭秘委托 类库中的委托 委托和反射 1.委托语法 本文会详细阐述委托的使用,以及实现,想必读者 ...

  6. c#的装箱和拆箱及值类型和引用类型

    装箱:它允许根据值类型创建一个对象,然后使用对这新对象的一个引用. int i = 5; object o = i; int j = (int)o; 装箱:运行时将在堆上创建一个包含值5的对象(它是一 ...

  7. poj-3667(线段树区间合并)

    题目链接:传送门 参考文章:传送门 思路:线段树区间合并问题,每次查询到满足线段树的区间最左值,然后更新线段树. #include<iostream> #include<cstdio ...

  8. hdu 1540(线段树区间合并)

    题目链接:传送门 参考文章:传送门 题意:n个数字初始连在一条线上,有三种操作, D x表示x号被摧毁: R 表示恢复剩下的通路 Q表示查询标号为x所在的串的最长长度. 思路:线段树的区间合并. #i ...

  9. ACM-ICPC 2018 徐州赛区网络预赛 J Maze Designer(最大生成树,倍增lca)

    https://nanti.jisuanke.com/t/31462 要求在一个矩形中任意选两个点都有唯一的通路,所以不会建多余的墙. 要求满足上述情况下,建墙的费用最小.理解题意后容易想到首先假设全 ...

  10. thinkphp3.2 链接数据库测试

    配置数据库: 在Application/Home/config.php文件中设置: <?php return array( 'DB_TYPE' => 'mysql', // 数据库类型 ' ...