|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
Question about C++ recusive pathfinding
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||||
|
*cough* Stock *cough*
|
Mods delete if you see this
__________________
Behold : the lousiest processor in this forum! P4 2.4GHz
Last edited by icyblizard : 12-10-07 at 06:19 AM. |
|||||||||||||
|
|
|
|
|
#2 (permalink) | ||||||||||||||
|
C0019 13210
|
Quote:
That will show your teacher!
__________________
Case Air-cooling: [ Vantec Nexus 205-B | 3 x AeroCool XtremeTurbine-Black | 1 x AeroCool SilverLightning ] Links: [ G15 LCD Program Archive | Project Ablative Armor ] [ Info: Overclocking Effects on Benchmark Scores | Info: Explanation of LCD Terminology ]
|
||||||||||||||
|
|
|
|
#3 (permalink) | |||||||||||||
|
*cough* Stock *cough*
|
sorry, actual maze looks like this, just replace the . with blank spaces. The brower sees many spacings as just 1 spacing
**************** *C .........................* ************......* * ..*.......................* *M *********......* *............................* **************** Actual maze is 16*8, just too troublesome to draw it here. And of course, i believe that his 10 lines are indented properly ![]()
__________________
Behold : the lousiest processor in this forum! P4 2.4GHz
Last edited by icyblizard : 12-06-07 at 08:36 AM. |
|||||||||||||
|
|
|
|
|
#4 (permalink) | |||||||||||||
|
Programmer
|
Oh man, I wrote this exact program a couple of years ago. There's no way in hell you could get a shortest path algorithm (thats what the name of this problem is) in 10 lines, especially if you have to load the maze from file!
__________________
Whats this folding I've been hearing about? Crucial Ballistix Club ![]() Member of the OCN Diablo III Club ~M Hail to the Victors M~
|
|||||||||||||
|
|
|
|
#5 (permalink) | |||||||||||||
|
*cough* Stock *cough*
|
Not the whole code, just the pathfinding part
__________________
Behold : the lousiest processor in this forum! P4 2.4GHz
|
|||||||||||||
|
|
|
|
|
#6 (permalink) | ||||||||||||
|
Intel Overclocker
|
Well, does it have to be the shortest path, or just any path? And how do you display the path that you traveled?
__________________
|
||||||||||||
|
|
|
|
#7 (permalink) | |||||||||||||
|
*cough* Stock *cough*
|
It does not have to be the shortest path. When the function is returned, the current coordinates of the maze[x][y]='+' to mark the path which u travelled.
__________________
Behold : the lousiest processor in this forum! P4 2.4GHz
|
|||||||||||||
|
|
|
|
|
#8 (permalink) | ||||||||||||
|
Intel Overclocker
|
I'll see what I can do.
__________________EDIT: Can you move diagonally? EDIT: This works fine (I think). It returns a path, not necessarily the shortest. I tested it with: "maze.txt" Code:
**************** *m * ************** * * * ***** ********** * * * * * * * c* **************** Code:
#include <iostream>
#include <fstream>
using namespace std;
const int ROWS = 8, COLS = 16;
void findStart(char maze[ROWS][COLS], char start[]);
bool path(char maze[ROWS][COLS], int x, int y);
void loadFile(string target, char maze[ROWS][COLS]);
void print(char maze[ROWS][COLS]);
int main()
{
char maze[ROWS][COLS], start[2];
loadFile("maze.txt", maze);
print(maze);
findStart(maze, start);
path(maze, start[0], start[1]);
cout << "Path: " << endl;
print(maze);
system("pause");
return 0;
}
void print(char maze[ROWS][COLS])
{
for(int row=0; row<ROWS; row++)
{
for(int col=0; col<COLS; col++)
cout << maze[row][col] << " ";
cout << endl;
}
}
void loadFile(string target, char maze[ROWS][COLS])
{
ifstream file;
file.open(target.c_str());
for(int row=0; row<ROWS; row++)
{
for(int col=0; col<COLS; col++)
maze[row][col] = file.get();
file.get();
}
file.close();
}
void findStart(char maze[ROWS][COLS], char start[])
{
for(int row=0; row<ROWS; row++)
for(int col=0; col<COLS; col++)
if(maze[row][col] == 'm')
{
start[0] = row;
start[1] = col;
}
}
bool path(char maze[ROWS][COLS], int x, int y)
{
if(maze[x][y] == ' ') maze[x][y] = '+';
for(int val=-2; val<=2; val++)
{
if(maze[x+val/2][y+val%2] == 'c') // found cheese
{
return true;
system("pause");
}
if(maze[x+val/2][y+val%2] == ' ')
{
if(path(maze, x+val/2, y+val%2))
return true;
}
}
if(maze[x][y] == '+') maze[x][y] = ' ';
return false;
}
Last edited by dante020 : 12-07-07 at 10:37 AM. |
||||||||||||
|
|
|
|
#9 (permalink) | |||||||||||||
|
Programmer
|
ahh, well making it not the shortest path might be quite a bit easier. If I had some time I'd attempt it, though it looks like dante has a good solution
__________________
Whats this folding I've been hearing about? Crucial Ballistix Club ![]() Member of the OCN Diablo III Club ~M Hail to the Victors M~
|
|||||||||||||
|
|
|
|
#10 (permalink) | ||||||||||||
|
Intel Overclocker
|
Well I could make it the shortest path without too much effort but if its not required...
__________________EDIT: This one should find the shortest path Code:
#include <iostream>
#include <fstream>
using namespace std;
const int ROWS = 8, COLS = 16;
void findStart(char maze[ROWS][COLS], char start[]);
void path(char maze[ROWS][COLS], int x, int y, char shortest[ROWS][COLS], int & length, int & shortPath);
void loadFile(string target, char maze[ROWS][COLS]);
void print(char maze[ROWS][COLS]);
int main()
{
char maze[ROWS][COLS], shortest[ROWS][COLS], start[2];
int length = 0, shortPath = 0;
loadFile("maze.txt", maze);
print(maze);
findStart(maze, start);
path(maze, start[0], start[1], shortest, length, shortPath);
cout << "Path: " << endl;
print(shortest);
cout << "Length: " << shortPath << endl;
system("pause");
return 0;
}
void print(char maze[ROWS][COLS])
{
for(int row=0; row<ROWS; row++)
{
for(int col=0; col<COLS; col++)
cout << maze[row][col] << " ";
cout << endl;
}
}
void loadFile(string target, char maze[ROWS][COLS])
{
ifstream file;
file.open(target.c_str());
for(int row=0; row<ROWS; row++)
{
for(int col=0; col<COLS; col++)
maze[row][col] = file.get();
file.get();
}
file.close();
}
void findStart(char maze[ROWS][COLS], char start[])
{
for(int row=0; row<ROWS; row++)
for(int col=0; col<COLS; col++)
if(maze[row][col] == 'm')
{
start[0] = row;
start[1] = col;
}
}
void path(char maze[ROWS][COLS], int x, int y, char shortest[ROWS][COLS], int & length, int & shortPath)
{
if(maze[x][y] == ' ')
{
maze[x][y] = '+';
length++;
}
for(int val=-2; val<=2; val++)
{
if(maze[x+val/2][y+val%2] == 'c') // found cheese
{
if(length < shortPath || shortPath == 0)
{
for(int row=0; row<ROWS; row++)
for(int col=0; col<COLS; col++)
shortest[row][col] = maze[row][col];
shortPath = length;
}
}
if(maze[x+val/2][y+val%2] == ' ')
{
path(maze, x+val/2, y+val%2, shortest, length, shortPath);
}
}
if(maze[x][y] == '+')
{
maze[x][y] = ' ';
length--;
}
}
Last edited by dante020 : 12-07-07 at 05:24 PM. |
||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|