Robot Motion
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10708   Accepted: 5192

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)
    题意:一个迷宫,机器人从某行最上方进入,跟随每一步到达的迷宫的指示行动,问机器人是否能到达迷宫外(任意一边都可以出),或者会在何时进入圈
    应用时:15min
    实际用时:51min
    原因:读题,x,y用反
  1. #define ONLINE_JUDGE
  2. #include<cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. int A,B,sx,sy;
  8. char maz[101][101];
  9. int vis[101][101];
  10. const int dx[4]={0,1,0,-1};
  11. const int dy[4]={-1,0,1,0};
  12.  
  13. int dir(char ch){
  14. if(ch=='N')return 0;
  15. else if(ch=='E')return 1;
  16. else if(ch=='S')return 2;
  17. return 3;
  18. }
  19. void solve(){
  20. memset(vis,0,sizeof(vis));
  21. sx--;sy=0;
  22. int step=0;
  23. int fx,fy;
  24. while(!vis[sy][sx]&&sx>=0&&sx<A&&sy>=0&&sy<B&&++step){
  25. fx=sx;fy=sy;
  26. vis[sy][sx]=step;
  27. sx=dx[dir(maz[fy][fx])]+fx;
  28. sy=dy[dir(maz[fy][fx])]+fy;
  29. }
  30. if(sx<0||sy<0||sx>=A||sy>=B)printf("%d step(s) to exit\n",step);
  31. else {
  32. printf("%d step(s) before a loop of %d step(s)\n",vis[sy][sx]-1,step+1-vis[sy][sx]);
  33. }
  34. }
  35. int main(){
  36. #ifndef ONLINE_JUDGE
  37. freopen("output.txt","w",stdout);
  38. #endif // ONLINE_JUDGE
  39. while(scanf("%d%d%d",&B,&A,&sx)==3&&A&&B){
  40.  
  41. for(int i=0;i<B;i++)scanf("%s",maz[i]);
  42. solve();
  43. }
  44. return 0;
  45. }

  

快速切题 poj1573的更多相关文章

  1. 快速切题sgu127. Telephone directory

    127. Telephone directory time limit per test: 0.25 sec. memory limit per test: 4096 KB CIA has decid ...

  2. 快速切题sgu126. Boxes

    126. Boxes time limit per test: 0.25 sec. memory limit per test: 4096 KB There are two boxes. There ...

  3. 快速切题 sgu123. The sum

    123. The sum time limit per test: 0.25 sec. memory limit per test: 4096 KB The Fibonacci sequence of ...

  4. 快速切题 sgu120. Archipelago 计算几何

    120. Archipelago time limit per test: 0.25 sec. memory limit per test: 4096 KB Archipelago Ber-Islan ...

  5. 快速切题 sgu119. Magic Pairs

    119. Magic Pairs time limit per test: 0.5 sec. memory limit per test: 4096 KB “Prove that for any in ...

  6. 快速切题 sgu118. Digital Root 秦九韶公式

    118. Digital Root time limit per test: 0.25 sec. memory limit per test: 4096 KB Let f(n) be a sum of ...

  7. 快速切题 sgu117. Counting 分解质因数

    117. Counting time limit per test: 0.25 sec. memory limit per test: 4096 KB Find amount of numbers f ...

  8. 快速切题 sgu116. Index of super-prime bfs+树思想

    116. Index of super-prime time limit per test: 0.25 sec. memory limit per test: 4096 KB Let P1, P2, ...

  9. 快速切题 sgu115. Calendar 模拟 难度:0

    115. Calendar time limit per test: 0.25 sec. memory limit per test: 4096 KB First year of new millen ...

随机推荐

  1. Python3基础 str format 四舍六入五凑偶 保留一位小数

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  2. C#中dll附加配置文件

    DLL-with-configuration-file带配置文件的dll http://www.codeproject.com/Tips/199441/DLL-with-configuration-f ...

  3. UVA 11806 Cheerleaders (容斥原理

    1.题意描述 本题大致意思是讲:给定一个广场,把它分为M行N列的正方形小框.现在给定有K个拉拉队员,每一个拉拉队员需要站在小框内进行表演.但是表演过程中有如下要求: (1)每一个小框只能站立一个拉拉队 ...

  4. UVa 11729 突击战

    https://vjudge.net/problem/UVA-11729 题意:有n个部下,每个部下需要完成一项任务.第i个部下需要你话B分钟交代任务,然后立刻执行J分钟完成任务.安排交代任务顺序并计 ...

  5. Linux(CentOS 6.5) 下安装MySql 5.7.18 二进制版本粗浅攻略

    鉴于Linux和mysql因不同版本,安装方式也不同,所以在阅读本攻略前,请确保各位同学的版本和我的Linux.MySql 版本一致. 如果不一致,只能参考. 我的版本: Linux CentOS 6 ...

  6. python 时间戳转元组

    #!/usr/bin/python # -*- coding: UTF- -*- import time localtime = time.localtime(time.time()) print(& ...

  7. listener TNS-01189 问题

    -- 启动监听,提示已经启动. [oracle@sh ~]$ lsnrctl start LSNRCTL for Linux: Version 12.1.0.2.0 - Production on 0 ...

  8. TinyXML用法小结2

    参考:http://www.cnblogs.com/hgwang/p/5833638.html TinyXML用法小结 1.      介绍 Tinyxml的官方网址:http://www.grinn ...

  9. [ios][swift]swift中如果做基本类型的转换

    在swift中如果做基本类型的转换的?比如Int -> Float(Double)Double -> 保留两位小数String -> IntDouble -> String 有 ...

  10. 《A_Pancers团队》———团队项目原型设计与开发

    一.实验目的与要求 (1)掌握软件原型开发技术: (2)学习使用软件原型开发工具:本实验中使用墨刀 二.实验内容与步骤 任务1:针对实验六团队项目选题,采用适当的原型开发工具设计团队项目原型: 任务2 ...