C ++ Purpose "Labyrinth"

It is assumed that the mini-program will print all the possible routes through the maze, where the entry / start point is always one to the left of the upper left corner and all possible exits that are always on the right wall. It extracts the maze from a text file.

A maze is just a bunch of text. The labyrinth consists of an nxn grid consisting of “#” characters, which are walls, and various letters [a ... z] representing the area / paths for walking. Letters can be repeated, but can never be near.

Labyrinth 15x15.

The upper case S always marks the entrance and is located on the left wall in second place. A possible path is only through letters - you cannot walk on # characters. Any letter on the right wall represents the exit (s).

For instance,

######
Sa#hln
#bdp##
##e#ko
#gfij#
######

- . , .

:

Path 1: S,a,b,d,e,f,i,j,k,o
Path 2: S,a,b,d,p,h,l,n
2 total paths

? , , , .

, , adajcent, , , , .

, ( , , , ):

    #include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <cstdio>
using namespace std;

ifstream file("maze.txt");
vector<char> vec(istreambuf_iterator<char>(file), (istreambuf_iterator<char>())); // Imports characters from file
vector<char> path;                      // Declares path as the vector storing the characters from the file
int x = 18;                             // Declaring x as 18 so I can use it with recursion below
char entrance = vec.at(16);             // 'S', the entrance to the maze
char firstsquare = vec.at(17);          // For the first walkable square next to the entrance
vector<char> visited;                   // Squares that we've walked over already

int main()
{
    if (file) {
        path.push_back(entrance);               // Store 'S', the entrance character, into vector 'path'
        path.push_back(firstsquare);            // Store the character of the square to the right of the entrance
                                                // into vector 'path'.

        while (isalpha(vec.at(x)))
        {
            path.push_back(vec.at(x));
            x++;
        }

        cout << "Path is: ";                    // Printing to screen the first part of our statement

        // This loop to print to the screen all the contents of the vector 'path'.
        for(vector<char>::const_iterator i = path.begin(); i != path.end(); ++i)  // 
        {
        std::cout << *i << ' ';
        }

        cout << endl;
        system ("pause");                       // Keeps the black box that pops up, open, so we can see results.
        return 0;
        }
}

!

+3
5

:

  • - " ", , .
  • - ,
  • .

, - , 3x3 - , . . . . . - " ".

, , . ( , , , .)

!

+2

( ) - . . (, , ).

"" - , . , , , , , (, "." ).

+2

, , , , . , , , , , .

+1

:

, , . , .

, ? ( ?)

+1

. : , , , ? 0 1 ... , , .

, ... ...

, , , ...

-P

+1
source

All Articles