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 ...
随机推荐
- tip 2:找最小公倍数之Boost
今天在codewars上面做了一题,kata5的,其中一个实现函数是几个数字的最小公倍数.自己的代码编译虽然也成功了,但是不够简介.看了别人的代码才发现可以直接调用Boost的math模块. 看eff ...
- DEDECMS中,channel标签
获取栏目列表标签 dede:channel 标签: {dede:channel type='top' row='8' currentstyle="<li><a href=' ...
- 利用cglib生成动态java bean
cglib详细学习 http://blog.csdn.net/u010150082/article/details/10901641 cglib-nodep jar报下载 http://grepcod ...
- 玩转Slot Machine
最近在做一个有关Slot Machine小游戏的开发,其中遇到了不少的坑,现将个人遇到的问题总结如下,希望今后对大家开发的过程中有所的帮助. 这个项目是部署到微信朋友圈广告的,两天时间,PV就有14 ...
- 最简便的清空memcache的方法
如果要清空memcache的items,常用的办法是什么?杀掉重启?如果有n台memcache需要重启怎么办?挨个做一遍? 很简单,假设memcached运行在本地的11211端口,那么跑一下命令行: ...
- Android笔记——Bitmap自动取色(纯搬运)
2015/6/12更新:发现一个更好的,带demo https://github.com/MichaelEvans/ColorArt 说明: 这个是一个老外写的自动自动从bitmap中取主色与第二主色 ...
- Oracle中的CR块详解
1.概述 Cr块consistent read块也就是用来维护oracle的读一致性的数据块.当查询某些数据的时候,发现数据块的版本比我们要查询的新,例如session1执行了dml操作并没有提交,s ...
- easy ui 菜单和按钮(Menu and Button)
http://www.zi-han.net/case/easyui/menu&button.html
- 在openwrt上初体验PostgreSQL数据库
要求 请确保在你的路由器shell 中有以下这些命令 adduser, deluser, addgroup, delgroup, su . 还需要熟悉su,chown ,opkg,mkdir,服务操作 ...
- 3.3 spring-meta子元素的使用与解析
1. meta元素的使用 在解析元数据的分析之前,我们先回顾一下 meta属性的使用: <bean id="car" class="test.CarFactoryB ...