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

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)

Source

简单的方位移动型模拟题,要用方位数组。移动模拟过程用搜索。

15777856

  ksq2013 1573 Accepted 700K 0MS G++ 1074B 2016-07-21 22:16:18
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int mx[]={0,+1,0,-1};
const int my[]={+1,0,-1,0};//ESWN;
bool vis[11][11];
int n,m,s,mp[11][11],stp[11][11];
void dfs(int x,int y,int cnt)
{
if(x<1||x>n||y<1||y>m){
printf("%d step(s) to exit\n",cnt);
return;
}
if(vis[x][y]){
printf("%d step(s) before a loop of %d step(s)\n",stp[x][y],cnt-stp[x][y]);
return;
}
vis[x][y]=1;
stp[x][y]=cnt;
int tmp=mp[x][y];
dfs(x+mx[tmp],y+my[tmp],cnt+1);
}
int main()
{
while(scanf("%d%d%d",&n,&m,&s)&&s){
memset(mp,0,sizeof(mp));
memset(vis,0,sizeof(vis));
for(int x=1;x<=n;x++){
for(int y=1;y<=m;y++){
char ch;
cin>>ch;
switch(ch){
case 'E':mp[x][y]=0;break;
case 'S':mp[x][y]=1;break;
case 'W':mp[x][y]=2;break;
case 'N':mp[x][y]=3;break;
}
}
}
dfs(1,s,0);
}
return 0;
}

poj1573 Robot Motion的更多相关文章

  1. POJ1573——Robot Motion

    Robot Motion Description A robot has been programmed to follow the instructions in its path. Instruc ...

  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. Python十六进制与字符串的转换

    电脑上装了Python2.7和3.3两个版本,平时运行程序包括在Eclipse里面调试都会使用2.7,但是由于某些原因在cmd命令行中输入python得到的解释器则是3.3, 一直没对此做处理,因为这 ...

  2. Httplistener Access Denied

    HttpListener.Start() 会出现HttpListenerException, 显示拒绝访问 一般是因为有些计算机账户是没有权限创建 HttpListener服务, 但是可以注册一些规则 ...

  3. Ubuntu下安装Naginx, PHP5(及PHP-FPM),MySQL

    一:安装前做个简单的说明 二:安装MySQL 三:安装Nginx 四:安装PHP5 五:配置 nginx,以下是我本机的配置文件. 六:让MySQL支持PHP5 七:配置PHP-FPM 八:在/etc ...

  4. 【代码笔记】iOS-iCarouselDemo

    一,效果图. 二,工程图. 三,代码. RootViewController.h RootViewController.m myCell.h #import <UIKit/UIKit.h> ...

  5. socket编程listen函数限制连接数的解决方案

    函数原型: int listen(int sockfd, int backlog); 当服务器编程时,经常需要限制客户端的连接个数,下面为问题分析以及解决办法: 下面只讨论TCP  UDP不做讨论(很 ...

  6. 2.1 CMMI2级——7个PA简述

    摘要: 阶段式的CMMI没有1级,最开始的级别就是2级.一个处于“无序化”生产的软件公司,要进行过程改进,首要是改进什么呢?2级告诉你,我们需要从计划.计划跟踪.需求管理.采购.度量.配置管理.质量保 ...

  7. (ios)MPMoviePlayerController首次播放视频的时候,没有控制条

    问题: 在视频播放时,现在控制条采用磨砂的效果,会遮罩部分视频 解决思路 1 播放器直接设置不带控制条,在app在 Foreground状态,默认播放器暂停,这样需要在获得Foreground事件,进 ...

  8. SQL Server中的锁的简单学习

    简介 在SQL Server中,每一个查询都会找到最短路径实现自己的目标.如果数据库只接受一个连接一次只执行一个查询.那么查询当然是要多快好省的完成工作.但对于大多数数据库来说是需要同时处理多个查询的 ...

  9. Java触发器CronTrigger

    摘要:如果需要像日历那样按日程来触发任务,而不是像SimpleTrigger 那样每隔特定的间隔时间触发,CronTriggers通常比SimpleTrigger更有用. 一.web.xml: < ...

  10. 0010《SQL必知必会》笔记06-表的修改与删除

    1.表的修改: 1.1 删除列:ALTER TABLE 表名 DROP COLUMN 列名 1.2 添加列:ALTER TABLE 表名 ADD(列名 数据类型) 1.3 修改列名:ALTER TAB ...