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' ,走 ...
随机推荐
- SPOJ MULTQ3 7299 Multiples of 3 (区间更新)
题目连接:http://www.spoj.com/problems/MULTQ3/ #include <iostream> #include <stdio.h> #includ ...
- C#图片上写文字
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...
- 【mongoDB运维篇④】Shard 分片集群
简述 为何要分片 减少单机请求数,降低单机负载,提高总负载 减少单机的存储空间,提高总存空间. 常见的mongodb sharding 服务器架构 要构建一个 MongoDB Sharding Clu ...
- JTable指定单元格加控件
原文链接:http://blog.csdn.net/transit136/article/details/2133638 JTable可以给表格的某一列加入控件,下面方法可以实现 try{ T ...
- iOS 开发--开源图片处理圆角
概述 开源项目名称:HYBImageCliped 当前版本:2.0.0 项目用途:可给任意继承UIView的控件添加任意多个圆角.可根据颜色生成图片且可带任意个圆角.给UIButton设置不同状态下的 ...
- Btrace入门到熟练小工完全指南
BTrace是神器,每一个需要每天解决线上问题,但完全不用BTrace的Java工程师,都是可疑的. BTrace的最大好处,是可以通过自己编写的脚本,获取应用的一切调用信息.而不需要不断地修改代码, ...
- 深度卷积神经网络用于图像缩放Image Scaling using Deep Convolutional Neural Networks
This past summer I interned at Flipboard in Palo Alto, California. I worked on machine learning base ...
- WCF-学习笔记概述之计算服务(1)
关于WCF的介绍,在此不再赘述,其他地方应有尽有.直接开始实例,第一个实例以一个简单的计算服务为例,本人是学习了蒋金楠的<WCF全面解析>. 1.构建解决方案 Interface:用于定义 ...
- html5 touch事件实现触屏页面上下滑动(一)
最近做的做那个app的项目由于用overflow:hidden导致了很多问题,于是决定研究下html5的touch事件.想找个全面点的帖子真是难死了,虽然好多关于html5 touch的文章但大多都是 ...
- Hbase总结(一)-hbase命令,hbase安装,与Hive的区别,与传统数据库的区别,Hbase数据模型
Hbase总结(一)-hbase命令 下面我们看看HBase Shell的一些基本操作命令,我列出了几个常用的HBase Shell命令,如下: 名称 命令表达式 创建表 create '表名称', ...