1204. Maze Traversal
1204. Maze Traversal
Input
For this problem, you will input the dimensions of a maze, as two integer
values on the first line. Of the two numbers, the first is the number of rows
and the second is the number of columns. Neither the number of rows nor columns
will exceed 60.
Following these numbers will be a number of rows, as specified previously. In
each row there will be a character for each column, with the tow terminated by
the end of line. Blank spaces are corridors, asterisks are walls. There needn't
be any exits from the maze.
Following the maze, will be an initial row and column specified as two
integers on one line. This gives the initial position of the robot. Initially
the robot will be facing North (toward the first row).
The remaining input will consist of commands to the robot, with any amount of
interspersed white-space. The valid commands are:
R rotate the robot 90 degrees clockwise (to the right)
L rotate the robot
90 degrees counter-clockwise (to the left)
F move the robot forward unless a
wall prevents this in which case do nothing
Q quite the program, printing out
the current robot row, column and orientation.
Output
The final row and column must be integers separated by a space. The
orientation must be one of N, W, S, E and separated from the column by a
space.
Sample Input
7 8
********
* * * **
* * *
* * ** *
* * * *
* * **
********
2 4
RRFLFF FFR
FF
RFFQ
Sample Output
5 6 W 特别注意:
1、输入的行列号数跟数组内的下标数差一位,一定要注意转换。
2、读入字符串,特别是带有空白字符的字符串,一定还要注意回车符。
3、最后一定要注意输出最后一定要有一个回车符,不然会是PE错误,也就是输出格式错误。
printf("%d %d %c\n",robot.row+1,robot.col+1,robot.ori);
#include <stdio.h>
#include <string.h> int handle(char *ptr);
void move(char str); struct R{
int row;//行号
int col;//列号
char ori;//朝向
}robot;
char maze[][]; int main(void)
{
int row,col,i,j;
char run[]; scanf("%d %d",&row,&col);
getchar(); for(i=;i<row;i++){
for(j=;j<col;j++)
maze[i][j]=getchar();
getchar();
} scanf("%d %d",&robot.row,&robot.col);
getchar();//吸收回车
robot.ori = 'N';
robot.row--;
robot.col--;//跟数组匹配。 while(){
gets(run);
if(handle(run) == )
break;
memset(run,,sizeof(run));
} printf("%d %d %c\n",robot.row+,robot.col+,robot.ori); //system("PAUSE");
return ;
} int handle(char *ptr)
{
char *next = ptr;
char c= *next;
while((c != '\0')&&(c != 'Q')){
move(c);
next++;
c= *next;
}
if(*next == 'Q')
return ;
return ;
} void move(char str)
{
char s=str;
if((s == 'R')||(s == 'L')){
switch(robot.ori){
case 'N':
if(s == 'R')
robot.ori = 'E';
else
robot.ori = 'W';
break;
case 'E':
if(s == 'R')
robot.ori = 'S';
else
robot.ori = 'N';
break;
case 'S':
if(s == 'R')
robot.ori = 'W';
else
robot.ori = 'E';
break;
case 'W':
if(s == 'R')
robot.ori = 'N';
else
robot.ori = 'S';
break;
}
}
if(s == 'F'){
switch(robot.ori){
case 'N':
if(maze[robot.row-][robot.col] == ' ')
robot.row--;
break;
case 'E':
if(maze[robot.row][robot.col+] == ' ')
robot.col++;
break;
case 'S':
if(maze[robot.row+][robot.col] == ' ')
robot.row++;
break;
case 'W':
if(maze[robot.row][robot.col-] == ' ')
robot.col--;
break;
}
}
}
1204. Maze Traversal的更多相关文章
- UVa 10377 - Maze Traversal
題目:一個機器人在迷宮中行走,它的指令是方向控制(前進.左轉.右轉).給你初始位置和一些指令: 問最後停在那個位置. 分析:模擬.直接模擬就可以,注意一下細節. 假设,不能行走(邊界或者是墻壁)則停在 ...
- HOJ题目分类
各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...
- Morris post order traversal algorithm
Sept. 5, 2015 花时间把代码读明白, 比光看书强. 动手写代码, 改代码, 兴趣是最好的老师. 多记几个例子, 增加情趣. 举个例子关于中序遍历, 4 ...
- Backtracking algorithm: rat in maze
Sept. 10, 2015 Study again the back tracking algorithm using recursive solution, rat in maze, a clas ...
- Leetcode, construct binary tree from inorder and post order traversal
Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
随机推荐
- Android5.0如何正确启用isLoggable(二) 理分析
转自:http://www.it165.net/pro/html/201506/43374.html 概要 在上文<Android 5.0 如何正确启用isLoggable(一)__使用详解&g ...
- SQL Server数据库大型应用解决方案总结(转载)
转载地址:http://hb.qq.com/a/20120111/000216.htm 随着互联网应用的广泛普及,海量数据的存储和访问成为了系统设计的瓶颈问题.对于一个大型的互联网应用,每天百万级甚至 ...
- 排序练习【sdut 1582】【堆排序】
排序 Time Limit: 1000ms Memory limit: 32678K 有疑问?点这里^_^ 题目描述 给你N(N<=100)个数,请你按照从小到大的顺序输出. 输入 输入数 ...
- ArcGIS图层和要素的过滤显示
ArcGIS可以设置动态地图服务(ArcGISDynamicMapServiceLayer)显示哪些图层,也可以设置每个图层根据某个属性字段的某些条件来进行过滤显示. 1.设置显示的图层 主要是通过A ...
- SSIS 包单元测试检查列表
1. 使用脚本任务(Script tasks) 组建的时候,在日志里增加一些调试信息,例如变量更新信息,可以帮助我们从日志中查看到变量是在何时何地更新的. 2. 使用ForceExecutionRes ...
- linux 读写锁应用实例
转自:http://blog.csdn.net/dsg333/article/details/22113489 /*使用读写锁实现四个线程读写一段程序的实例,共创建了四个新的线程,其中两个线程用来读取 ...
- 第二十五篇:在SOUI中做事件分发处理
不同的SOUI控件可以产生不同的事件.SOUI系统中提供了两种事件处理方式:事件订阅 + 事件处理映射表(参见第八篇:SOUI中控件事件的响应) 事件订阅由于直接将事件及事件处理函数连接,不存在事件分 ...
- ubuntu初始化root帐号密码
Ubuntu Kylin 14.04的安装过程中并没有提供设置root密码的过程,取而代之的是自定义的帐号. 如果我们需要使用到root帐号或者root权限,则需要重新设置root帐号的密码. 设置方 ...
- hdu 4770(枚举 + dfs爆搜)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4770 思路:由于最多只有15个".",可以直接枚举放置的位置,然后判断是否能够全部 ...
- SQLServer 维护脚本分享(07)IO
sp_helpfile --当前数据库文件分配情况 sp_spaceused --当前db空间大小(有时不准) sp_spaceused 'dbo.user' --指定表的空间大小(有时不准) sp_ ...