ABA


"צריך עזרה עם באקטראקינג"
גירסת הדפסה        
קבוצות דיון פיתוח, תיכנות ובניית אתרים נושא #14641 מנהל    סגן המנהל    מפקח   Winner    צל"ש   מומחה  
אשכול מספר 14641
DLN
חבר מתאריך 20.4.07
15884 הודעות
   20:43   12.03.08   
אל הפורום  
  צריך עזרה עם באקטראקינג  
 
   כתבתי אלגוריתם לאתגר
שעובד עם באקטראקינג
ומסיבה כלשהי הבאקטראקינג פשוט נעצר שרירותית בלי שום סיבה באיזה שלב, ואני לא מצליח להבין למה...
אולי מישהו פה יבין
הרעיון הכללי הוא כזה, לפונקציה מועברת הנק' הסופית, נק' ההתחלה, שמשמשת כנק' הנוכחית, זה בעצם מפרק את זה כל פעם למסלול שמתחיל מנק' אחרת, קרובה או רחוקה יותר מהנק' הנוכחית, כאשר אי אפשר לצעוד באותו מקום פעמיים למסלול, מועברת גרסאת המפה הנוכחית (כאשר הנק' שעברו עליהם מסומנות ב2) ורשימה של נק' שכל פעם מתווספת אליה הנק' הנוכחית, ובמידה ומגיעים לנק' הסופית המסלול הנוכחי נשמר ברשימה של מסלולים שמוגדרת גלובלית.

// lPathfinder.cpp : Defines the entry mPoint for the console application.
//

#include "stdafx.h"
#include<stdio.h>
#include<list>
#include<stdlib.h>
#define n 5
#define m 4
struct mPoint
{
int x,y;
}; //Describes a Coordinate.
typedef int pMap[n][m]; //creates a map
typedef std::list<mPoint> lPath; // a single successful path from start to end
typedef std::list<lPath> lPaths; // a list of all successful paths
mPoint makeP(int x, int y); // Groups two values x and y to a single point
bool pCompare(mPoint a, mPoint b); //compares two points for equality. Returns true if the points are the same, false if not.
//void mPrint(pMap mp, lPath c); Not used
lPaths final;
void Pathfinder(mPoint stop, pMap mp, lPath current, mPoint loc) // Takes as parameters two points, stop(end point) and loc that serves as the relative start point, our map and the current path(empty on first call)
{
if (mp[loc.x][loc.y]==0) //Checking the course isnt started as a obstacle(0) point.
{
printf("**Error! You cannot start the path in a cell containing a '0'**\n");
return;
}
current.push_back(loc); //saves current point
mp[loc.x][loc.y]=2; //marks current point as passed-through
if (pCompare(stop,loc)) //checks if we've reached target point.
{
printf("**(%d,%d) - We are in stop point (%d,%d) Path Found! Adding to list of successful paths and starting over.\n",loc.x,loc.y,stop.x,stop.y);
final.push_front(current);
return;
}
//right direction
if (loc.y<m-1)
{
if(mp[loc.x][loc.y+1]==1)
{
printf("**(%d,%d) Going Right >> !**\n",loc.x,loc.y);
Pathfinder(stop,mp,current,makeP(loc.x,loc.y+1));
}
}
//left
if (loc.y>0)
{
if(mp[loc.x][loc.y-1]==1)
{
printf("**(%d,%d) Going Left << !**\n",loc.x,loc.y);
Pathfinder(stop,mp,current,makeP(loc.x,loc.y-1));
}
}
//down
if (loc.x<n-1)
{
if(mp[loc.x+1][loc.y]==1)
{
printf("**(%d,%d) Going Down vv !**\n",loc.x,loc.y);
Pathfinder(stop,mp,current,makeP(loc.x+1,loc.y));
}
}
//up
if (loc.x>0)
{
if(mp[loc.x-1][loc.y]==1)
{
printf("**(%d,%d) Going Up ^^ !**\n",loc.x,loc.y);
Pathfinder(stop,mp,current,makeP(loc.x-1,loc.y));
}
}
return;
}
int _tmain(int argc, _TCHAR* argv[])
{
int mp[5][4] = { { 1 , 0 , 0 , 0 } ,
{ 0 , 1 , 0 , 1 } ,
{ 1 , 1 , 1 , 1 } ,
{ 1 , 1 , 1 , 1 } ,
{ 0 , 1 , 0 , 0 } };
lPath c;
mPoint sl = makeP(1,1);
mPoint e = makeP(3,3);
mPoint temp;
Pathfinder(e,mp,c,sl);
while(!final.empty())
{
printf("------------------------------\nPath:\n");
c=final.front();
final.pop_front();
while(!c.empty())
{
temp=c.front();
c.pop_front();
printf("(%d,%d)\n",temp.x,temp.y);
}
//mPrint(mp,c); Not used
printf("End Path\n------------------------------");

}
system("PAUSE > NUL");
return 0;

}
mPoint makeP(int x, int y)
{
mPoint p;
p.x=x;
p.y=y;
return p;
}
bool pCompare(mPoint a, mPoint b)
{
return (bool)((a.x==b.x)&&(a.y==b.y));
}


                                שתף        
מכתב זה והנלווה אליו, על אחריות ועל דעת הכותב בלבד

  האשכול     מחבר     תאריך כתיבה     מספר  
  מה בעיה אלון כנס akoka 12.03.08 23:06 1

       
akoka

   23:06   12.03.08   
אל הפורום  
  1. מה בעיה אלון כנס  
בתגובה להודעה מספר 0
 
  


                                                         (ניהול: מחק תגובה)
מכתב זה והנלווה אליו, על אחריות ועל דעת הכותב בלבד

תגובה מהירה  למכתב מספר: 
 
___________________________________________________________________

___________________________________________________________________
למנהלים:  נעל | תייק בארכיון | מחק | העבר לפורום אחר | מחק תגובות | עגן אשכול
       



© כל הזכויות שמורות ל-רוטר.נט בע"מ rotter.net