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. Candy 解答

    Question There are N children standing in a line. Each child is assigned a rating value. You are giv ...

  2. Android应用程序请求SurfaceFlinger服务渲染Surface的过程分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/7932268 在前面一篇文章中,我们分析了And ...

  3. ORACLE物理存储结构

    1.查看数据库实例基本信息: SQL> SELECT DBID,NAME,CREATED,LOG_MODE,OPEN_MODE,FORCE_LOGGING,CURRENT_SCN,FLASHBA ...

  4. C# 多线程的坑 之 代码变序

    英文好的,可跳过,直接打开底部的“参考“链接. 代码变序--reordering of memory operations 大概4年前,阅读了这篇文章后http://www.albahari.com/ ...

  5. 使用FindControl("id")查找控件 返回值都是Null的问题

    做了一个通过字符串ID查找页面控件并且给页面控件赋值的功能,过程中遇到了this.FindControl("id")返回值都是Null的问题,记录一下解决办法. 问题的原因是我所要 ...

  6. Sublime Text 3中使用正则表达式删除空行

    Sublime Text 3 中使用正则表达式删除空行 Ctrl+H Find What: \n\n+ Replace With:\n

  7. java static关键字

    方便在没有创建对象的情况下来进行调用(方法/变量). 很显然,被static关键字修饰的方法或者变量不需要依赖于对象来进行访问,只要类被加载了,就可以通过类名去进行访问. static可以用来修饰类的 ...

  8. Struts2中使用Session的两种方法

    在Struts2里,如果需要在Action中使用到session,可以使用下面两种方式: 通过ActionContext 类中的方法getSession得到 Action实现org.apache.st ...

  9. Submission Details

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  10. win7笔记本电脑实现wifi共享

    前提条件:win 7系统,有wifi 同dos命令就可实现wifi共享 第一步: netsh wlan start hostednetwork pause 第二步: netsh wlan set ho ...