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. SQL跨项目查询语法

    EXEC sp_addlinkedserver 'ITSV', '', 'SQLOLEDB', '192.168.1.248' EXEC sp_addlinkedsrvlogin 'ITSV', 'f ...

  2. Localstorage本地存储兼容函数

    前言HTML5提供了本地存储的API:localstorage对象和sessionStorage对象,实现将数据存储到用户的电脑上.Web存储易于使用.支持大容量(但非无限量)数据同时存储,同时兼容当 ...

  3. Unreal Engine4 学习笔记1 状态机 动画蓝图

    1.动画蓝图 包含 状态机 包含 混合空间BlendSpace,即状态机包含在动画蓝图的"动画图表中",而混合空间可用于在状态机中向某(没)一个状态输出最终POSE:    动画蓝 ...

  4. elasticsearch入门

    到 https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.6.0.zip 下载最新包: 启动: ./elast ...

  5. 继续Get News List

    拿到news list 所需要的技能 json数组反序列化 iOS中有哪些集合对象 数组的遍历 Debugging with GDB json数组反序列化 id jsonObject = [NSJSO ...

  6. bootstrap 入门

    bootstrap 入门 <!DOCTYPE html> <html> <head lang="en"> <meta charset=&q ...

  7. Java中synchronized详解

    synchronized 原则: 尽量避免无谓的同步控制,同步需要系统开销,可能造成死锁 尽量减少锁的粒度 同步方法 public synchronized void printVal(int v) ...

  8. 【MongoDB】1.安装--以及简单使用

    第一次接触MongoDB    参考&粘贴:http://jingyan.baidu.com/article/ed15cb1b52b8661be2698162.html 一.安装 1.首先去官 ...

  9. 纯window下VMware 安装 OS X El Capitan 原版映像【未完待续】

    一.所需软件1.下载OS X El Capitan 10.11.2 15C50链接:http://pan.baidu.com/s/1skuLgAx 密码:u2jf 2.下载VMware Worksta ...

  10. 在CSDN中添加友情连接

    <a bref='http://www......'>友情连接</a><br/> <a bref='http://www......'>友情连接2< ...