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

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

简单的方位移动型模拟题,要用方位数组。移动模拟过程用搜索。

15777856

  ksq2013 1573 Accepted 700K 0MS G++ 1074B 2016-07-21 22:16:18
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. using namespace std;
  5. const int mx[]={0,+1,0,-1};
  6. const int my[]={+1,0,-1,0};//ESWN;
  7. bool vis[11][11];
  8. int n,m,s,mp[11][11],stp[11][11];
  9. void dfs(int x,int y,int cnt)
  10. {
  11. if(x<1||x>n||y<1||y>m){
  12. printf("%d step(s) to exit\n",cnt);
  13. return;
  14. }
  15. if(vis[x][y]){
  16. printf("%d step(s) before a loop of %d step(s)\n",stp[x][y],cnt-stp[x][y]);
  17. return;
  18. }
  19. vis[x][y]=1;
  20. stp[x][y]=cnt;
  21. int tmp=mp[x][y];
  22. dfs(x+mx[tmp],y+my[tmp],cnt+1);
  23. }
  24. int main()
  25. {
  26. while(scanf("%d%d%d",&n,&m,&s)&&s){
  27. memset(mp,0,sizeof(mp));
  28. memset(vis,0,sizeof(vis));
  29. for(int x=1;x<=n;x++){
  30. for(int y=1;y<=m;y++){
  31. char ch;
  32. cin>>ch;
  33. switch(ch){
  34. case 'E':mp[x][y]=0;break;
  35. case 'S':mp[x][y]=1;break;
  36. case 'W':mp[x][y]=2;break;
  37. case 'N':mp[x][y]=3;break;
  38. }
  39. }
  40. }
  41. dfs(1,s,0);
  42. }
  43. return 0;
  44. }

poj1573 Robot Motion的更多相关文章

  1. POJ1573——Robot Motion

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

  2. POJ-1573 Robot Motion模拟

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

  3. poj1573 Robot Motion(DFS)

    题目链接 http://poj.org/problem?id=1573 题意 一个机器人在给定的迷宫中行走,在迷宫中的特定位置只能按照特定的方向行走,有两种情况:①机器人按照方向序列走出迷宫,这时输出 ...

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

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

  5. POJ1573 Robot Motion(模拟)

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

  6. 【POJ - 1573】Robot Motion

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

  7. Robot Motion(imitate)

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

  8. 模拟 POJ 1573 Robot Motion

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

  9. POJ 1573 Robot Motion(BFS)

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

随机推荐

  1. git 删除远程源,新增加源

    git remote remove origin git remote add origin git@XXXX

  2. RxJava 和 RxAndroid 四(RxBinding的使用)

    对Rxjava不熟悉的同学可以先看我之前写的几篇文章 RxJava 和 RxAndroid 一 (基础) RxJava 和 RxAndroid 二(操作符的使用) RxJava 和 RxAndroid ...

  3. UIView属性

    UIView属性 1.alpha 设置视图的透明度.默认为1. // 完全透明 view.alpha = 0; // 不透明 view.alpha = 1; 2.clipsToBounds // 默认 ...

  4. UIscrollView和UIPageControl的循环滚动

    因为昨天在网上找了很久,很多只能实现向右滚动,而且一张图一个imageview ,感觉工作量很可怕啊 ,  下面的例子就是不论你多少图 , 只和我代码里面的几个数值有关,  只需要修改分页和循环i的最 ...

  5. 【好书摘要】性能优化中CPU、内存、磁盘IO、网络性能的依赖

    系统优化是一项复杂.繁琐.长期的工作,优化前需要监测.采集.测试.评估,优化后也需要测试.采集.评估.监测,而且是一个长期和持续的过程,不 是说现在优化了,测试了,以后就可以一劳永逸了,也不是说书本上 ...

  6. 数据表格,查询、导出共用一个form表单,实现文件流方式下载

    在开发中遇到问题是这样的: 在维护老的管理系统的过程中,老板说让加导出功能:项目中,查询的筛选条件是用的表单提交的方式写的. 解决方案有两种: 一.用ajax方式导出 var array = $('# ...

  7. SSRS 的简单使用(二)

    经过上一篇的初始,我们已经做好了报表的准备工作,接下来我们进行报表的展示和其他一下操作,并且给出一些使用RS的方法方便大家日后能灵活使用. 步骤:        1.首先拖拽表格等进入到设计模板 点击 ...

  8. spring annotation简述

    一.Annotation基本概念 Annotation是jdk5以后出现的新特性,在jdk中,其内置了许多自己的Annotation,例如@Override,@SuppresWarning,@Depr ...

  9. Windows服务调试小结(附Demo)

    本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 阅读目录 介绍 搭建环境 调试方式 Demo下载 本文版权归mephisto和博客园共有,欢迎转载,但须 ...

  10. JNA 如何 加载多个 存在依赖的 DLL 库

    JNA 的出现,极大的简化了原有的 JNI 技术.下面是JNA github地址:https://github.com/java-native-access/jna 1. 简单的一个例子: /** S ...