Robot Motion

Time Limit: 1000 MS Memory Limit: 10000 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status] [Discuss]

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>
#include <string.h> using namespace std; int main(void)
{
int row,col,entry;
char grid[][]; //在规定大小的grid外部至少再定义一圈"门槛"以判断Robot是否离开了grid (最大grid为10x10) for(;;)
{
memset(grid,'O',sizeof(grid)); // 'O' 为大写字母O,意为 Out /*Input*/ int i,j; cin>>row>>col>>entry;
if(!(row && col && entry))break; for(i=;i<=row;i++)
for(j=;j<=col;j++)
cin>>grid[i][j]; /*Judge Robot get out of the grid or starts a loop in the grid*/ int flag[][]={}; //标记Robot经过某点的次数,当有一点为2则说明Robot陷入了以该点为loop起始点的循环
int count;
int r=;
int c=entry;
for(count=;;count++)
{
flag[r][c]++; //注意顺序,先标记,再位移
if(grid[r][c]=='N') // ↑
r--;
else if(grid[r][c]=='S') // ↓
r++;
else if(grid[r][c]=='W') // ←
c--;
else if(grid[r][c]=='E') // →
c++;
else if(grid[r][c]=='O') // Out
{
cout<<count<<" step(s) to exit"<<endl;
break;
} if(flag[r][c]==) //loop
{
row=r; //标记Robot循环起止点
col=c;
int flg=;
for(r=,c=entry,count=;;count++)
{
if(r==row && c==col && flg==) //注意顺序,先寻找循环点再位移(避免Robot刚进入grid就陷入了循环的情况)
{
cout<<count<<" step(s) before a loop of "; //输出进入循环前的步数
count=;
flg++;
}
if(r==row && c==col && count!= && flg==)
{
cout<<count<<" step(s)"<<endl; //输出循环步数
break;
}
if(grid[r][c]=='N') // ↑
r--;
else if(grid[r][c]=='S') // ↓
r++;
else if(grid[r][c]=='W') // ←
c--;
else if(grid[r][c]=='E') // →
c++;
}
break; //跳出count的for循环,不是跳出if(当然break也不能用于跳出if,这里的说明是为了避免误解)
}
}
}
return ;
}

题意:按照表格的提示走~~~

模拟模拟  太恶心啦

http://blog.csdn.net/lyy289065406/article/details/6645434

他的博客真心棒!!!!!

poj1573模拟的更多相关文章

  1. poj1573 模拟

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11270   Accepted: 5487 Des ...

  2. POJ1573(Robot Motion)--简单模拟+简单dfs

    题目在这里 题意 : 问你按照图中所给的提示走,多少步能走出来??? 其实只要根据这个提示走下去就行了.模拟每一步就OK,因为下一步的操作和上一步一样,所以简单dfs.如果出现loop状态,只要记忆每 ...

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

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

  4. POJ-1573 Robot Motion模拟

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

  5. POJ1573 Robot Motion(模拟)

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

  6. App开发:模拟服务器数据接口 - MockApi

    为了方便app开发过程中,不受服务器接口的限制,便于客户端功能的快速测试,可以在客户端实现一个模拟服务器数据接口的MockApi模块.本篇文章就尝试为使用gradle的android项目设计实现Moc ...

  7. 故障重现, JAVA进程内存不够时突然挂掉模拟

    背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...

  8. Python 爬虫模拟登陆知乎

    在之前写过一篇使用python爬虫爬取电影天堂资源的博客,重点是如何解析页面和提高爬虫的效率.由于电影天堂上的资源获取权限是所有人都一样的,所以不需要进行登录验证操作,写完那篇文章后又花了些时间研究了 ...

  9. HTML 事件(四) 模拟事件操作

    本篇主要介绍HTML DOM中事件的模拟操作. 其他事件文章 1. HTML 事件(一) 事件的介绍 2. HTML 事件(二) 事件的注册与注销 3. HTML 事件(三) 事件流与事件委托 4.  ...

随机推荐

  1. 洛谷2860 [USACO06JAN]冗余路径Redundant Paths

    原题链接 题意实际上就是让你添加尽量少的边,使得每个点都在至少一个环上. 显然对于在一个边双连通分量里的点已经满足要求,所以可以用\(tarjan\)找边双并缩点. 对于缩点后的树,先讲下我自己的弱鸡 ...

  2. Windows 下 Phpstrom 配置git使用

    首先先去下载 git 下载链接 https://git-scm.com/download/winphpstrom 配置git    链接  http://jingyan.baidu.com/artic ...

  3. Linux命令大全完整版

      1. linux系统管理命令 adduser 功能说明:新增用户帐号.语 法:adduser补充说明:在Slackware中,adduser指令是个script程序,利用交谈的方式取得输入的用户帐 ...

  4. Python中的类方法、实例方法、静态方法

    类方法 @classmethod 在python中使用较少,类方法传入的第一个参数是 cls,是类本身: 类方法可以通过类直接调用或者通过实例直接调用,但无论哪种调用方式,最左侧传入的参数一定是类本身 ...

  5. JDK8集合类源码解析 - LinkedList

    linkedList主要要注意以下几点: 1构造器 2 添加add(E e) 3 获取get(int index) 4 删除 remove(E e),remove(int index) 5  判断对象 ...

  6. 解决mysql安装出现error Nr.1045问题

    我们在windows下安装mysql最后一步时会出现Access denied for user 'root'@localhost'(using password:No)的问题.这几个问题经常出现在卸 ...

  7. Java学习笔记:注解Annotation

    annotation的概念 In the Java computer programming language, an annotation is a form of syntactic metada ...

  8. centos7 虚拟机安装docker-ce-17.09

    1.创建虚拟机使用iso镜像centos-x86_64-7.3.1611 2.安装centos选择桌面版 3.配置命令行环境,网卡(见博客另一篇文章) 4.安装container-selinux-2. ...

  9. PHP中require(),include(),require_once()和include_once()有什么区别

    引用文件的方法有两种:require 及 include.两种方式提供不同的使用弹性. require 的使用方法如 require("MyRequireFile.php"); . ...

  10. for循环添加的闭包问题

    function test(){ var arr = []; for (var i=0;i;i++){ arr[i] = function(){ console.log(i); } } return ...