⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pc5_12.cpp

📁 《c++大学教程实验指导书》源码
💻 CPP
字号:
// Chapter 5 of C++ How to Program
// Programming Challenge 12 (Solution)
// This solution assumes that there is only one
// entrance and one exit for a given maze, and
// these are the only two periods on the borders.
#include <iostream> 

using std::cout;  
using std::cin; 

#include <cstdlib>

enum Direction { DOWN, RIGHT, UP, LEFT };
const int X_START = 2;
const int Y_START = 0; // starting maze coordinates
static bool flag = false;

void mazeTraversal( char [][ 12 ], int, int, int, bool );
void printMaze( const char[][ 12 ] );
bool validMove( const char [][ 12 ], int, int );
bool coordsAreEdge( int, int );

int main()
{
   char maze[ 12 ][ 12 ] =
  { {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
    {'#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#'},
    {'.', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#'},
    {'#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#'},
    {'#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', '.'},
    {'#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#'},
    {'#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#'},
    {'#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#'},
    {'#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#'},
    {'#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#'},
    {'#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#'},
    {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'} };

   mazeTraversal( maze, X_START, Y_START, RIGHT, true );

   return 0;

} // end main

// assume that there is exactly 1 entrance  1 exit to the maze.
void mazeTraversal( char maze[][ 12 ], int xCoord,
                    int yCoord, int direction, bool firstMove )
{
   maze[ xCoord ][ yCoord ] = 'x';
   printMaze( maze );

   if ( !firstMove )
    
      if ( coordsAreEdge( xCoord, yCoord ) )
      
         if ( xCoord != X_START && yCoord != Y_START ) {
            cout << "\nMaze successfully exited!\n\n";
            return;   // maze is complete
         
         } // end if

         else {
            cout << "\nArrived back at the starting location,"
                 << " maze has no exit.";
            return;
         
         } // end else

   int move = direction;
   
   for ( int count = 0; count < 4; ++count ) {

      switch( move ) {
 
         case DOWN:
         
            if ( validMove( maze, xCoord + 1, yCoord ) ) { 
               mazeTraversal( maze, xCoord + 1, yCoord, LEFT, false );
               return;     
            }
            
            break;
          
         case RIGHT:
         
            if ( validMove( maze, xCoord, yCoord + 1 ) ) { 
               mazeTraversal( maze, xCoord, yCoord + 1, DOWN, false );
               return;
            }
            
            break;
         
         case UP:
         
            if ( validMove( maze, xCoord - 1, yCoord ) ) { 
               mazeTraversal( maze, xCoord - 1, yCoord, RIGHT, false );
               return;
            }
            
            break;
         
         case LEFT:
            if ( validMove( maze, xCoord, yCoord - 1 ) ) { 
               mazeTraversal( maze, xCoord, yCoord - 1, UP, false );

               return;
            }
         
            break;
      
      } // end switch

      move++;
      move %= 4;
   
   } // end for

} // end mazeTraverse

// function validMove definition
bool validMove( const char maze[][ 12 ], int r, int c )
{
   return ( r >= 0 && r <= 11 && c >= 0 &&
            c <= 11 && maze[ r ][ c ] != '#' );

} // end function validMove

// function coordsAreEdge definition
bool coordsAreEdge( int x, int y )
{
   if ( x == 0 || x == 11 || y == 0 || y == 11 )
      return true;
   else
      return false;

} // end function coordsAreEdge

// output maze
void printMaze( const char maze[][ 12 ] )
{
   for ( int x = 0; x < 12; ++x ) {

      for ( int y = 0; y < 12; ++y )
         cout << maze[ x ][ y ] << ' ';

      cout << '\n';
   
   } // end for

   cout << "\nHit return to see next move\n";
   cin.get();

} // end function printMaze


 /**************************************************************************
 * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice      *
 * Hall. All Rights Reserved.                                             *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 *************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -