Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 12-06-07   #1 (permalink)
*cough* Stock *cough*
 
icyblizard's Avatar
 
intel nvidia

Join Date: Dec 2006
Location: Singapore
Posts: 46

Rep: 2 icyblizard Unknown
Unique Rep: 2
Trader Rating: 0
Exclamation Question about C++ recusive pathfinding

Mods delete if you see this
__________________
Behold : the lousiest processor in this forum! P4 2.4GHz

System: Dell
CPU
P4 2.4GHz
Motherboard
not sure
Memory
Kingston 768 Mb DDR-333
Graphics Card
Geforce 4 Mx 400
Hard Drive
Hitachi 160 GB HDD
Sound Card
Creative Soundblaster 5.1
Power Supply
icute 420W
Case
stock
CPU cooling
stock
GPU cooling
none
OS
Wins XP
Monitor
not sure

Last edited by icyblizard : 12-10-07 at 06:19 AM.
icyblizard is offline   Reply With Quote
Old 12-06-07   #2 (permalink)
C0019 13210
 
stargate125645's Avatar
 
intel nvidia

Join Date: Sep 2005
Location: Omaha, NE
Posts: 7,344

Rep: 350 stargate125645 is a proven memberstargate125645 is a proven memberstargate125645 is a proven memberstargate125645 is a proven member
Unique Rep: 225
FAQs Submitted: 1
Hardware Reviews: 9
Trader Rating: 21
Default

Quote:
Originally Posted by icyblizard View Post
Does anyone know how to do a pathfinding function than has less than 10 lines of code? We have to do something like a mouse looking for a cheese
****************
*C *
************ *
* * *
*M ********* *
* *
****************
This is just an example, we actually have to load a txt file and save it into an array (16 length by 8height).
C - cheese
M - mouse
* - wall
The thing is, i have done it using recursions and my code is like 80 lines and yet the lecturer said it was possible to do it with just 8-9 lines of actual code
I think im going to be like *** when he shows us his code
Don't use any spaces, and it will be 1 line of code. 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 ]

System: BladeRunner
CPU
Core 2 Duo E6700 @ 3.361GHz 24/7
Motherboard
ASUS P5N32-E SLI
Memory
2 x 2GB G.Skill PKs DDR2 1066 @ 1092MHz 24/7
Graphics Card
VisionTek HD 4870X2 @ 792MHz/1950MHz 24/7
Hard Drive
4 x 36GB WD Raptors in RAID-0, 750GB WD AAKS
Sound Card
X-Fi XtremeGamer Fatal1ty Professional
Power Supply
Ultra X3 1000W
Case
Thermaltake Armor with A2400 (upgraded 25cm fan)
CPU cooling
Tuniq Tower 120 (lapped) with Silverstone FM121
GPU cooling
Stock
OS
Windows Vista Ultimate 64-bit
Monitor
BenQ V2400W 24" Monitor
stargate125645 is offline Overclocked Account stargate125645's Gallery   Reply With Quote
Old 12-06-07   #3 (permalink)
*cough* Stock *cough*
 
icyblizard's Avatar
 
intel nvidia

Join Date: Dec 2006
Location: Singapore
Posts: 46

Rep: 2 icyblizard Unknown
Unique Rep: 2
Trader Rating: 0
Default

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

System: Dell
CPU
P4 2.4GHz
Motherboard
not sure
Memory
Kingston 768 Mb DDR-333
Graphics Card
Geforce 4 Mx 400
Hard Drive
Hitachi 160 GB HDD
Sound Card
Creative Soundblaster 5.1
Power Supply
icute 420W
Case
stock
CPU cooling
stock
GPU cooling
none
OS
Wins XP
Monitor
not sure

Last edited by icyblizard : 12-06-07 at 08:36 AM.
icyblizard is offline   Reply With Quote
Old 12-06-07   #4 (permalink)
Programmer
 
kdbolt70's Avatar
 
intel ati

Join Date: May 2007
Location: Walled Lake, MI
Posts: 1,119

Rep: 127 kdbolt70 is acknowledged by manykdbolt70 is acknowledged by many
Unique Rep: 92
Folding Team Rank: 284
Trader Rating: 1
Default

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!
__________________

~M Hail to the Victors M~

System: It's about time!
CPU
Q6600 G0 @3.3Ghz
Motherboard
Gigabyte P35-DS3L
Memory
2Gb Ballistix DDR2 800 @915Mhz
Graphics Card
Sapphire 2900Pro Flashed to XT
Hard Drive
Seagate Barracuda 320Gb
Sound Card
Onboard
Power Supply
Corsair HX 620W
Case
CM 690
CPU cooling
Tuniq Tower 120
GPU cooling
stock
OS
Vista Business and VMWare Ubuntu
Monitor
Acer AL2223W 22"
kdbolt70 is offline I fold for Overclock.net   Reply With Quote
Old 12-06-07   #5 (permalink)
*cough* Stock *cough*
 
icyblizard's Avatar
 
intel nvidia

Join Date: Dec 2006
Location: Singapore
Posts: 46

Rep: 2 icyblizard Unknown
Unique Rep: 2
Trader Rating: 0
Default

Not the whole code, just the pathfinding part
__________________
Behold : the lousiest processor in this forum! P4 2.4GHz

