转载请注明出处:http://blog.csdn.net/u012860063?

viewmode=contents

题目链接:

HDU:

pid=1035">http://acm.hdu.edu.cn/showproblem.php?pid=1035

POJ:   

id=1573">http://poj.org/problem?

id=1573


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)

纯模拟题;

代码例如以下:

#include <iostream>
using namespace std;
#include <cstring>
#define M 1000
char mat[M][M];
int re[M][M];
int n, m, s;
int ans, i, j, k;
void F()
{
i = 1; j = s;
k = 0;
for(int l = 1; ; l++)
{
if(re[i][j] != 0)
{
ans = k - re[i][j];
break;
}
if(i>n || i<1 || j>m || j<1)
{
break;
}
if(mat[i][j] == 'N')
{
k++;
re[i][j] = k;
i-=1;
continue;
}
if(mat[i][j] == 'S')
{
k++;
re[i][j] = k;
i+=1; continue;
}
if(mat[i][j] == 'W')
{
k++;
re[i][j] = k;
j-=1;
continue;
}
if(mat[i][j] == 'E')
{
k++;
re[i][j] = k;
j+=1;
continue;
}
}
}
int main()
{
while(cin>>n>>m>>s)
{
ans = 0;
if(n == 0 && m == 0 && s == 0)
break;
memset(mat,0,sizeof(mat));
memset(re,0,sizeof(re));
for(i = 1; i <= n; i++)
{
for(j = 1; j <= m; j++)
{
cin>>mat[i][j];
}
}
F();
if(ans == 0)
cout<<k<<" step(s) to exit"<<endl;
else
cout<<re[i][j]-1<<" step(s) before a loop of "<<ans+1<<" step(s)"<<endl;
}
return 0;
}

poj1573&amp;&amp;hdu1035 Robot Motion(模拟)的更多相关文章

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

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

  2. [ACM] hdu 1035 Robot Motion (模拟或DFS)

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

  3. HDU-1035 Robot Motion

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

  4. hdu1035 Robot Motion (DFS)

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

  5. POJ-1573 Robot Motion模拟

    题目链接: https://vjudge.net/problem/POJ-1573 题目大意: 有一个N*M的区域,机器人从第一行的第几列进入,该区域全部由'N' , 'S' , 'W' , 'E' ...

  6. POJ1573 Robot Motion(模拟)

    题目链接. 分析: 很简单的一道题, #include <iostream> #include <cstring> #include <cstdio> #inclu ...

  7. HDU1035 Robot Motion

    Problem Description A robot has been programmed to follow the instructions in its path. Instructions ...

  8. POJ 1573 Robot Motion 模拟 难度:0

    #define ONLINE_JUDGE #include<cstdio> #include <cstring> #include <algorithm> usin ...

  9. poj1573 Robot Motion

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

随机推荐

  1. [条款36]绝不重新定义继承而来的non-virtual函数

    看下面的两个类的声明代码: class B { public: void mf(); //something to do }; class D : public B { public: void mf ...

  2. PowerShell入门(序):为什么需要PowerShell?

    原文:http://www.cnblogs.com/ceachy/archive/2013/01/23/PowerShellPreface.html 曾几何时,微软的服务器操作系统因为缺乏一个强大的S ...

  3. django user模块改写

    示例一:继承AbstractBaseUser,AbstractBaseUser只有三个字段password,last_login,is_active,以及需要复习的两个方法 def get_full_ ...

  4. NET Core 环境搭建和命令行CLI入门

    NET Core 环境搭建和命令行CLI入门 2016年6月27日.NET Core & ASP.NET Core 1.0在Redhat峰会上正式发布,社区里涌现了很多文章,我也计划写个系列文 ...

  5. java 抽象类与接口的区别 整理

    抽象类与接口的区别 抽象类 包含抽象方法的类就是抽象类,声明的语句:abstract class 必须是public protected 接口 对行为的抽象,声明语句:interface 抽象方法的修 ...

  6. 开启Linux VNC远程桌面

    Xwindows:gnome (红帽默认安装的图形界面)   一, 确认及安装VNCSERVER. 1,首先确认你服务器是否配置了VNCSERVER,可以在命令行下敲入以下命令查看: [root@lo ...

  7. 解题报告 HDU1789 Doing Homework again

    Doing Homework again Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  8. frame和bounds的区别

    frame:根据父视图坐标系来确定自己的位置 bounds:该视图在自己坐标系的位置和大小 修改bounds并不会引起视图位置的变化,会影响自身子视图的位置:修改frame会引起视图位置的变化 UIV ...

  9. Jenkins Maven打包出错异常的解决方法

    Jenkins是一个很好用的打包部署工具,实现一键式部署项目,在项目处于测试阶段或者对于运维人员来讲是非常方便的一个工具. 但是最近使用Jenkins部署项目时老是出错,主要是maven打包的问题,错 ...

  10. android应用如何启动另外一个apk应用

    在开发的过程中,经常会遇到在一个应用中启动另外一个apk应用的情况 问题的核心点在于我们要拿到第三方apk的package名称跟class名称, 如:package名称是com.funcity.tax ...