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. bzoj1630 [Usaco2007 Demo]Ant Counting

    Description Bessie was poking around the ant hill one day watching the ants march to and fro while g ...

  2. 将Oracle JDBC驱动库安装到本地仓库

    1.为了添加详细版本信息,先查看当前Oracle数据库版本 select * from v$version; 查询返回如下信息 BANNER ----------------------------- ...

  3. 用到的Python运算符

    假设变量a为10,变量b为20. 算术运算符  比较运算符 赋值运算符 逻辑运算符 运算符优先级 对于逻辑运算符,not的优先级最大,or的优先级最小.它们三个的优先级排序为:not > and ...

  4. 安装jansson库【JSON库C语言版】

    本次操作在Ubuntu 14.04下进行,其他的系统大同小异,安装软件时请根据系统版本进行调整. 1.下载jansson源码: git clone https://github.com/akheron ...

  5. A Simple Problem with Integers(100棵树状数组)

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  6. C#静态构造函数和析构函数片段化认知

    一.静态构造函数 一个类可以有静态构造函数,实现如下源代码.静态构造函数有以下特性: 1).静态构造函数不能有修饰符(润饰符) 2).静态构造函数不能有参数 3).不能被调用——在实例化类的时候,静态 ...

  7. FP-Growth算法之频繁项集的挖掘(python)

    前言: 关于 FP-Growth 算法介绍请见:FP-Growth算法的介绍. 本文主要介绍从 FP-tree 中提取频繁项集的算法.关于伪代码请查看上面的文章. FP-tree 的构造请见:FP-G ...

  8. MySQL5.6 windows7下安装及基本操作

    图形界面安装MySQL5.6关于图形界面的安装,网上相关相关资料比较多,此处省略安装过程.安装过程中选择安装路径.所需组件及root账号密码.1.目前针对不同用户,MySQL提供了2个不同的版本:My ...

  9. apache 开启压缩功能

    apache如何开启压缩功能. 1,首先先确认是安装deflatte模块.如果未安装,可以重新编译apache添加参数--enable-deflate=shared ,或者扩展安装deflate模块, ...

  10. EF查询数据库框架的搭建

    一个简单的EF查询框架除了运行项目外,大概需要5个类库项目,当然这个不是一定要这样做,这可以根据自己的需要设置有多少个项目.这里介绍的方法步骤只适合EF零基础的人看看就是了. 在开始之前,先建立一个运 ...