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. 从Lumia退役看为什么WP走向没落

    前段时间决定将自己用了三年多的Lumia 800正式退役,这是我用的时间最长的手机,虽然系统上有缺陷,但是好不妨碍他成为我最有感情的一部手机.由于之前是WinPhone 开发者的关系,这部手机是微软送 ...

  2. css3动画 bug 2点

    1. .myanimate{ transition-property: left;transition-duration: .3s;transition-timing-function: ease } ...

  3. cocos2d-x 3.0以上版本字体设置问题

    cocos2d-x中的万年大坑,字体总算是有比较好的结局办法了.之前都是CCLabelTTF,CCLabelBMFont,CCLabelAtlas什么的,我只想说一句:好难用!毕竟是做游戏,那么难看的 ...

  4. NuGet更新引用Dll

    第一种 通过 "Add Library Package Reference..." 添加 点击 ‘Add Library Package Reference...’ , 搜索你要添 ...

  5. WCF入门(9)

    前言 上次搬家空调出了点问题,和修空调的师傅商量了一下,感觉还是讲理的. 今天又在公司基本没有任何存在感的过了一天,纠结...领导还不在... 前些天往手机里面放了几集WCF入门视频,今天用暴风影音看 ...

  6. SQL删除重复的记录(只保留一条)

    首先新建表: --创建示例表 CREATE TABLE t ( id ,) PRIMARY KEY, a ), b ) ) --插入数据 INSERT INTO t SELECT 'aa','bb' ...

  7. javascript 漏洞

    1.javascript语言中,每一个对象都有一个对应的原型对象,称为prototype对象.  继承是基于原型的! 2.prototype对象的作用,就是定义所有实例对象共享的属性和方法! 3.“原 ...

  8. JS实现Ajax---例:获取服务器时间

    Ajax在本质上是一个浏览器端的技术 XMLHttpRequest XMLHttpRequest对象 XMLHttpRequest对象在IE浏览器和非IE浏览器中创建的方法不同. 简而言之:它可以异步 ...

  9. sql-and、or

    WHERE 指令可以被用来由表格中有条件地选取资料. 这个条件可能是简单的 (像上一页的例子),也可能是复杂的.复杂条件是由二或多个简单条件透过 AND 或是 OR 的连接而成.一个 SQL 语句中可 ...

  10. 详解HTML中的window对象和document对象

    Window -- 代表浏览器中一个打开的窗口: 对象属性 window //窗口自身 window.self //引用本窗户window=window.self window.name //为窗口命 ...