1204.   Maze Traversal

A common problem in artificial intelligence is negotiation of a maze. A maze has corridors and walls. The robot can proceed along corridors, but cannot go through walls.

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的更多相关文章

  1. UVa 10377 - Maze Traversal

    題目:一個機器人在迷宮中行走,它的指令是方向控制(前進.左轉.右轉).給你初始位置和一些指令: 問最後停在那個位置. 分析:模擬.直接模擬就可以,注意一下細節. 假设,不能行走(邊界或者是墻壁)則停在 ...

  2. HOJ题目分类

    各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...

  3. Morris post order traversal algorithm

    Sept. 5, 2015 花时间把代码读明白, 比光看书强. 动手写代码, 改代码,  兴趣是最好的老师. 多记几个例子, 增加情趣. 举个例子关于中序遍历,            4       ...

  4. Backtracking algorithm: rat in maze

    Sept. 10, 2015 Study again the back tracking algorithm using recursive solution, rat in maze, a clas ...

  5. 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 ...

  6. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  7. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  8. [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  9. [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 ...

随机推荐

  1. Printf()输出格式控制(转)

    int printf(const char *format,[argument]); format 参数输出的格式,定义格式为: %[flags][width][.perc] [F|N|h|l]typ ...

  2. struct 类型重定义

    类型定义的那个头文件只需要在功能源文件里#include 开始在主函数源文件里也#include,所以出现了重定义

  3. PE文件的执行顺序

    当一个PE文件被执行时,PE装载器首先检查DOS MZ header里的PE header的偏移量.如果找到,则直接跳转到PE header的位置. 当PE装载器跳转到PE header后,第二步要做 ...

  4. 【项目经验】——JSON.parse() && JSON.stringify()

    我们在做项目的时候,都知道序列化和反序列化,师哥说:"有正就有反,有来就有回!"的确,就是这样.然后我们在这里分享一下JSON.stringify()  和JSON.parse() ...

  5. 获取APK签名

    获取apk签名工具类 import android.content.Context; import android.content.pm.PackageInfo; import android.con ...

  6. 【转】Struts2国际化

    原文章:http://www.cnblogs.com/hellokitty1/p/5083663.html 简单理解     国际化简称i18n,其来源是英文单词 internationalizati ...

  7. Android Studio使用第三方类库

    导入*.jar包 新建好了Android项目,添加一个第三方已经打包好的jar文件进你项目,下面就已添加一个odata4j的一个包 在项目中添加一个libs文件 直接通过COPY/PAST 把你下载的 ...

  8. 那些Android中的性能优化

    性能优化是一个大的范畴,如果有人问你在Android中如何做性能优化的,也许都不知道从哪开始说起. 首先要明白的是,为什么我们的App需要优化,最显而易见的时刻:用户say,什么狗屎,刷这么久都没反应 ...

  9. super一些要点

    package o6; class Grandparent { public Grandparent() { System.out.println("GrandParent Created. ...

  10. express-9 Handlebars模板引擎(2)

    视图和布局 视图通常表现为网站上的各个页面(它也可以表现为页面中AJAX局部加载的内容,或一封电子邮件,或页面上的任何东西).默认情况下,Express会在views子目录中查找视图.布局是一种特殊的 ...