POJ 1573 Robot Motion
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 12978 | Accepted: 6290 |
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
Output
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
#include <cstdio>
#include <cstring> char s[15][15];
int n, m , start;
int dis[15][15]; bool inGrid(int x, int y)
{
return 1 <= x && x <= n && 1 <= y && y <= m;
} void solve()
{
memset(dis, 0, sizeof(dis));
int time = 0;
int x = 1, y = start;
while(1){
if(!inGrid(x, y)){
printf("%d step(s) to exit\n", time);
break;
}
if(dis[x][y] != 0){
printf("%d step(s) before a loop of %d step(s)\n", dis[x][y]-1, time-dis[x][y]+1);
break;
}
dis[x][y] = ++time;
if(s[x][y] == 'N') --x;
else if(s[x][y] == 'S') ++x;
else if(s[x][y] == 'E') ++y;
else if(s[x][y] == 'W') --y;
}
} int main()
{
while(scanf("%d%d%d", &n, &m, &start), n){
for(int i = 1; i <= n; ++i)
scanf("%s", &s[i][1]);
solve();
}
return 0;
}
POJ 1573 Robot Motion的更多相关文章
- 模拟 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 ...
- POJ 1573 Robot Motion(模拟)
题目代号:POJ 1573 题目链接:http://poj.org/problem?id=1573 Language: Default Robot Motion Time Limit: 1000MS ...
- poj 1573 Robot Motion【模拟题 写个while循环一直到机器人跳出来】
...
- POJ 1573 Robot Motion 模拟 难度:0
#define ONLINE_JUDGE #include<cstdio> #include <cstring> #include <algorithm> usin ...
- Poj OpenJudge 百练 1573 Robot Motion
1.Link: http://poj.org/problem?id=1573 http://bailian.openjudge.cn/practice/1573/ 2.Content: Robot M ...
- poj 1573 Robot Motion_模拟
又是被自己的方向搞混了 题意:走出去和遇到之前走过的就输出. #include <cstdlib> #include <iostream> #include<cstdio ...
- PKU 1573 Robot Motion(简单模拟)
原题大意:原题链接 给出一个矩阵(矩阵中的元素均为方向英文字母),和人的初始位置,问是否能根据这些英文字母走出矩阵.(因为有可能形成环而走不出去) 此题虽然属于水题,但是完全独立完成而且直接1A还是很 ...
- 【POJ - 1573】Robot Motion
-->Robot Motion 直接中文 Descriptions: 样例1 样例2 有一个N*M的区域,机器人从第一行的第几列进入,该区域全部由'N' , 'S' , 'W' , 'E' ,走 ...
随机推荐
- java基础知识回顾之---java String final类普通方法的应用之“模拟字符串Trim方法”
/* * 4,模拟一个trim功能一致的方法.去除字符串两端的空白 * 思路: * 1,定义两个变量. * 一个变量作为从头开始判断字符串空格的角标.不断++. * 一个变量作为从尾开始判断字符串空 ...
- SDUT1591交叉排序
http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=1591&cid=1187 #include<cstdio> #include& ...
- CLR 读书笔记
http://www.cnblogs.com/wang_yb/tag/CLR%20via%20C%23%E7%AC%94%E8%AE%B0/
- cojs 疯狂的重心 疯狂的机器人 题解报告
疯狂的重心 话说做过幻想乡战略游戏的人应该很容易切掉这道题目吧 我们考虑一棵树如果添加了一个叶子,那么其重心最多向叶子方向移动1的距离 而是否移动我们只需要记录子树中有多少个点就可以判断啦 也就是说这 ...
- 【Linux常识篇(2)】理解inode
inode是什么? 理解inode,要从文件储存说起.文件储存在硬盘上,硬盘的最小存储单位叫做"扇区"(Sector).每个扇区储存512字节(相当于0.5KB). 操作系统读取硬 ...
- Hibernate中createCriteria即QBC查询的详细用法 .Hibernate中createCriteria即QBC查询的详细用法 .
现在假设有一个Student类,内有id,name,age属性String hql = "from Student s";按照以前的做法,我们通常是Query query = se ...
- iOS开发 -- 发送JSON数据给服务器
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 1.URL NSURL *url = [NSURL URLW ...
- VirtualUI - Convert your Windows App to HTML5
http://www.cybelesoft.com/thinfinity/virtualui/
- JavaScript基础精华03(String对象,Array对象,循环遍历数组,JS中的Dictionary,Array的简化声明)
String对象(*) length属性:获取字符串的字符个数.(无论中文字符还是英文字符都算1个字符.) charAt(index)方法:获取指定索引位置的字符.(索引从0开始) indexOf(‘ ...
- Android scrollview嵌套listview运行后最先显示出来的位置不在顶部而是中间问题
scrollview里面嵌套了一个listview ,通过设置一个方法设置了listview的高度 现在的情况就是进到这个界面的时候看到的不是最上面 而是中间 ,该问题的解决办法为: mScrollV ...