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

3 6 5
NEESWE
WWWESS
SNWWWW
4 5 1
SESWE
EESNW
NWEEN
EWSEN
0 0 0

Sample Output

10 step(s) to exit
3 step(s) before a loop of 8 step(s)

Source

 
 
 
解析:模拟。
 
 
 
#include <cstdio>
#include <cstring> char s[15][15];
int n, m , start;
int dis[15][15]; bool inGrid(int x, int y)
{
return 1 <= x && x <= n && 1 <= y && y <= m;
} void solve()
{
memset(dis, 0, sizeof(dis));
int time = 0;
int x = 1, y = start;
while(1){
if(!inGrid(x, y)){
printf("%d step(s) to exit\n", time);
break;
}
if(dis[x][y] != 0){
printf("%d step(s) before a loop of %d step(s)\n", dis[x][y]-1, time-dis[x][y]+1);
break;
}
dis[x][y] = ++time;
if(s[x][y] == 'N') --x;
else if(s[x][y] == 'S') ++x;
else if(s[x][y] == 'E') ++y;
else if(s[x][y] == 'W') --y;
}
} int main()
{
while(scanf("%d%d%d", &n, &m, &start), n){
for(int i = 1; i <= n; ++i)
scanf("%s", &s[i][1]);
solve();
}
return 0;
}

  

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. 空格的URL编码

    Q: 为什么我看的教材一会说是“+” 一会说是“%20” A: urlencode(" ") '返回+encodeURI(" ") '返回%20是有区别的

  2. C Primer Plus 第4章 字符串和格式化输入/输出 编程练习

    1. #include <stdio.h> int main(void) { ]; ]; printf("请输入您的名字: "); scanf("%s&quo ...

  3. Android 监测手机联网状态 wifi、移动数据流量、无联网状态

    手机当完成联网时会发送一个广播,我们只要创建一个广播接收者即可,代码如下: package com.example.NetworkChangeReceiver2; import android.con ...

  4. Linux使用本地iso作为yum源

    虚拟机中的Linux有时不能连接上外网,为了能够方便的安装各种packages,于是调查配置本地yum安装的方法. 首先,将作为源的iso的挂载到系统上. mount -o loop /dev/cdr ...

  5. 李洪强漫谈iOS开发[C语言-008]- C语言重难点

      C语言学习的重难点 写程序的三个境界: 照抄的境界,翻译的境界,创新的境界 1  伪代码: 描述C语言的编程范式 范式: 规范的一种表示 对于C的范式学会的话,C, C++ Java 都会了 2 ...

  6. hdu 1002 java 大数相加

    package Main; //import java.io.InputStream; import java.math.BigDecimal; import java.util.Scanner; p ...

  7. 为什么需要用到序列化?为什么HttpSession中对象要序列化

    简单说就是为了保存在内存中的各种对象的状态,并且可以把保存的对象状态再读出来.虽然你可以用你自己的各种各样的方法来保存Object States,但是Java给你提供一种应该比你自己好的保存对象状态的 ...

  8. python 处理 Excel 表格

    see: http://www.cnblogs.com/sunada2005/p/3193300.html 一.可使用的第三方库 python中处理excel表格,常用的库有xlrd(读excel)表 ...

  9. ArcGIS 10.1 for Desktop新特性之地理标记照片

    转自:http://blog.csdn.net/esrichinacd/article/details/7730825 地理标记照片是指带有地理位置信息的照片,通常通过内置GPS的数码相机或智能手机拍 ...

  10. TCP digest

    TCP在网络OSI的七层模型中的第四层——Transport层,IP在第三层——Network层,ARP在第二层——Data Link层,在第二层上的数据,我们叫Frame,在第三层上的数据叫Pack ...