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. 重温WCF之WCF传输安全(十三)(2)基于SSL的WCF匿名客户端(转)

    转载地址:http://www.cnblogs.com/lxblog/archive/2012/09/13/2683514.html 这一篇我们利用上一篇制作的证书,来演示一个基于SSL的WCF服务, ...

  2. CXF学习 (1)

    Axis(Apache) -> Axis2(Apache) XFire - > CXF (XFire+Celtrix) (Apache) CXF并不仅仅是Webservice框架,更号称是 ...

  3. 解决postgresql -- ERROR: 42601: query has no destination for result data

    I am learning Npgsql and PostgreSQL. I am unable to define the output parameter correctly. What am I ...

  4. 攻城狮在路上(叁)Linux(二十九)--- 完整备份工具:dump以及restore

    一.dump命令: 该命令既可以针对整个文件系统进行备份,也可以仅针对目录来备份.还可以指定不同的备份等级(-0~-9共10个等级). dump -W:列出在/etc/fstab中具有dump设置的分 ...

  5. 攻城狮在路上(壹) Hibernate(十五)--- Hibernate的高级配置

    一.配置数据库连接池: 1.使用默认的数据库连接池: Hibernate提供了默认了数据库连接池,它的实现类为DriverManegerConnectionProvider,如果在Hibernate的 ...

  6. 一种快速刷新richedit中内嵌动画的方法的实现

    在IM中使用动画表情是一种非常有趣的方式,然而选择一种合适的方式来实现却并不容易. 一般来说,除了自己去实现一个富文本控件,目前主要的解决方案有3种: 1.使用浏览器做容器. 2.使用QT提供的Ric ...

  7. [Unity3D]上海某大型游戏公司的基础面试题

    一个小老乡跟我聊到去上海某大公司的基础面试题,面试结果不尽如人意,但还是分享了下面试的试题,刚刚第一次录制视频,给某人讲课,我感觉讲的还算比较耐心,但发现一些新手入门学习的弊端,可能是很普遍的现象,这 ...

  8. easyui tree折叠

    标签 <div id="buildDiv" data-options="region:'center'" title="楼栋权限" s ...

  9. Android UI学习 - Tab的学习和使用(转)

      本文是参考Android官方提供的sample里面的ApiDemos的学习总结.   TabActivity   首先Android里面有个名为TabActivity来给我们方便使用.其中有以下可 ...

  10. POJ 2114 Boatherds 树分治

    Boatherds     Description Boatherds Inc. is a sailing company operating in the country of Trabantust ...