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' ,走 ...
随机推荐
- Hint
select /*+ first_rows(20) */ * from t where id<20 --分页 select /*+ all_rows */ * from ...
- windows Azure 域名绑定
windows Azure 的虚拟机的ip是会变化的,比如你关机.所以绑定域名用A记录就不太可靠. 你新建虚拟机的同时,也会新建一个云服务,给你一个类似XX.cloudapp.net的二级域名. 这样 ...
- 如果类的属性是copy的NSString类型 用set方法拦截时
@property (nonatomic, copy) NSString *badgeValue; - (void)setBadgeValue:(NSString *)badgeValue { _ba ...
- PostgreSQL Replication之第十二章 与Postgres-XC一起工作(6)
12.6 添加节点 Postgres-XC允许您在那个过程中的任何一个时间点添加新的服务器到计划中.所有您需要做的是按照我们之前演示的设置一个节点,并在 控制器上调用CREATE NODE.然后,该系 ...
- Leetcode: Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- win32 listbox
real-time refresh: the scrollbar will jump when the listbox refresh change color: how to change the ...
- poj: 2262
简单题 #include <iostream> #include <stdio.h> #include <string> #include <stack> ...
- list和map的区别
list和map的区别 list-->list是对象集合,允许对象重复 map-->map是键值对的集合,不允许key重复
- snmp getTable demo :iftable ipAddresstable
package org.huangxf.snmp.test; import java.io.IOException; import java.util.List; import org.snmp4j. ...
- 数据可视化工具zeppelin安装
介绍 zeppelin主要有以下功能 数据提取 数据发现 数据分析 数据可视化 目前版本(0.5-0.6)之前支持的数据搜索引擎有如下 安装 环境 centOS 6.6 编译准备工作 sudo yum ...