ACM题目————Robot Motion
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
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
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 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, 分别表示向东, 向西, 向南, 向北 然后, 有两种情况, 一种能走出去, 一种是陷入死循环。要求输出结果! 解法一:(直接模拟)
#include<stdio.h> char str[][]; int main() {
int r, c, in;
while(scanf("%d %d %d", &r, &c, &in) != EOF) {
if(r == && c == && in == )
break;
getchar();
for(int i = ; i < r; i++)
gets(str[i]);
int tr, tc, loop;
int step = ;
int mark = ;
tr = ; tc = in-; while() {
if(str[tr][tc] == 'E') {
step++;
str[tr][tc] = step + '';
if(tc == c-)
break;
else
tc = tc + ;
}
else if(str[tr][tc] == 'W') {
step++;
str[tr][tc] = step + '';
if(tc == )
break;
else
tc = tc - ;
}
else if(str[tr][tc] == 'S') {
step++;
str[tr][tc] = step + '';
if(tr == r-)
break;
else
tr = tr + ;
}
else if(str[tr][tc] == 'N') {
step++;
str[tr][tc] = step + '';
if(tr == )
break;
else
tr = tr - ;
}
else {
mark = ;
loop = str[tr][tc] - '' - ;
printf("%d step(s) before a loop of %d step(s)\n", loop, step - loop);
break;
}
}
if(mark == )
printf("%d step(s) to exit\n", step);
}
return ;
}
解法二:(DFS)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char map[][] ; //用来记录图形
int des[][] ; //用来记录该点所走的步数,以及判断是否构成循环
int a ,b , l , count , f , h;//a,b记录输入的行列数,count记录循环频数,h,循环前的步数
void DFS( int m , int n , int sp )
{
if( m < || m >= a || n < || n >= b )//判断是否出去
{
f = ;
count = sp ;
return ;
}
if( des[m][n] )//判断是否循环
{
f = ;
count = des[m][n];
h = sp - des[m][n] ;
return ;
}
des[m][n] = sp ; //把步数赋给des
if( map[m][n] == 'N' )
DFS( m- , n , sp+) ;
if( map[m][n] == 'S' )
DFS( m+ , n , sp+ ) ;
if( map[m][n] == 'E' )
DFS( m , n+ , sp+ ) ;
if( map[m][n] == 'W' )
DFS( m , n- , sp+ ) ;
}
int main( )
{
while( scanf("%d%d" , &a, &b ) , a || b )
{
scanf("%d" , &l ) ;
memset( des , , sizeof( des ) ) ;
getchar ( ) ;
count = ;
f = ;
h = ;
for( int i = ; i < a ; i++ )
{
for( int j = ; j < b ; j++ )
scanf("%c" , &map[i][j] ) ;
getchar();
} DFS ( ,l- , ) ;
if( f )
{
printf("%d step(s) to exit\n" , count- ) ;
}
else
printf("%d step(s) before a loop of %d step(s)\n" ,count- ,h ) ; } return ;
}
ACM题目————Robot Motion的更多相关文章
- [ACM] hdu 1035 Robot Motion (模拟或DFS)
Robot Motion Problem Description A robot has been programmed to follow the instructions in its path. ...
- 模拟 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 ...
- POJ1573——Robot Motion
Robot Motion Description A robot has been programmed to follow the instructions in its path. Instruc ...
- HDU-1035 Robot Motion
http://acm.hdu.edu.cn/showproblem.php?pid=1035 Robot Motion Time Limit: 2000/1000 MS (Java/Others) ...
- hdu-1573 Robot Motion
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10219 Accepted: 4977 Des ...
- HDOJ(HDU).1035 Robot Motion (DFS)
HDOJ(HDU).1035 Robot Motion [从零开始DFS(4)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DF ...
- poj 1573 Robot Motion【模拟题 写个while循环一直到机器人跳出来】
...
- 【POJ - 1573】Robot Motion
-->Robot Motion 直接中文 Descriptions: 样例1 样例2 有一个N*M的区域,机器人从第一行的第几列进入,该区域全部由'N' , 'S' , 'W' , 'E' ,走 ...
随机推荐
- bootstrap 时间选择器 datetime
$("'#datetimepicker").datetimepicker({ format: "yyyy-mm-dd hh:ii:ss",//设置时间格式,默认 ...
- 解决td标签上的position:relative属性在各浏览器中的兼容性问题
在css中的position属性规定了页面元素的定位类型,它有以下几个值: absolute:绝对定位,相对于static以外的第一个父元素进行定位: fixed:生成绝对定位的元素,相对于浏览器窗口 ...
- Java基础之处理事件——添加工具提示(Sketcher 9 with tooltips)
控制台程序. 在Java中实现对工具提示的支持是非常简单的,秘诀仍在我们一直使用的Action对象中.Action对象拥有存储工具提示文本的内置功能因为文本是通过SHORT_DESCRIPTION键提 ...
- Postgres数据库基本介绍
最近一直在做一个和PostgreSQL数据库相关的项目,把自己在这个过程中学习的知识记录下来.关于PostgreSQL数据库网上已经有太多的相关介绍了,为了博文的系统性还是先看一下维基百科对Postg ...
- linux [Fedora] 下的 "飞秋/飞鸽传书"
官方网址: http://www.msec.it/blog/?page_id=11 http://software.opensuse.org/download.html?project=home:co ...
- csuoj 1120: 病毒
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1120 1120: 病毒 Time Limit: 3 Sec Memory Limit: 128 ...
- 关于gem包下载网站的说明
- mysql之消息队列
消息队列:在消息的传输过程中保存消息的容器. 消息队列管理器在将消息从它的源中继到它的目标时充当中间人.队列的主要目的是提供路由并保证消息的传递:如果发送消息时接收者不可用,消息队列会保留消息,直到可 ...
- Codeforces Round #312 (Div. 2) E. A Simple Task
题目大意就是给一个字符串,然后多个操作,每次操作可以把每一段区间的字符进行升序或者降序排序,问最终的字符串是多少. 一开始只考虑字符串中字符'a'的情况,假设操作区间[L,R]中有x个'a',那么一次 ...
- Spring 中的 Bean 配置
内容提要 •IOC & DI 概述 •配置 bean –配置形式:基于 XML 文件的方式:基于注解的方式 –Bean 的配置方式:通过全类名(反射).通过工厂方法(静态工厂方法 & ...