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
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
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的更多相关文章
-
POJ1573——Robot Motion
Robot Motion Description A robot has been programmed to follow the instructions in its path. Instruc ...
-
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 ...
随机推荐
-
ASP.NET收发邮件
在.NET中常见到在线发邮件的实例,网站加上这个功能可以方便站长与用户的交流. NET 中发邮件有时候会用到IIS组件中的邮件服务器,不过复杂.对虚拟主机的配置也较麻烦, 也可用第三方组件比如Jmai ...
-
linux expect详解(ssh自动登录)
shell脚本实现ssh自动登录远程服务器示例: #!/usr/bin/expect spawn ssh root@192.168.22.194 expect "*password:&quo ...
-
AppStore ipa (苹果内购)笔记
内购示意图 准备条件 苹果的开发者证书,已经为应用启用App内购,并在Xcode更新配置文件 itunes store设置 itunes中创建App及其它设置 参考:iOS应用程序内购/内付费(一) ...
-
drupal7 Views Slideshow 简单教程
一.下载安装(略) 二.内容类型建立(过程略,首页幻灯),字段建立(过程略)主要有2个字段,图片字段 和 指向链接字段 三.view 1.建立一个新的view,名称为frontbanner 显示为内容 ...
-
eclipse新建一个Android项目,就会报错android.support.v7.app.ActionBarActivity
解决方法: 今天被这个问题折腾了一下,最后终于找到了解决办法. 产生这个问题,是因为你升级了ADT到version 22,但是还需要升级SDK Tools,Platform Tools,Build T ...
-
NYOJ 47-过河问题
点击打开链接 过河问题 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描述 在漆黑的夜里,N位旅行者来到了一座狭窄而且没有护栏的桥边.如果不借助手电筒的话,大家是无论如何也不 ...
-
ASP.NET-FineUI开发实践-12
1.网上找到了行合并的示例,extjs写的,我把它挪过来改了下,FineUI也能用,就是只能放着看,选择和编辑行扩展列没有测试,放出来大家看着用吧. <script> F.ready(fu ...
-
doT js模板入门
doT.js github地址: doT.js 官方站点 实例1:简单 <!DOCTYPE html> <html lang="en"> <head& ...
-
C++中发声函数Beep详解
By zhcs 以前,我听过一个神犇用C++函数做的音乐,当时的心里就十分激动:哇,好厉害啊,好神啊. 这次,我终于通过自己无助的盲目的摸索.研究,写出了这篇文章(此时我的内心是鸡冻的233) 下面是 ...
-
JavaEE开发之记事本完整案例(SpringBoot + iOS端)
上篇博客我们聊了<JavaEE开发之SpringBoot整合MyBatis以及Thymeleaf模板引擎>,并且在之前我们也聊了<Swift3.0服务端开发(五) 记事本的开发(iO ...