poj1573&&hdu1035 Robot Motion(模拟)
转载请注明出处:http://blog.csdn.net/u012860063?
viewmode=contents
题目链接:
HDU: pid=1035">http://acm.hdu.edu.cn/showproblem.php?pid=1035
POJ: id=1573">http://poj.org/problem? id=1573
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
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
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)
纯模拟题;
代码例如以下:
#include <iostream>
using namespace std;
#include <cstring>
#define M 1000
char mat[M][M];
int re[M][M];
int n, m, s;
int ans, i, j, k;
void F()
{
i = 1; j = s;
k = 0;
for(int l = 1; ; l++)
{
if(re[i][j] != 0)
{
ans = k - re[i][j];
break;
}
if(i>n || i<1 || j>m || j<1)
{
break;
}
if(mat[i][j] == 'N')
{
k++;
re[i][j] = k;
i-=1;
continue;
}
if(mat[i][j] == 'S')
{
k++;
re[i][j] = k;
i+=1; continue;
}
if(mat[i][j] == 'W')
{
k++;
re[i][j] = k;
j-=1;
continue;
}
if(mat[i][j] == 'E')
{
k++;
re[i][j] = k;
j+=1;
continue;
}
}
}
int main()
{
while(cin>>n>>m>>s)
{
ans = 0;
if(n == 0 && m == 0 && s == 0)
break;
memset(mat,0,sizeof(mat));
memset(re,0,sizeof(re));
for(i = 1; i <= n; i++)
{
for(j = 1; j <= m; j++)
{
cin>>mat[i][j];
}
}
F();
if(ans == 0)
cout<<k<<" step(s) to exit"<<endl;
else
cout<<re[i][j]-1<<" step(s) before a loop of "<<ans+1<<" step(s)"<<endl;
}
return 0;
}
poj1573&&hdu1035 Robot Motion(模拟)的更多相关文章
- HDU-1035 Robot Motion 模拟问题(水题)
题目链接:https://cn.vjudge.net/problem/HDU-1035 水题 代码 #include <cstdio> #include <map> int h ...
- [ACM] hdu 1035 Robot Motion (模拟或DFS)
Robot Motion Problem Description A robot has been programmed to follow the instructions in its path. ...
- HDU-1035 Robot Motion
http://acm.hdu.edu.cn/showproblem.php?pid=1035 Robot Motion Time Limit: 2000/1000 MS (Java/Others) ...
- hdu1035 Robot Motion (DFS)
Robot Motion Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- POJ-1573 Robot Motion模拟
题目链接: https://vjudge.net/problem/POJ-1573 题目大意: 有一个N*M的区域,机器人从第一行的第几列进入,该区域全部由'N' , 'S' , 'W' , 'E' ...
- POJ1573 Robot Motion(模拟)
题目链接. 分析: 很简单的一道题, #include <iostream> #include <cstring> #include <cstdio> #inclu ...
- HDU1035 Robot Motion
Problem Description A robot has been programmed to follow the instructions in its path. Instructions ...
- POJ 1573 Robot Motion 模拟 难度:0
#define ONLINE_JUDGE #include<cstdio> #include <cstring> #include <algorithm> usin ...
- poj1573 Robot Motion
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12507 Accepted: 6070 Des ...
随机推荐
- Web开发找错基本思路
WEB开发主要是两个交互(B/S数据) 浏览器: 1 包含html.css.js等客户端代码运行错误. 传输端: 1 路径引入错误,常用插件的引入(artDialog.Ztree.JQuery等). ...
- mysql支持emoji解决办法
mysql显示不了emoji表情或者显示??,原因这里不解释,直接说解决办法.(主要就是修改utf8mb4) 1.修改表 ALTER TABLE `TABLE_NAME` CHARACTER SET ...
- linux命令行后台运行与调回
直接ctrl+z 这个是暂时到后台执行 要调回来 输入 fg
- 64位win8 配置Apache2.4+mod_msgi4.4.21+django1.8.6+python3.4
学习了很多前人分享的资料,整理如下: 安装步骤: 一.安装python3.4 下载 python-3.4.3.amd64.msi 直接安装 地址:https://www. ...
- pkusc2015
Mex 题目大意:给出一个序列\(a\),定义\(f(l,r)\)为集合{\(a_l, a_{l+1}, -, a_r\)}的sg值,求\(\sum_i \sum_{j(i\leq j)} f(i,j ...
- 免费利用网页版谷歌翻译实现任意语言转换php版
本文源发布地址: http://ourgarden.cn/2013/07/20/%E5%85%8D%E8%B4%B9%E5%88%A9%E7%94%A8%E7%BD%91%E9%A1%B5%E7%89 ...
- postgres-xc手册生成方法
步骤 检测编译环境 安装编译工具 编译 以上只在linux环境当中进行,本人所用系统ubuntu15.04 检测编译环境 在posgtgresql目录下运行./configure,并安装需要安 ...
- 【论文阅读】Parsing Clothing in Fashion Photographs(翻译与理解)
发表于2012年 作者:Kota Yamaguchi M.Hadi Kiapour Luis E.Ortiz Tamara L.Berg 摘要:展示了一个从时装图片中解析衣服的有效方法,提供了一个一般 ...
- Android中EditText,Button等控件的设置
EditText可以使用:TextView.setEnabled(true)来设置为可编辑,其实很简单,写在这里以便以后自己查看. Button设置可用性:setVisibility(View.VIS ...
- 感觉Release有时比Debug要健壮
评估文件夹大小的时候,直接跨线程操作UI界面,Debug崩溃,Release不崩溃. 更多的一种情况是,本机DEBUG下不崩溃,把RELEASE版本到别的机子上,立刻崩溃(登录框的进度条的对象为空,仍 ...