zoukankan      html  css  js  c++  java
  • 1204. Maze Traversal

    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 ",robot.row+1,robot.col+1,robot.ori);



      1 #include <stdio.h>
      2 #include <string.h> 
      3 
      4 int handle(char *ptr);
      5 void move(char str);
      6 
      7 
      8 struct R{
      9     int row;//行号 
     10     int col;//列号 
     11     char ori;//朝向 
     12 }robot;
     13 char maze[60][60];
     14 
     15 int main(void)
     16 {
     17     int row,col,i,j; 
     18     char run[100];
     19 
     20     scanf("%d %d",&row,&col); 
     21     getchar();
     22     
     23     for(i=0;i<row;i++){
     24         for(j=0;j<col;j++)
     25             maze[i][j]=getchar();
     26         getchar();
     27     }
     28     
     29     scanf("%d %d",&robot.row,&robot.col); 
     30     getchar();//吸收回车 
     31     robot.ori = 'N';
     32     robot.row--;
     33     robot.col--;//跟数组匹配。 
     34     
     35     while(1){
     36         gets(run);
     37         if(handle(run) == 0)
     38             break;
     39         memset(run,0,sizeof(run));
     40     }
     41     
     42     printf("%d %d %c
    ",robot.row+1,robot.col+1,robot.ori);
     43     
     44     //system("PAUSE");
     45     return 0;
     46 }
     47 
     48 int  handle(char *ptr)
     49 {
     50      char *next = ptr;
     51      char c= *next;
     52      while((c != '')&&(c != 'Q')){
     53          move(c);
     54          next++;
     55          c= *next;
     56      }
     57      if(*next == 'Q')
     58          return 0;
     59      return 1;
     60 }
     61 
     62 void move(char str)
     63 {
     64      char s=str;
     65      if((s == 'R')||(s == 'L')){
     66          switch(robot.ori){
     67              case 'N':
     68                   if(s == 'R')
     69                       robot.ori = 'E';
     70                   else
     71                       robot.ori = 'W';
     72                   break;
     73              case 'E':
     74                   if(s == 'R')
     75                       robot.ori = 'S';
     76                   else
     77                       robot.ori = 'N';
     78                   break;
     79              case 'S':
     80                   if(s == 'R')
     81                       robot.ori = 'W';
     82                   else
     83                       robot.ori = 'E';
     84                   break;
     85              case 'W':
     86                   if(s == 'R')
     87                       robot.ori = 'N';
     88                   else
     89                       robot.ori = 'S';
     90                   break;
     91          }
     92      }
     93      if(s == 'F'){
     94          switch(robot.ori){
     95              case 'N':
     96                   if(maze[robot.row-1][robot.col] == ' ')
     97                       robot.row--;
     98                   break;
     99              case 'E':
    100                   if(maze[robot.row][robot.col+1] == ' ')
    101                       robot.col++;
    102                   break;
    103              case 'S':
    104                   if(maze[robot.row+1][robot.col] == ' ')
    105                       robot.row++;
    106                   break;
    107              case 'W':
    108                   if(maze[robot.row][robot.col-1] == ' ')
    109                       robot.col--;
    110                   break;
    111          }
    112      }
    113 }
  • 相关阅读:
    使用nltk库查找英文同义词和反义词
    argostranslate 翻译的使用
    python从git上安装相应的依赖库
    json.dumps()函数解析
    python将xml文件转为json
    python匹配字符串中,某个词的位置
    TypeError: Cannot read property 'version' of undefined
    js常用工程类函数 | 下载链接 | 自定义下载内容
    C# Visual Studio 2019 代码自动补全 TAB+TAB
    国内开源镜像站点汇总
  • 原文地址:https://www.cnblogs.com/yushuo1990/p/4312329.html
Copyright © 2011-2022 走看看