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 ...
随机推荐
- AIX性能监控
http://www.ibm.com/developerworks/cn/aix/library/au-aix7memoryoptimize2/ http://www.aixchina.net/Art ...
- 无废话Android之activity的生命周期、activity的启动模式、activity横竖屏切换的生命周期、开启新的activity获取他的返回值、利用广播实现ip拨号、短信接收广播、短信监听器(6)
1.activity的生命周期 这七个方法定义了Activity的完整生命周期.实现这些方法可以帮助我们监视其中的三个嵌套生命周期循环: (1)Activity的完整生命周期 自第一次调用onCrea ...
- Java简明教程
Java与C++比较概况 C++ Java class Foo { // 声明 Foo 类 public: int x; // 成员变量 Foo(): x() { // Foo 的构造函数Constr ...
- HDU3359 Kind of a Blur(高斯消元)
建立方程后消元 #include<cstdio> #include<iostream> #include<cstdlib> #include<cstring& ...
- Codeforces Round #364 As Fast As Possible
二分思想,对所要花费的时间进行二分,再以模拟的形式进行验证是否可行. 使用二分法,可以将一个求最优解的问题转化为一个判定问题,优雅的暴力. #include<cstdio> #includ ...
- .NET NLog 详解(五) - Condition Expression
Sample <!-- during normal execution only log Info messages --> <defaultFilter>level > ...
- 实时视频应用之QoS关键技术分析
转自:http://www.aiweibang.com/m/detail/104476372.html?from=p 随着WebRTC标准的逐步推广,实时音视频通讯技术受到越来越多公司和技术人员的关注 ...
- RTCP资料详解
转自:http://www.360doc.com/content/13/0606/10/1317564_290865866.shtml RTCP RTCP协议将控制包周期发送给所有连接者,应用与数据包 ...
- 监听报错 TNS-00525: Insufficient privilege for operation 11gR2 + 连接报错ORA-12537: TNS:connection closed
1.TNS-00525: Insufficient privilege for operation Started with pid= Listening on: (DESCRIPTION=(ADDR ...
- How to use Ajax on Visualforce page on Salesforce platform
Just use Ajax pattern to call object data from server on visualforce page. Following is the Asynchro ...