Robot Motion
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 11324   Accepted: 5512

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)

突然爱上了模拟题,要是所有题都像这样的题目只需模拟一下走的方向,而不考虑算法的该有多好。。。哈哈

从头开始走,记录第几步走在了哪一个格子上,如果要走的格子大于0了,说明走重复了。这题比较水。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; enum{ E,N,W,S };
int map_f[20][20];
char call[20][20];
int move_x[5]={0,-1,0,1};
int move_y[5]={1,0,-1,0};
int X,Y,start,i,cur_x,cur_y,flag; void solve()
{
int temp; cur_x = 1;
cur_y = start;
map_f[cur_x][cur_y]=1; while(1)
{
if(flag)
break;
if(call[cur_x][cur_y]=='E')
{
temp=map_f[cur_x][cur_y];
cur_x = cur_x + move_x[E];
cur_y = cur_y + move_y[E]; if(cur_x<=0 || cur_y<=0 || cur_x>X || cur_y>Y)
{
flag=1;
cout<<temp<<" step(s) to exit"<<endl;
}
else if(map_f[cur_x][cur_y])
{
flag=1;
cout<<map_f[cur_x][cur_y]-1<<" step(s) before a loop of "<<temp-map_f[cur_x][cur_y]+1<<" step(s)"<<endl;
}
else
{
map_f[cur_x][cur_y]=temp+1;
}
}
else if(call[cur_x][cur_y]=='N')
{
temp=map_f[cur_x][cur_y];
cur_x = cur_x + move_x[N];
cur_y = cur_y + move_y[N]; if(cur_x<=0 || cur_y<=0 || cur_x>X || cur_y>Y)
{
flag=1;
cout<<temp<<" step(s) to exit"<<endl;
}
else if(map_f[cur_x][cur_y])
{
flag=1;
cout<<map_f[cur_x][cur_y]-1<<" step(s) before a loop of "<<temp-map_f[cur_x][cur_y]+1<<" step(s)"<<endl;
}
else
{
map_f[cur_x][cur_y]=temp+1;
}
}
else if(call[cur_x][cur_y]=='W')
{
temp=map_f[cur_x][cur_y];
cur_x = cur_x + move_x[W];
cur_y = cur_y + move_y[W]; if(cur_x<=0 || cur_y<=0 || cur_x>X || cur_y>Y)
{
flag=1;
cout<<temp<<" step(s) to exit"<<endl;
}
else if(map_f[cur_x][cur_y])
{
flag=1;
cout<<map_f[cur_x][cur_y]-1<<" step(s) before a loop of "<<temp-map_f[cur_x][cur_y]+1<<" step(s)"<<endl;
}
else
{
map_f[cur_x][cur_y]=temp+1;
}
}
else if(call[cur_x][cur_y]=='S')
{
temp=map_f[cur_x][cur_y];
cur_x = cur_x + move_x[S];
cur_y = cur_y + move_y[S]; if(cur_x<=0 || cur_y<=0 || cur_x>X || cur_y>Y)
{
flag=1;
cout<<temp<<" step(s) to exit"<<endl;
}
else if(map_f[cur_x][cur_y])
{
flag=1;
cout<<map_f[cur_x][cur_y]-1<<" step(s) before a loop of "<<temp-map_f[cur_x][cur_y]+1<<" step(s)"<<endl;
}
else
{
map_f[cur_x][cur_y]=temp+1;
}
}
} } int main()
{ while(cin>>X>>Y>>start)
{
if(!(X+Y+start))
break; memset(map_f,0,sizeof(map_f));
flag=0; for(i=1;i<=X;i++)
{
cin>>call[i]+1;
}
solve();
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1573:Robot Motion的更多相关文章

  1. 【POJ - 1573】Robot Motion

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

  2. 模拟 POJ 1573 Robot Motion

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

  3. POJ 1573 Robot Motion(BFS)

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

  4. Poj OpenJudge 百练 1573 Robot Motion

    1.Link: http://poj.org/problem?id=1573 http://bailian.openjudge.cn/practice/1573/ 2.Content: Robot M ...

  5. POJ 1573 Robot Motion(模拟)

    题目代号:POJ 1573 题目链接:http://poj.org/problem?id=1573 Language: Default Robot Motion Time Limit: 1000MS ...

  6. Robot Motion 分类: POJ 2015-06-29 13:45 11人阅读 评论(0) 收藏

    Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11262 Accepted: 5482 Descrip ...

  7. POJ 1573 Robot Motion

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12978   Accepted: 6290 Des ...

  8. poj 1573 Robot Motion【模拟题 写个while循环一直到机器人跳出来】

                                                                                                         ...

  9. poj1573 Robot Motion

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

随机推荐

  1. Deepctr框架代码阅读

    DeepCtr是一个简易的CTR模型框架,集成了深度学习流行的所有模型,适合学推荐系统模型的人参考. 我在参加比赛中用到了这个框架,但是效果一般,为了搞清楚原因从算法和框架两方面入手.在读代码的过程中 ...

  2. 题解 loj2065 「SDOI2016」模式字符串

    点分治. 考虑经过当前分治中心\(u\)的点对数量. 这种数点对数的问题,有一个套路.我们可以依次考虑\(u\)的每个儿子,看用当前的儿子,能和之前已经考虑过的所有儿子,组成多少点对.这样所有合法的点 ...

  3. uni-app小程序组建

    (1)新建组建:编辑器右击 新建组建 (2)传值 <template> <view class="myRankingList"> <block v-f ...

  4. 洛谷 P2031 脑力达人之分割字串

    题目传送门 解题思路: f[i]表示到第i位可获得的最大分割次数,对于每个f[i]都可由其符合条件的前缀转移过来,条件就是当前串除了前缀的剩余字符里有所给单词,然后一看,这不是在剩余字符里找有没有所给 ...

  5. python 字典复制(存疑)

    import copy x = {'a':1,'b':[2,3,4]} y = x.copy() z = copy.deepcopy(x) print(x) print(y) print(z) pri ...

  6. java随机函数用法Random

     原文地址:http://blog.csdn.net/wpjava/article/details/6004492  import java.util.Random; public class Ran ...

  7. 微信小程序是什么

    官方的开发文档 微信小程序写的不多,随便写写 创建项目,分析工具 微信小程序有专门的编辑工具,去官网下载 然后申请一个小程序项目,获得一个appId,然后进入编辑工具就可以直接开发了 编辑工具可以设置 ...

  8. 原型与继承与class

    对象有属性(专业点叫静态属性)和方法(专业点叫静态方法)和原型属性和原型方法 除了系统自带的那么几百万个对象,我们自己写在js的创建的对象,自定义的对象,都来自对象的构造函数,用来构造对象的函数,叫做 ...

  9. mac搭建nginx

    0.介绍 Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务.Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler ...

  10. 使用线程池测试cpu的并发计算能力

    接到一个需求是测试一下cpu并发计算能力,针对int和float求和单位时间能执行几次的问题.可能是服务器选型用到的参数. 开始使用的是fork-join,但是发现fork-join每次得到的结果值波 ...