POJ 1573 (13.10.11)
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)
题意: 不难理解, 我简单说说, 给出r行c列, 然后从第一行的第i个位置开始走
每个位置有标记E W S N, 分别表示向东, 向西, 向南, 向北
然后, 有两种情况, 一种能走出去, 一种是陷入死循环
做法: 每一步都标上是第几步走的就OK了~ 然后按题意模拟~
AC代码:
#include<stdio.h> char str[100][100]; int main() {
int r, c, in;
while(scanf("%d %d %d", &r, &c, &in) != EOF) {
if(r == 0 && c ==0 && in == 0)
break;
getchar();
for(int i = 0; i < r; i++)
gets(str[i]);
int tr, tc, loop;
int step = 0;
int mark = 1;
tr = 0; tc = in-1; while(1) {
if(str[tr][tc] == 'E') {
step++;
str[tr][tc] = step + '0';
if(tc == c-1)
break;
else
tc = tc + 1;
}
else if(str[tr][tc] == 'W') {
step++;
str[tr][tc] = step + '0';
if(tc == 0)
break;
else
tc = tc - 1;
}
else if(str[tr][tc] == 'S') {
step++;
str[tr][tc] = step + '0';
if(tr == r-1)
break;
else
tr = tr + 1;
}
else if(str[tr][tc] == 'N') {
step++;
str[tr][tc] = step + '0';
if(tr == 0)
break;
else
tr = tr - 1;
}
else {
mark = 0;
loop = str[tr][tc] - '0' - 1;
printf("%d step(s) before a loop of %d step(s)\n", loop, step - loop);
break;
}
}
if(mark == 1)
printf("%d step(s) to exit\n", step);
}
return 0;
}
POJ 1573 (13.10.11)的更多相关文章
- Robot Motion 分类: POJ 2015-06-29 13:45 11人阅读 评论(0) 收藏
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11262 Accepted: 5482 Descrip ...
- MAC 下编译 ANDROID P 源码 提示 internal error: Could not find a supported mac sdk: ["10.10" "10.11" "10.12" "10.13"]
MAC 下编译 ANDROID P 源码出现下面的问题: ninja: no work to do. [21/21] out/soong/.bootstrap/bin/soong_build out/ ...
- 剑指offer19:按照从外向里以顺时针的顺序依次打印出每一个数字,4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
1 题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印 ...
- fastjson反序列化LocalDateTime失败的问题java.time.format.DateTimeParseException: Text '2019-05-24 13:52:11' could not be parsed at index 10
本地java类 import org.springframework.format.annotation.DateTimeFormat; import java.time.LocalDateTime; ...
- 战神Z7 D2安装黑苹果OS X El Capitan 10.11.2
安装之初状态:两块硬盘,都是MBR格式分区,一块是机械硬盘,安装了WIN7 32位和Linux,一块是SSD,安装的是WIN7 64位与WIN10 64位以前玩过Mavericks,安装的就是在硬盘的 ...
- Windows下 VM12虚拟机安装OS X 10.11 和VM TOOLS
Windows下虚拟机安装Mac OS X —– VMware Workstation12安装Mac OS X 10.11 本文即将介绍WIN虚拟MAC的教程.完整详细教程(包含安装中的一些问题) [ ...
- ubuntu 13.10 mono asp.net服务 安装
ubuntu 13.10 从官方文档http://www.mono-project.com/Mod_mono 可看到 Mod_Mono is an Apache 2.0/2.2/2.4.3 modul ...
- Fix catalyst driver in Ubuntu 13.04 / 13.10
Fix catalyst driver in Ubuntu 13.04 / 13.10(墙外文章备份) 1. Introduction I found lots of people strugglin ...
- 模拟 POJ 1573 Robot Motion
题目地址:http://poj.org/problem?id=1573 /* 题意:给定地图和起始位置,robot(上下左右)一步一步去走,问走出地图的步数 如果是死循环,输出走进死循环之前的步数和死 ...
随机推荐
- SQL大数据操作统计
SQL大数据操作统计 1:select count(*) from table的区别SELECT object_name(id) as TableName,indid,rows,rowcnt FROM ...
- UVa 10294 (Pólya计数) Arif in Dhaka (First Love Part 2)
Burnside定理:若一个着色方案s经过置换f后不变,称s为f的不动点,将置换f的不动点的数目记作C(f).等价类的数目等于所有C(f)的平均值. 一个项链,一个手镯,区别在于一个能翻转一个不能,用 ...
- objective-c里的方法指针IMP的用法
SGPopSelectView.h @interface SGPopSelectView : UIView @property (nonatomic, assign) SEL selector; @p ...
- 【转】mac终端安装node时候,显示“-bash: brew: command not found”,怎么解决?
原文网址:https://segmentfault.com/q/1010000004221389/a-1020000004221408 mac终端安装node时候,显示“-bash: brew: co ...
- 五分钟solr4.5教程(搭建、运行)
环境要求 jdk1.6及以上版本 solr发布版本 下载地址 http://lucene.apache.org/solr/mirrors-solr-latest-redir.html? 启动solr ...
- Oracle :一次数据库连接,返回多个结果集
1. 一次数据库连接,返回多个结果集 1.1 建立包规范 create or replace package QX_GDJTJ is -- Author : xxx -- Created : 2012 ...
- [C++]cin读取回车键
最近碰到一个问题,就是从控制台读取一组数,如: 12 23 34 56 若是使用 int data; while ( cin >> data ) {//...} 当回车后,不能有效转换到后 ...
- Linux+Apache+Tomcat集群配置
参考: http://blog.csdn.net/bluishglc/article/details/6867358# http://andashu.blog.51cto.com/8673810/13 ...
- Multiple View Geometry in Computer Vision Second Edition by Richard Hartley 读书笔记(二)
// Chapter 2介绍的是2d下的投影变换,摘录下了以下定理 Result 2.1. The point x lies on the line l if and only if xTl = 0. ...
- MVC & MVVM
什么是MVC,什么是MVVM? 面向过程 --> 面向对象 --> MVC --> MV* 面向过程: 开发人员按照需求逻辑顺序开发代码逻辑,主要思维模式在于如何实现.先细节,后整体 ...