POJ1573——Robot Motion
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的更多相关文章
- poj1573 Robot Motion
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12507 Accepted: 6070 Des ...
- POJ-1573 Robot Motion模拟
题目链接: https://vjudge.net/problem/POJ-1573 题目大意: 有一个N*M的区域,机器人从第一行的第几列进入,该区域全部由'N' , 'S' , 'W' , 'E' ...
- poj1573 Robot Motion(DFS)
题目链接 http://poj.org/problem?id=1573 题意 一个机器人在给定的迷宫中行走,在迷宫中的特定位置只能按照特定的方向行走,有两种情况:①机器人按照方向序列走出迷宫,这时输出 ...
- POJ1573(Robot Motion)--简单模拟+简单dfs
题目在这里 题意 : 问你按照图中所给的提示走,多少步能走出来??? 其实只要根据这个提示走下去就行了.模拟每一步就OK,因为下一步的操作和上一步一样,所以简单dfs.如果出现loop状态,只要记忆每 ...
- POJ1573 Robot Motion(模拟)
题目链接. 分析: 很简单的一道题, #include <iostream> #include <cstring> #include <cstdio> #inclu ...
- 【POJ - 1573】Robot Motion
-->Robot Motion 直接中文 Descriptions: 样例1 样例2 有一个N*M的区域,机器人从第一行的第几列进入,该区域全部由'N' , 'S' , 'W' , 'E' ,走 ...
- Robot Motion(imitate)
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11065 Accepted: 5378 Des ...
- 模拟 POJ 1573 Robot Motion
题目地址:http://poj.org/problem?id=1573 /* 题意:给定地图和起始位置,robot(上下左右)一步一步去走,问走出地图的步数 如果是死循环,输出走进死循环之前的步数和死 ...
- POJ 1573 Robot Motion(BFS)
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12856 Accepted: 6240 Des ...
随机推荐
- Codevs 1158 尼克的任务
1158 尼克的任务 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 尼克每天上班之前都连接上英特网,接收他的上司发来的邮件,这些邮 ...
- Dribbble客户端应用源码
简约大气的Dribbble客户端,帮你时刻紧跟潮流,版本描述,添加对Likes & Following的支持设计简约的Dribbble客户端,提供了全面的浏览功能,让你时刻紧跟潮流! A BE ...
- WAMP环境的安装与测试
首先来点其他的补充:web服务的发展历程 对等网服务模式 没有专门的服务器,也没有专门的客户端!无法提供可靠的服务! C/S模式 client/server,各自安装不同的客户端和服务器端! B/S模 ...
- Openvpn完美解决公司网络没有固定公网IP的问题
方案背景: 公司办公网络使用长城宽带上网有一段时间了,有4个固定IP(2个电信,2个网通),链路不太稳定,经常有问题,因此考虑取消长城宽带,采用原来的adsl上网.但是有个问题,因为公司内网有几台服务 ...
- WebForm页面运行周期--页面关系
1.前台文件类继承于后台文件类 2.当前台文件中包含某个标签runat= server的时候,asp.net就会在编译这个页面前后台文件类的时候,在后台类中添加一个相应的控件对象:当页面被访问,也就是 ...
- Spark Streaming揭秘 Day28 在集成开发环境中详解Spark Streaming的运行日志内幕
Spark Streaming揭秘 Day28 在集成开发环境中详解Spark Streaming的运行日志内幕 今天会逐行解析一下SparkStreaming运行的日志,运行的是WordCountO ...
- 开博第一篇:DHT 爬虫的学习记录
经过一段时间的研究和学习,大致了解了DHT网络的一些信息,大部分还是参会别人的相关代码,一方面主要对DHT爬虫原理感兴趣,最主要的是为了学习python,大部分是别人的东西原理还是引用别人的吧 DHT ...
- sublime text2 配置php调试环境
步骤一: 首先确保你电脑安装了php,并把php设置到环境变量里了. 步骤二: 点击 sublime_text的“工具”->"编译系统"->"编译新系统&qu ...
- C# 白话系列之——白话委托
今天看到首页有个委托的文章,但大都写的太专业,而且没有实用的例子场景.正好昨天做了一个有关委托的功能,所以也来凑个热闹,用白话掰掰 一.委托是什么 我们都知道数据类型,简单点的如,想给一个变量赋值整数 ...
- web sevice 生成代理类及使用
一.生成代理类: VS2008下这样写 wsdl.exe /l:cs /out:D:/ProxyServices.cs http://localhost/WebService.asmx VS2010下 ...