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

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<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int row , col , o ;
char map[][] ;
int a[][] ;
int x , y ; void loop (char dir)
{
switch (dir)
{
case 'N' : printf ("%d step(s) before a loop of %d step(s)\n" , a[x][y] - , a[x + ][y] - a[x][y] + ) ; break ;
case 'E' : printf ("%d step(s) before a loop of %d step(s)\n" , a[x][y] - , a[x][y - ] - a[x][y] + ) ; break ;
case 'S' : printf ("%d step(s) before a loop of %d step(s)\n" , a[x][y] - , a[x - ][y] - a[x][y] + ) ; break ;
case 'W' : printf ("%d step(s) before a loop of %d step(s)\n" , a[x][y] - , a[x][y + ] - a[x][y] + ) ; break ;
}
}
void solve ()
{
x = , y = o ;
bool flag = ;
char temp ;
a[x][y] = ;
while (x >= && x <= row && y >= && y <= col) {
switch (map[x][y])
{
case 'N' : x-- ; if (a[x][y]) flag = ;
if (!flag)
a[x][y] = a[x + ][y] + ;
else
temp = 'N' ; break ;
case 'E' : y++ ; if (a[x][y]) flag = ;
if (!flag)
a[x][y] = a[x][y - ] + ;
else
temp = 'E' ; break ;
case 'S' : x++ ; if (a[x][y]) flag = ;
if (!flag)
a[x][y] = a[x - ][y] + ;
else
temp = 'S' ; break ;
case 'W' : y-- ; if (a[x][y]) flag = ;
if (!flag)
a[x][y] = a[x][y + ] + ;
else
temp = 'W' ; break ;
}
if (flag) {
loop (temp) ;
break ;
}
}
if (!flag)
printf ("%d step(s) to exit\n" , a[x][y] - ) ;
}
int main ()
{
// freopen ("a.txt" , "r" , stdin) ;
while (~ scanf ("%d%d%d" , &row , &col , &o)) {
if (row == && col == && o == )
break ;
memset (a , , sizeof(a)) ;
for (int i = ; i <= row ; i++)
for (int j = ; j <= col ; j++)
cin >> map[i][j] ; solve () ;
/* for (int i = 1 ; i <= row ; i++) {
for (int j = 1 ; j <= col ; j++) {
printf ("%d " , a[i][j]) ;
}
puts ("") ;
}
printf ("\n\n") ;*/
}
return ;
}

Robot Motion(imitate)的更多相关文章

  1. poj1573 Robot Motion

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

  2. 模拟 POJ 1573 Robot Motion

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

  3. POJ 1573 Robot Motion(BFS)

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

  4. Robot Motion 分类: POJ 2015-06-29 13:45 11人阅读 评论(0) 收藏

    Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11262 Accepted: 5482 Descrip ...

  5. POJ 1573 Robot Motion

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

  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. POJ1573——Robot Motion

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

  8. hdoj 1035 Robot Motion

    Robot Motion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  9. HDU-1035 Robot Motion

    http://acm.hdu.edu.cn/showproblem.php?pid=1035 Robot Motion Time Limit: 2000/1000 MS (Java/Others)   ...

随机推荐

  1. python实现简易数据库之一——存储和索引建立

    最近没事做了一个数据库project,要求实现一个简单的数据库,能满足几个特定的查询,这里主要介绍一下我们的实现过程,代码放在过ithub,可参看这里.都说python的运行速度很慢,但因为时间比较急 ...

  2. 《android基于andFix的热修复方案》思路篇

    1:需求背景 项目上线之后,发现BUG需要修复(比如安卓兼容性等测试难以发现的问题),频繁的更新影响用户体验 2:方案要求 静默下载,耗费流量少,打完补丁后立刻生效,不用重启apk 3:解决思路 3. ...

  3. grootJS ui控件定义

    index13.html <html><head> <title>ui控件定义</title> <script src="jquery- ...

  4. 接受客户端传的inputstream类型转成string类型

    public string GetReqContent() { var request = HttpContext.Current.Request; var contentStream = reque ...

  5. 【BZOJ1013】【JSOI2008】球形空间产生器sphere(高斯消元)

    1013: [JSOI2008]球形空间产生器sphere Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 1600  Solved: 860[Submi ...

  6. 16.C#初见Lambda表达式及表达式树(九章9.1-9.3)

    在说明Lambda相关知识前,我们需要了解Lambda表达式常用于LINQ,那么我们来聊下LINQ. LINQ的基本功能就是创建操作管道,以及这些操作需要的任何状态.这些操作表示了各种关于数据的逻辑: ...

  7. Bootstrap3.0学习第十九轮(JavaScript插件——标签页)

    详情请看 http://aehyok.com/Blog/Detail/46.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...

  8. Sublime-jQueryDocs

    Package Control Messages======================== jQueryDocs---------- This package shows a selected ...

  9. OC基础--分类(category) 和 协议(protocol)

    OC 中的category分类文件相当于 C#中的部分类:OC 中的protocol协议文件(本质是头文件)相当于 C#中的接口.今天就简单说明一下OC中的这两个文件. 由于视频中的Xcode版本低, ...

  10. BZOJ-2326 数学作业 矩阵乘法快速幂+快速乘

    2326: [HNOI2011]数学作业 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1564 Solved: 910 [Submit][Statu ...