Problem 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
 
Sample Output
10 step(s) to exit
3 step(s) before a loop of 8 step(s)
 
Source
 

模拟题

AC代码:

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 16
#define inf 1e12
int n,m,st;
char mp[N][N];
int vis[N][N];
bool judge(int i,int j){
if(i< || i>=n || j< || j>=m) return true;
return false;
}
int main()
{
while(scanf("%d%d",&n,&m)==){
if(n== && m==) break;
memset(vis,,sizeof(vis));
memset(mp,'\0',sizeof(mp));
scanf("%d",&st);
st--;
for(int i=;i<n;i++){
scanf("%s",mp[i]);
}
/*for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
printf("%c",mp[i][j]);
}
}
*/ int i=,j=st;
int ans=;
int loops=-;
int L;
//vis[i][j]=1;
while(){
if(mp[i][j]=='W' && vis[i][j]==){
vis[i][j]=ans;
j--; //printf("%d %d\n",i,j);
}else if(mp[i][j]=='E' && vis[i][j]==){
vis[i][j]=ans;
j++;
//printf("%d %d\n",i,j); }else if(mp[i][j]=='N' && vis[i][j]==){
vis[i][j]=ans;
i--;
//printf("%d %d\n",i,j); }else if(mp[i][j]=='S' && vis[i][j]==){
vis[i][j]=ans;
i++;
//printf("%d %d\n",i,j); }
else if(vis[i][j]){
ans--;
loops = ans-vis[i][j]+;
L = vis[i][j];
break;
} else if(judge(i,j)){
//printf("%d %d\n",i,j);
ans--;
break;
}
ans++;
} if(loops==-){
printf("%d step(s) to exit\n",ans);
}else{
printf("%d step(s) before a loop of %d step(s)\n",L-,loops);
} }
return ;
}

hdu 1035 Robot Motion(模拟)的更多相关文章

  1. [ACM] hdu 1035 Robot Motion (模拟或DFS)

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

  2. HDOJ(HDU).1035 Robot Motion (DFS)

    HDOJ(HDU).1035 Robot Motion [从零开始DFS(4)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DF ...

  3. HDU 1035 Robot Motion(dfs + 模拟)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1035 这道题比较简单,但自己一直被卡,原因就是在读入mp这张字符图的时候用了scanf被卡. ...

  4. hdu 1035 Robot Motion(dfs)

    虽然做出来了,还是很失望的!!! 加油!!!还是慢慢来吧!!! >>>>>>>>>>>>>>>>> ...

  5. 题解报告:hdu 1035 Robot Motion(简单搜索一遍)

    Problem Description A robot has been programmed to follow the instructions in its path. Instructions ...

  6. (step 4.3.5)hdu 1035(Robot Motion——DFS)

    题目大意:输入三个整数n,m,k,分别表示在接下来有一个n行m列的地图.一个机器人从第一行的第k列进入.问机器人经过多少步才能出来.如果出现了循环 则输出循环的步数 解题思路:DFS 代码如下(有详细 ...

  7. hdoj 1035 Robot Motion

    Robot Motion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  8. HDU 1035(走迷宫 模拟)

    题意是给定初始位置在一个迷宫中按照要求前进,判断多少步能离开迷宫或者多少步会走入一个长达多少步的循环. 按要求模拟前进的位置,对每一步在 vis[ ] 数组中进行已走步数的记录,走出去或走到已走过的位 ...

  9. POJ 1573 Robot Motion 模拟 难度:0

    #define ONLINE_JUDGE #include<cstdio> #include <cstring> #include <algorithm> usin ...

随机推荐

  1. python多线程同步

    python多线程同步 作者:vpoet 日期:大约在夏季 import threading import time mylock = threading.RLock() num=0 class my ...

  2. linux 之 tar 命令

    通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候tar命令就是是必不可少的一个功能强大的工具.linux中最流行的tar是麻雀虽小,五脏俱全,功能强大. tar 命令可以为linu ...

  3. js中的注意事项(持续整理)

    1.兼容性 <div class="toutiao_r fl_r" id ="toutiao_r"></div> 这个div中有两个样式 ...

  4. Conquering Keokradong && Get the Containers(二分)

    Conquering Keokradong Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  5. static静态属性和静态方法的原理与调用技巧

    这篇文章主要介绍了php面向对象中static静态属性和静态方法的调用,实例分析了static静态属性和静态方法的原理与调用技巧,需要的朋友可以参考下     本文实例讲述了php中static静态属 ...

  6. 1. 用U盘安装Centos6.5 + Win7 双系统

    一. 用U盘安装Centos6.5 + Win7 双系统 准备工作:U盘(8G).需要安装的Centos6.5系统(64bit).EasyBCD(用来修复引导,否则开机只有一个系统).         ...

  7. ng-class用法

    在angular中为我们提供了3种方案处理class: 1:scope变量绑定.这种方案不推荐,因为scope里最好处理业务逻辑,不去管渲染的事.2:字符串数组形式.3:对象key/value处理. ...

  8. OpenLayers加载QQ地图(转)

    OpenLayers加载QQ地图 http://www.openlayers.cn/portal.php?mod=view&aid=4 2012-10-21 17:22| 发布者: admin ...

  9. thinkphp+redis实现秒杀功能

    好久没来整理文章了,闲了没事写篇文章记录下php+redis实现商城秒杀功能. 1,安装redis,根据自己的php版本安装对应的redis扩展(此步骤简单的描述一下) 1.1,安装 php_igbi ...

  10. java数据导出成 EXCEL

    /** * * @param out 输出流 * @param maplist 数据 * @param title 标题 * @param headers 表头 * @param keys 表头对应的 ...