System: Dell
CPU
P4 2.4GHz
Motherboard
not sure
Memory
Kingston 768 Mb DDR-333
Graphics Card
Geforce 4 Mx 400
Hard Drive
Hitachi 160 GB HDD
Sound Card
Creative Soundblaster 5.1
Power Supply
icute 420W
Case
stock
CPU cooling
stock
GPU cooling
none
OS
Wins XP
Monitor
not sure
icyblizard is offline   Reply With Quote
Old 12-06-07   #6 (permalink)
Intel Overclocker
 
dante020's Avatar
 
intel nvidia

Join Date: Sep 2007
Location: Novi, MI
Posts: 203

Rep: 34 dante020 is acknowledged by some
Unique Rep: 23
Folding Team Rank: 126
Trader Rating: 4
Default

Well, does it have to be the shortest path, or just any path? And how do you display the path that you traveled?
__________________
System: The System
CPU
Q6600 @ 3.2Ghz - 356FSB
Motherboard
Asus P5Q Pro
Memory
OCZ Platinum Rev. 2 4x1GB
Graphics Card
EVGA 8800GT 512MB
Hard Drive
WD Raptor 150GB x 2
Sound Card
Creative X-Fi XtremeGamer
Power Supply
Corsair CMPSU-620HX 620W
Case
Antec P180B
CPU cooling
Scythe Ninja SCNJ-1000
OS
Windows Vista Ultimate x64
Monitor
Samsung 226BW 22" LCD
dante020 is offline I fold for Overclock.net   Reply With Quote
Old 12-07-07   #7 (permalink)
*cough* Stock *cough*
 
icyblizard's Avatar
 
intel nvidia

Join Date: Dec 2006
Location: Singapore
Posts: 46

Rep: 2 icyblizard Unknown
Unique Rep: 2
Trader Rating: 0
Default

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

System: Dell
CPU
P4 2.4GHz
Motherboard
not sure
Memory
Kingston 768 Mb DDR-333
Graphics Card
Geforce 4 Mx 400
Hard Drive
Hitachi 160 GB HDD
Sound Card
Creative Soundblaster 5.1
Power Supply
icute 420W
Case
stock
CPU cooling
stock
GPU cooling
none
OS
Wins XP
Monitor
not sure
icyblizard is offline   Reply With Quote
Old 12-07-07   #8 (permalink)
Intel Overclocker
 
dante020's Avatar
 
intel nvidia

Join Date: Sep 2007
Location: Novi, MI
Posts: 203

Rep: 34 dante020 is acknowledged by some
Unique Rep: 23
Folding Team Rank: 126
Trader Rating: 4
Default

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;
}
__________________
System: The System
CPU
Q6600 @ 3.2Ghz - 356FSB
Motherboard
Asus P5Q Pro
Memory
OCZ Platinum Rev. 2 4x1GB
Graphics Card
EVGA 8800GT 512MB
Hard Drive
WD Raptor 150GB x 2
Sound Card
Creative X-Fi XtremeGamer
Power Supply
Corsair CMPSU-620HX 620W
Case
Antec P180B
CPU cooling
Scythe Ninja SCNJ-1000
OS
Windows Vista Ultimate x64
Monitor
Samsung 226BW 22" LCD

Last edited by dante020 : 12-07-07 at 10:37 AM.
dante020 is offline I fold for Overclock.net   Reply With Quote
Old 12-07-07   #9 (permalink)
Programmer
 
kdbolt70's Avatar
 
intel ati

Join Date: May 2007
Location: Walled Lake, MI
Posts: 1,119

Rep: 127 kdbolt70 is acknowledged by manykdbolt70 is acknowledged by many
Unique Rep: 92
Folding Team Rank: 284
Trader Rating: 1
Default

Quote:
Originally Posted by icyblizard View Post
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.
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
__________________

~M Hail to the Victors M~

System: It's about time!
CPU
Q6600 G0 @3.3Ghz
Motherboard
Gigabyte P35-DS3L
Memory
2Gb Ballistix DDR2 800 @915Mhz
Graphics Card
Sapphire 2900Pro Flashed to XT
Hard Drive
Seagate Barracuda 320Gb
Sound Card
Onboard
Power Supply
Corsair HX 620W
Case
CM 690
CPU cooling
Tuniq Tower 120
GPU cooling
stock
OS
Vista Business and VMWare Ubuntu
Monitor
Acer AL2223W 22"
kdbolt70 is offline I fold for Overclock.net   Reply With Quote
Old 12-07-07   #10 (permalink)
Intel Overclocker
 
dante020's Avatar
 
intel nvidia

Join Date: Sep 2007
Location: Novi, MI
Posts: 203

Rep: 34 dante020 is acknowledged by some
Unique Rep: 23
Folding Team Rank: 126
Trader Rating: 4
Default

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--;
	}
}
__________________
System: The System
CPU
Q6600 @ 3.2Ghz - 356FSB
Motherboard
Asus P5Q Pro
Memory
OCZ Platinum Rev. 2 4x1GB
Graphics Card
EVGA 8800GT 512MB
Hard Drive
WD Raptor 150GB x 2
Sound Card
Creative X-Fi XtremeGamer
Power Supply
Corsair CMPSU-620HX 620W
Case
Antec P180B
CPU cooling
Scythe Ninja SCNJ-1000
OS
Windows Vista Ultimate x64
Monitor
Samsung 226BW 22" LCD

Last edited by dante020 : 12-07-07 at 05:24 PM.
dante020 is offline I fold for Overclock.net   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 03:24 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.38128 seconds with 8 queries