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' ,走 ...
随机推荐
- 异常处理原则--good
异常机制是现代主流语言的标配,但是异常处理问题虽然已经被讨论很多,也有很多经典书籍的论述,却一直都充满争议.很多人都觉得异常处理很难拿捏,同时也难以理解一些语言或库的异常处理设计.我使用Java近10 ...
- docker offical docs:Working with Containers
enough ---------------------------------------------------------------------------------- Working wi ...
- Latex技巧
文件.tex 文件打开 error reading 版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://www.blogbus.com/jzhao-logs/27226136 ...
- Lintcode: Rehashing
The size of the hash table is not determinate at the very beginning. If the total size of keys is to ...
- Web TreeView 加载级联数据
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { dt = BLL.GetTable(); LoadL ...
- [原创] 分享一些linux教程
书<鸟哥的linux私房菜第三版>,链接:http://pan.baidu.com/s/1i3femnr 配套视频,链接:http://pan.baidu.com/s/1v72xw --- ...
- 通过反射封装JDBC
具体上代码我的BaseDao: public class BaseDao<T> { private Class clazz; private Properties pro=null; ...
- [php] PHPExcel插入图片
其它的代码就不贴了,直接上关键代码: $objPHPExcel = new PHPExcel(); $objPHPExcel->setActiveSheetIndex(0); $objActSh ...
- Eclipse字符集设置方式
默认的字符集是GBK 1.windows->Preferences...打开"首选项"对话框,左侧导航树,导航到general->Workspace,右侧Text fi ...
- 【fedora】设置fedora系统
1.安装自动选择最快源的插件fastestmirror: #sudo yum -y install axel yum-plugin-fastestmirror && sudo yum ...