I've been working on this program for school and I just about got it before
I had to turn it in.
Here's my final code on it. I didn't finish
it, but I think I'm pretty close. M is the starting point in the maze and W
the finish. Using recursion the program is to leave a trail of asterisks
(*) showing the solution.
Well, I got stumped on this one. I found something similar to it online,
but not quite what I was looking for.
Here's the code. Maybe someone out there can improve on this.
Roger
P.S. This compiles okay, it just doesn't do what it's supposed to do.
#include <iostream>
using namespace std;
int col = 1;
int row = 2;
const ROWMAX = 11;
const COLMAX = 16;
char maze[ROWMAX][COLMAX] =
{
{'B','B','B','B ','B','B','B',' B','B','B','B', 'B','B','B','B' ,'B'},
{'B','M',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','B'},
{'B','B','B','B ','B','B','B',' B','B','B',' ','B','B','B',' ','B'},
{'B','W',' ',' ',' ',' ',' ',' ',' ','B',' ','B',' ','B',' ','B'},
{'B','B','B','B ','B','B','B',' B',' ','B','B','B',' ','B',' ','B'},
{'B',' ',' ',' ',' ',' ',' ','B',' ',' ',' ',' ',' ','B',' ','B'},
{'B','B','B','B ','B','B','B',' B',' ','B','B','B',' B','B',' ','B'},
{'B',' ',' ',' ','B',' ',' ',' ',' ','B',' ',' ',' ',' ',' ','B'},
{'B',' ',' ',' ',' ',' ',' ',' ','B','B','B',' B',' ','B','B','B'},
{'B',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','B'},
{'B','B','B','B ','B','B','B',' B','B','B','B', 'B','B','B','B' ,'B'}
};
void printMaze(char[]);
void runMaze(char *, int, int);
int main()
{
char * pt = new char;
pt = &maze[ROWMAX + 1][COLMAX + 1];
cout << "Maze before solution:\n";
printMaze(pt);
cout << "Maze after solution:\n";
runMaze(pt, 1, 2);
delete pt;
return 0;
}
void printMaze(char[])
{
for(int row = 1; row <= ROWMAX; row++)
{
for(int col=1; col <= COLMAX; col++)
cout << maze[row][col];
cout << "\n";
}
}
void runMaze(char * ptmaze, int row, int col)
{
if(1 <= row && row <= ROWMAX && 1 <= col && col <= COLMAX)
{
if(' ' == maze[row][col])
{
maze[row][col] = '*';
if(row == 1 || row == ROWMAX || col == 1 || col == COLMAX)
printMaze(ptmaz e);
else
{
runMaze(ptmaze, row - 1, col);
runMaze(ptmaze, row, col + 1);
runMaze(ptmaze, row + 1, col);
runMaze(ptmaze, row, col - 1);
}
}
}
} 2 21412
In comp.lang.c++
"Roger Douglass" <ro****@comcast .net> wrote:
There are some maze algorithms in Snippets. I've been working on this program for school and I just about got it before I had to turn it in. Here's my final code on it. I didn't finish it, but I think I'm pretty close. M is the starting point in the maze and W the finish. Using recursion the program is to leave a trail of asterisks (*) showing the solution.
Well, I got stumped on this one. I found something similar to it online, but not quite what I was looking for.
Here's the code. Maybe someone out there can improve on this.
Roger
P.S. This compiles okay, it just doesn't do what it's supposed to do.
#include <iostream> using namespace std;
int col = 1; int row = 2; const ROWMAX = 11; const COLMAX = 16;
char maze[ROWMAX][COLMAX] = { {'B','B','B','B ','B','B','B',' B','B','B','B', 'B','B','B','B' ,'B'}, {'B','M',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','B'}, {'B','B','B','B ','B','B','B',' B','B','B',' ','B','B','B',' ','B'}, {'B','W',' ',' ',' ',' ',' ',' ',' ','B',' ','B',' ','B',' ','B'}, {'B','B','B','B ','B','B','B',' B',' ','B','B','B',' ','B',' ','B'}, {'B',' ',' ',' ',' ',' ',' ','B',' ',' ',' ',' ',' ','B',' ','B'}, {'B','B','B','B ','B','B','B',' B',' ','B','B','B',' B','B',' ','B'}, {'B',' ',' ',' ','B',' ',' ',' ',' ','B',' ',' ',' ',' ',' ','B'}, {'B',' ',' ',' ',' ',' ',' ',' ','B','B','B',' B',' ','B','B','B'}, {'B',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','B'}, {'B','B','B','B ','B','B','B',' B','B','B','B', 'B','B','B','B' ,'B'} }; void printMaze(char[]); void runMaze(char *, int, int);
int main() { char * pt = new char; pt = &maze[ROWMAX + 1][COLMAX + 1];
cout << "Maze before solution:\n"; printMaze(pt); cout << "Maze after solution:\n"; runMaze(pt, 1, 2); delete pt; return 0; } void printMaze(char[]) { for(int row = 1; row <= ROWMAX; row++) { for(int col=1; col <= COLMAX; col++) cout << maze[row][col]; cout << "\n"; } } void runMaze(char * ptmaze, int row, int col) { if(1 <= row && row <= ROWMAX && 1 <= col && col <= COLMAX) { if(' ' == maze[row][col]) { maze[row][col] = '*'; if(row == 1 || row == ROWMAX || col == 1 || col == COLMAX) printMaze(ptmaz e);
else { runMaze(ptmaze, row - 1, col); runMaze(ptmaze, row, col + 1); runMaze(ptmaze, row + 1, col); runMaze(ptmaze, row, col - 1); } } } }
Try this!
#include <iostream>
using namespace std;
int col = 1;
int row = 2;
const int ROWMAX = 11;
const int COLMAX = 16;
char maze[ROWMAX][COLMAX] =
{
{'B','B','B','B ','B','B','B',' B','B','B','B', 'B','B','B','B' ,'B'},
{'B','M',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','B'},
{'B','B','B','B ','B','B','B',' B','B','B',' ','B','B','B',' ','B'},
{'B','W',' ',' ',' ',' ',' ',' ',' ','B',' ','B',' ','B',' ','B'},
{'B','B','B','B ','B','B','B',' B',' ','B','B','B',' ','B',' ','B'},
{'B',' ',' ',' ',' ',' ',' ','B',' ',' ',' ',' ',' ','B',' ','B'},
{'B','B','B','B ','B','B','B',' B',' ','B','B','B',' B','B',' ','B'},
{'B',' ',' ',' ','B',' ',' ',' ',' ','B',' ',' ',' ',' ',' ','B'},
{'B',' ',' ',' ',' ',' ',' ',' ','B','B','B',' B',' ','B','B','B'},
{'B',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','B'},
{'B','B','B','B ','B','B','B',' B','B','B','B', 'B','B','B','B' ,'B'}
};
void printMaze();
void runMaze(int, int);
void printMaze()
{
for(int row = 0; row < ROWMAX; row++)
{
for(int col=0; col < COLMAX; col++)
cout << maze[row][col];
cout << "\n";
}
}
void runMaze(int row, int col)
{
if( (row>0 && row<ROWMAX) && (col>0 && col<COLMAX)) {
if( maze[row][col] == 'W' ) return;
if( maze[row][col] == ' ') {
maze[row][col]='*';
runMaze(row, col+1);
runMaze(row, col-1);
runMaze(row-1, col);
runMaze(row+1, col);
}
}
}
int main()
{
cout << "Maze before solution:\n";
printMaze();
cout << "Maze after solution:\n";
runMaze(1, 2);
printMaze();
return 0;
}
Marko Djogatovic This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Bernard Fields |
last post by:
Greets, all.
As the title suggests, I'm trying to make a maze. Specifically, it's a
top-down, 2-d maze, preferably randomly generated, though I'm willing to
forego that particular aspect as this point.
I've done many, many web-searches, but nothing that I've found so far has
provided any clues....
|
by: Letterfly |
last post by:
Greetings,
I'm experimenting with some pages that will create 100% random mazes.
You can see my demo page here:
http://www.mazes.com/asp-maze/test-maz.asp
Each time you refresh the page or put in a new number and click "Make A
Maze" it creates a brand new maze.
|
by: JoeC |
last post by:
I am trying to create a simple maze program on a window. I am trying
to create a global var to handle the commands.
I declare:
#include "space.h"
#include "player.h"
#include "Command.h"
static char gCmd = '$'; <--- My global command.
|
by: shaveta |
last post by:
The problem is like this:-
A maze is a rectangular area, with m rows and n columns, with an entrance and an exit. The interior of the maze contains obstacles. The entrance is at the upper-left corner, and exit is at the lower-right corner. A rat in a maze problem is to find a path from the entrance to exit of a maze. A path is basically a...
|
by: daneshjo |
last post by:
Hi
im an IT student.I have registered as a member of this site recently.I have a question about the solution of maze program. I want to write maze program in c++ .I want to solve the maze class includes two methods: 1)makeMaze & 2)runMaze.
I write the method of makeMaze in a way that fulls a n*n matrix.
one of my problem is about the writing...
| |
by: Brosert |
last post by:
I am writing (or trying to) a small program to draw a maze, that can then be traversed by a user. I have set up a grid of squares that can be either present (blocking the path) or not (allowing the user to navigate into that square).
My first problem, is that all algorithms I can find on the net (Prim, Kruskal etc) deal with a cell having walls...
|
by: ichar |
last post by:
hi guys, am currently coding a multi threaded program to solve a maze. But firstly im having trouble saving the maze into 2d array. Ay help is much appreciated
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <fstream>
using namespace std;
void getMaze()
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
| |
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |