Robot Motion

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)

题目大意:看图+样例基本就懂了。
解题思路:模拟
Code:

 #include<string>
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int a,b,first,px,py,date1,date2;
struct Point
{
char dir;
int vis,step;
} P[];
int move(int cnt)
{
int tmp=(py-)*a+px;
if (P[tmp].dir=='N') py--;
if (P[tmp].dir=='E') px++;
if (P[tmp].dir=='S') py++;
if (P[tmp].dir=='W') px--;
if (px>=a+||px<=||py>=b+||py<=)
return ;
tmp=(py-)*a+px;
if (P[tmp].vis)
{
date1=P[tmp].step-;
date2=cnt-date1-;
return -;
}
else P[tmp].vis=,P[tmp].step=cnt;
return ;
}
int main()
{
while (cin>>b>>a>>first)
{
int k=;
if (!a&&!b&&!first) break;
for (int i=; i<=b; i++)
for (int j=; j<=a; j++)
{
cin>>P[k].dir;
P[k++].vis=;
}
px=first,py=;
P[first].step=,P[first].vis=;
int cnt=,ok;
while ()
{
ok=move(cnt);
if (ok) break;
cnt++;
}
if (ok==) printf("%d step(s) to exit\n",cnt-);
else printf("%d step(s) before a loop of %d step(s)\n",date1,date2);
}
return ;
}

POJ1573——Robot Motion的更多相关文章

  1. poj1573 Robot Motion

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

  2. POJ-1573 Robot Motion模拟

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

  3. poj1573 Robot Motion(DFS)

    题目链接 http://poj.org/problem?id=1573 题意 一个机器人在给定的迷宫中行走,在迷宫中的特定位置只能按照特定的方向行走,有两种情况:①机器人按照方向序列走出迷宫,这时输出 ...

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

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

  5. POJ1573 Robot Motion(模拟)

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

  6. 【POJ - 1573】Robot Motion

    -->Robot Motion 直接中文 Descriptions: 样例1 样例2 有一个N*M的区域,机器人从第一行的第几列进入,该区域全部由'N' , 'S' , 'W' , 'E' ,走 ...

  7. Robot Motion(imitate)

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

  8. 模拟 POJ 1573 Robot Motion

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

  9. POJ 1573 Robot Motion(BFS)

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

随机推荐

  1. iOS 非ARC基本内存管理系列 2-多对象内存管理(2)

    /* 多对象内存管理: 以人拥有车为例涉及到@property底层set方法管理内存的实现 注意:人在换车的时候要进行当前传入的车和人所拥有的车进行判断 */ /******************* ...

  2. 隐藏index.php - ThinkPHP完全开发手册 - 3.1

      为了更好的实现SEO优化,我们需要隐藏URL地址中的index.php,由于不同的服务器环境配置方法区别较大,apache环境下面的配置我们可以参考5.9 URL重写来实现,就不再多说了,这里大概 ...

  3. 解决Scala异常处理java.lang.OutOfMemoryError: Java heap space error

    需求:百万.千万.4千万级日志对设备进行除重环境:设备内存64G,scala单机版运行shell文件日志:20G 48000000.log4.0G 10000000.log396M 1000000.l ...

  4. Java中的Enum枚举类型总结

    废话不多说,直接上代码,该例子来源于:http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html public enum Planet { ...

  5. Linux学习系列之Linux入门(三)gcc学习

    GCC(GNU Compiler Collection,GNU编译器套装),是一套由GNU开发的编程语言编译器.它是一套以GPL及LGPL许可证所发布的自由软件,也是GNU计划的关键部分,亦是自由的类 ...

  6. 特定用户QQ群聊天记录导出的实现

    一.把QQ群的聊天记录txt格式导出 消息管理器 -> 选择要导出的群 -> 右击.导出   这里要注意 : 导出之后的 文本是 unicode 编码的,需要转换 ==|| 之前不知道,搞 ...

  7. 快捷设置IE代理小工具

    时间:2015-02-06 起因: 公司新装了PLM系统,用这个系统必须使用指定IP段的IP才能访问.所以为了还能愉快的继续使用代理进行特定网站的访问,我们必须要频繁的去设置IE代理,这也太麻烦了吧. ...

  8. swift基础--数组、字典

    (1)初始化 (2)新增.修改.删除 (3)清空 (4)遍历 var array1 = ["x","y","z"] var array2:[ ...

  9. DevExpress GridControl 导出为Excel

    private void btnExport_ItemClick(object sender, EventArgs e)         {             SaveFileDialog sa ...

  10. 手写归并排序(MergeSort)

    #include<iostream> #include<stdio.h> #include<algorithm> #define N 10000 using nam ...