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' ,走 ...
随机推荐
- cache与SDRAM
hugohong hugohong 本版等级: #2 得分:20回复于: 2009-04-19 21:51:03 牛人说的,拿出来分享一下:cache是高速缓冲, 解决高速cpu和相对低速sdra ...
- Linux C进程内存布局
当程序文件运行为进程时,进程在内存中获得空间.这个空间是进程自己的内存空间.每个进程空间按照如下方式分为不同区域: 进程内存空间布局图 text:代码段.存放的是程序的全部代码(指令),来源于二进制可 ...
- FlashBuilder 4.7 win64 和 mac版 下载地址
win64 http://trials3.adobe.com/AdobeProducts/FLBR/4_7/win64/FlashBuilder_4_7_LS10_win64.exe mac http ...
- The golden ratio: 1.618
http://www.chinaz.com/design/2015/1109/467968_2.shtml The golden ratio: 1.618 a/b=b/(a+b) The Fibona ...
- codevs 1202 求和
http://codevs.cn/problem/1202/ 1202 求和 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze 题解 题目描述 D ...
- C++之路进阶——codevs3287(货车运输)
3287 货车运输 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description A 国有 n ...
- SpringMvc:处理模型数据
SpringMvc提供了以下途径输出模型数据: -ModelAndView:处理方法返回值类型为ModelAndView,方法体即可通过该对象添加模型数据 -Map或Model:入参为org.spri ...
- mysql之消息队列
消息队列:在消息的传输过程中保存消息的容器. 消息队列管理器在将消息从它的源中继到它的目标时充当中间人.队列的主要目的是提供路由并保证消息的传递:如果发送消息时接收者不可用,消息队列会保留消息,直到可 ...
- php session 跨子域和跨服务器解决方式
Session主要分两部分: 一个是Session数据,该数据默认情况下是存放在服务器的tmp文件下的,是以文件形式存在 另一个是标志着Session数据的Session Id,Sess ...
- paper 6:支持向量机系列三:Kernel —— 介绍核方法,并由此将支持向量机推广到非线性的情况。
前面我们介绍了线性情况下的支持向量机,它通过寻找一个线性的超平面来达到对数据进行分类的目的.不过,由于是线性方法,所以对非线性的数据就没有办法处理了.例如图中的两类数据,分别分布为两个圆圈的形状,不论 ...