Problem 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
 
Sample Output

10 step(s) to exit 
3 step(s) before a loop of 8 step(s)
 
Code:
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <ctype.h> using namespace std; int key[][]; int main()
{
int a,b,p;
while(scanf("%d%d",&a,&b)&&a&&b)
{
scanf("%d",&p);
char arr[a][b];
int i,j;
for(i=;i<a;i++)
scanf("%s",&arr[i]); int s=,flag=;
i=;
j=p-;
while(true)
{
if(arr[i][j]=='N')
{
key[i][j]=s;
arr[i][j]='A';
i--;
} else if(arr[i][j]=='S')
{
key[i][j]=s;
arr[i][j]='A';
i++;
}
else if(arr[i][j]=='W')
{
key[i][j]=s;
arr[i][j]='A';
j--;
}
else if(arr[i][j]=='E')
{
key[i][j]=s;
arr[i][j]='A';
j++;
}
s++; if(arr[i][j]=='A')
{
flag=;
break;
}
if(i<||i==a||j<||j==b)
break;
}
if(flag)
printf("%d step(s) before a loop of %d step(s)\n",key[i][j],s-key[i][j]);
else
printf("%d step(s) to exit\n",s);
}
return ;
}

HDU1035 Robot Motion的更多相关文章

  1. HDU-1035 Robot Motion

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

  2. hdu1035 Robot Motion (DFS)

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

  3. poj1573&amp;&amp;hdu1035 Robot Motion(模拟)

    转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接: HDU:pid=1035">http://acm.hd ...

  4. HDU-1035 Robot Motion 模拟问题(水题)

    题目链接:https://cn.vjudge.net/problem/HDU-1035 水题 代码 #include <cstdio> #include <map> int h ...

  5. poj1573 Robot Motion

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

  6. Robot Motion(imitate)

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

  7. 模拟 POJ 1573 Robot Motion

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

  8. POJ 1573 Robot Motion(BFS)

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

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

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

随机推荐

  1. Bash shell执行命令的优先级

    1.别名2.关键字:if.function.while .until等3.函数4.内置命令5.可执行程序或脚本 别关函内可 =-=-=-=-=Powered by Blogilo

  2. js中年份、月份下拉框

    <select id="year" style="width: 100px;"></select> <select id=&quo ...

  3. module.exports与exports,export和export default

    还在为module.exports.exports.export和export default,import和require区别与联系发愁吗,这一篇基本就够了! 一.首先搞清楚一个基本问题: modu ...

  4. Python os模块--路径、文件、系统命令等操作

    os模块包含普遍的操作系统功能. 注意:函数参数path是文件或目录的路径,filename是文件的路径,dirname是目录的路径,路径可以是相对路径,也可绝对路径 常见或重要的函数为加粗字体 os ...

  5. Kafka官方文档翻译——简介

    简介 Kafka擅长于做什么? 它被用于两大类应用: 在应用间构建实时的数据流通道 构建传输或处理数据流的实时流式应用 几个概念: Kafka以集群模式运行在1或多台服务器上 Kafka以topics ...

  6. sqlserver isnull判断

    --在新增或编辑的时候设置默认值或加isnull判断 Sql isnull函数 ISNULL(columName, 0)<>35 或 ISNULL(columName, '')<&g ...

  7. ==,=和equals()区别

    equals和=,==的区别   一. ==和equals的区别 1. ==是运算符 2. equals是String对象的方法 一般有两种类型的比较 1. 基本数据类型的比较 2. 引用对象的比较 ...

  8. 【Socket】Java Socket基础编程

    Socket是Java网络编程的基础,了解还是有好处的, 这篇文章主要讲解Socket的基础编程.Socket用在哪呢,主要用在进程间,网络间通信.本篇比较长,特别做了个目录: 一.Socket通信基 ...

  9. Loadrunner--自动关联和手动关联

    2017-06-09 15:32:45个人也属于刚刚开始学习,有什么不对的地方敬请指导:qq:389791447 一开始的时候,准备去学习怎么去关联.一时也毛不着头脑,就在网上找了一些视频看,有的人说 ...

  10. man ssh翻译(ssh命令中文手册)

    本文为命令ssh的man文档翻译,翻译了90%的内容,剩余是一些没必要翻译的东西,请见谅. 如此文有所疑惑,希望我的另一篇文章能解惑: SSH(1)                    BSD Ge ...