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

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 <cstdio>
  2. #include <cstring>
  3.  
  4. char s[15][15];
  5. int n, m , start;
  6. int dis[15][15];
  7.  
  8. bool inGrid(int x, int y)
  9. {
  10. return 1 <= x && x <= n && 1 <= y && y <= m;
  11. }
  12.  
  13. void solve()
  14. {
  15. memset(dis, 0, sizeof(dis));
  16. int time = 0;
  17. int x = 1, y = start;
  18. while(1){
  19. if(!inGrid(x, y)){
  20. printf("%d step(s) to exit\n", time);
  21. break;
  22. }
  23. if(dis[x][y] != 0){
  24. printf("%d step(s) before a loop of %d step(s)\n", dis[x][y]-1, time-dis[x][y]+1);
  25. break;
  26. }
  27. dis[x][y] = ++time;
  28. if(s[x][y] == 'N') --x;
  29. else if(s[x][y] == 'S') ++x;
  30. else if(s[x][y] == 'E') ++y;
  31. else if(s[x][y] == 'W') --y;
  32. }
  33. }
  34.  
  35. int main()
  36. {
  37. while(scanf("%d%d%d", &n, &m, &start), n){
  38. for(int i = 1; i <= n; ++i)
  39. scanf("%s", &s[i][1]);
  40. solve();
  41. }
  42. return 0;
  43. }

  

POJ 1573 Robot Motion的更多相关文章

  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 1573 Robot Motion(模拟)

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

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

                                                                                                         ...

  5. POJ 1573 Robot Motion 模拟 难度:0

    #define ONLINE_JUDGE #include<cstdio> #include <cstring> #include <algorithm> usin ...

  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. poj 1573 Robot Motion_模拟

    又是被自己的方向搞混了 题意:走出去和遇到之前走过的就输出. #include <cstdlib> #include <iostream> #include<cstdio ...

  8. PKU 1573 Robot Motion(简单模拟)

    原题大意:原题链接 给出一个矩阵(矩阵中的元素均为方向英文字母),和人的初始位置,问是否能根据这些英文字母走出矩阵.(因为有可能形成环而走不出去) 此题虽然属于水题,但是完全独立完成而且直接1A还是很 ...

  9. 【POJ - 1573】Robot Motion

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

随机推荐

  1. poj 2031 Building a Space Station(最小生成树,三维,基础)

    只是坐标变成三维得了,而且要减去两边的半径而已 题目 //最小生成树,只是变成三维的了 #define _CRT_SECURE_NO_WARNINGS #include<stdlib.h> ...

  2. 架构探险——从零开始写Java Web框架》第二章照作

    沉下来慢慢看实现了. 越来越觉得可以和DJANGO作对比. package org.smart4j.chapter2.model; /** * Created by sahara on 2016/3/ ...

  3. JavaC 编译目录下所有的UTF-8编码的java文件

    javac -encoding UTF-8  *.java

  4. Servlet 各种path路径比较

    假定你的web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下结果 ...

  5. [DLX]HDOJ4069 Squiggly Sudoku

    题意:有9*9的格子 每个格子 由五部分组成:上(16).右(32).下(64).左(128).和该格的数值(0~9) 若上下左右有分割格子的线 就加上相应的数, 该格的数值若为0,则是未知  1~9 ...

  6. @JsonFormat时间不对

    实际时间为:2015-07-06 20:20:23 1. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")    private Date ...

  7. iOS UICollectionView简单使用

    UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableVie ...

  8. NSMutableArray 初始化与添加删除程序

           Person *person1=[[Person alloc]initWithName:@"Kenshin"];        Person *person2=[[P ...

  9. 在Android里完美实现基站和WIFI定位

    来自:http://www.cnblogs.com/coffeegg/archive/2011/10/01/2197129.html 众所周知的,在OPhone和大部分国产的Android定制机里不支 ...

  10. mysql字符串区分大小写的问题

    一.1. CREATE TABLE NAME(name VARCHAR(10)); 对这个表,缺省情况下,下面两个查询的结果是一样的: SELECT * FROM TABLE NAME WHERE n ...