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' ,走 ...
随机推荐
- 12C RAC中的一个数据库实例自动crash并报ORA-27157、ORA-27300等错误
rhel7.2上安装12C RAC数据库后,其中一个数据库实例经常会自动crash.查看alert日志发现以下错误信息: Errors in file /d12/app/oracle/diag/rdb ...
- tomcat deploy部署项目三种方法
1.将应用文件夹或war文件直接copy到tomcat的webapps目录下,这样tomcat启动的时候会将webapps目录下的文件夹或war文件的内容当成应用部署.这种方式最简单且无须书写任何配置 ...
- tomcat部署方法总结
可以参考之前的:http://www.cnblogs.com/youxin/archive/2013/01/18/2865814.html 在Tomcat中部署Java Web应用程序有两种方式:静态 ...
- 转:Python requests 快速入门
迫不及待了吗?本页内容为如何入门Requests提供了很好的指引.其假设你已经安装了Requests.如果还没有, 去 安装 一节看看吧. 首先,确认一下: ·Requests 已安装 ·Reques ...
- hdu 2846 Repository
http://acm.hdu.edu.cn/showproblem.php?pid=2846 Repository Time Limit: 2000/1000 MS (Java/Others) ...
- SpringMvc的数据绑定流程
在SpringMvc中会将来自web页面的请求和响应数据与controller中对应的处理方法的入参进行绑定,即数据绑定.流程如下: -1.SpringMvc主框架将ServletRequest对象及 ...
- EBS多OU和多帐套客户化总结
(一) 多OU总结 . Form多OU实现 ) 创建一个Table,以CUX_AP_CHECK_HEADER_ALL为例 ) 创建Table的两个Synonym(一个不含_ALL,一个以_ALL结尾) ...
- 13.熟悉JDK的配置,环境变量
已经做烂的东西,公司的新人环境配置手册文档Java方面的就是我写的,有意的留邮箱,很详细
- PTPX中的clock tree与LP design
PTPX在加入CPF/UPF这样的文件后,可以分析multi-voltage,power-gating这样的设计. 针对某个power rail的cell,PTPX支持进行annotate. set_ ...
- MyEclipse启动失败
日志的一部分: !SESSION 2014-09-24 11:47:03.156 -----------------------------------------------eclipse.buil ...