Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <stdio.h>
- #include <iostream>
- const int MazeHeight = 9;
- const int MazeWidth = 9;
- char Maze[MazeHeight][MazeWidth + 1] =
- {
- "# #######",
- "# # #",
- "# ### # #",
- "# # # #",
- "# # # ###",
- "# # # #",
- "# ### # #",
- "# # #",
- "####### #",
- };
- const char Wall = '#';
- const char Free = ' ';
- const char Filler = '*';
- class COORD
- {
- public:
- int X;
- int Y;
- COORD(int x = 0, int y = 0) { X = x, Y = y; }
- COORD(const COORD &coord) { X = coord.X; Y = coord.Y; }
- };
- COORD StartingPoint(1, 0);
- COORD EndingPoint(7, 8);
- void PrintMaze()
- {
- for (int Y = 0; Y < MazeHeight; Y++)
- {
- printf("%s\n", Maze[Y]);
- }
- printf("\n");
- }
- bool Solve(int X, int Y)
- {
- Maze[Y][X] = Filler;
- if (X == EndingPoint.X && Y == EndingPoint.Y)
- {
- return true;
- }
- if (X > 0 && Maze[Y][X - 1] == Free && Solve(X - 1, Y))
- {
- return true;
- }
- if (X < MazeWidth && Maze[Y][X + 1] == Free && Solve(X + 1, Y))
- {
- return true;
- }
- if (Y > 0 && Maze[Y - 1][X] == Free && Solve(X, Y - 1))
- {
- return true;
- }
- if (Y < MazeHeight && Maze[Y + 1][X] == Free && Solve(X, Y + 1))
- {
- return true;
- }
- // Otherwise we need to backtrack and find another solution.
- Maze[Y][X] = Free;
- // If you want progressive update, uncomment these lines...
- //PrintDaMaze();
- //Sleep(50);
- return false;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- if (Solve(StartingPoint.X, StartingPoint.Y))
- {
- PrintMaze();
- }
- else
- {
- printf("Damn\n");
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement