poj1573 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
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 ...
随机推荐
- atitit.数据验证--db数据库数据验证约束
atitit.数据验证--db数据库数据验证约束 1. 为了加强账户数据金额的安全性,需要增加验证字段..1 2. 创建帐户1 3. 更改账户2 4. ---code3 5. --fini4 1. 为 ...
- [Android]Activity启动过程
Android系统启动加载流程: 参考图 Linux内核加载完毕 启动init进程 init进程fork出zygote进程 zygote进程在ZygoteInit.main()中进行初始化的时候for ...
- 停止运行ExecutorService中的线程
while(true){ try { sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch bloc ...
- iOS数据缓存及YYCache的实现分析
1. 什么是cache cache就是缓存的意思. 计算机上的cache就是高速缓存,计算机组成课程里的定义是,存在于主存和CPU之间,主要用于解决CPU处理数据的速度远远大于读取主存数据的速度. 手 ...
- android SharedPreferences 轻量级存储!
首先在当前进程也就是当前的项目里面进行存储 SharedPreferences.Editor editor = mContext.getSharedPreferences("tvplay&q ...
- 运算符&,|,^
1.&按位“与”的计算是把两个数字分别写成二进制形式,然后按照每一位进行比较,&计算中,只要有一个是0就算成02.|运算转换成2进制进行比较,两个位只要有一个为1,那么结果就是1,否则 ...
- 用GCD线程组与GCD信号量将异步线程转换为同步线程
有时候我们会碰到这样子的一种情形: 同时获取两个网络请求的数据,但是网络请求是异步的,我们需要获取到两个网络请求的数据之后才能够进行下一步的操作,这个时候,就是线程组与信号量的用武之地了. #impo ...
- eclipse maven 插件 安装 和 配置
eclipse 安装插件的方式最常见的有两种: 离线安装,用 link 的方式来安装,这种方式可拔性更好,可以随时将插件插上和拔下,非常方便. link 离线安装 eclipse maven 插件 ...
- Computer Network and Internet(1)
计算机网路相关的教材很少,TCP/IP,HTTP 协议非常多,很难找到一个合适的材料去学习. <计算机网络>自上而下方法是这个方面的经典之作. 1.what is internet? 1. ...
- Jmeter教程索引贴
新的一年即将到来,不知不觉2015年自己在Jmeter方面总结的文章有十几篇,在此汇总一下,顺便也算是个总结吧.2016年,继续学习技术,总结,写文章. 一.基础部分: 使用Jmeter进行http接 ...