Hello :) A* algorithm trouble

Hello I'm a new member of this forum, anyway to business I've been trying to make a app which searches the shortest route between point A and B in a coordinate system... here is what i have so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <cstdlib>
#include <iostream>
#include <windows.h>

using namespace std;

int main(int argc, char *argv[])
{

    
    int pocetokx,pocetoky,krajx,krajy;
    COORD CLOSED[100000];
    COORD PARENT[100000];
    COORD RUTA[30];

    int min,max;
    cin>>min>>max;
    cin>>pocetokx>>pocetoky;
    cin>>krajx>>krajy;
    int i,j;
    int open,check,last;
    for (i=1;i<=100000;i++){ CLOSED[i].X=-1; CLOSED[i].Y=-1; PARENT[i].X=-1; PARENT[i].Y=-1;}
    for (i=1;i<=30;i++){ RUTA[i].X=-1; RUTA[i].Y=-1;}
    CLOSED[1].X=pocetokx;
    CLOSED[1].Y=pocetoky;
    
    check=1;
    i=1;
    while( CLOSED[check].Y!=krajy || CLOSED[check].X!=krajx ){
           for(j=i+1;j<=100000;j++){ if(PARENT[j].X==CLOSED[i].X & PARENT[j].Y==CLOSED[i].Y){ i++; break; check=0;}}
                        if(check!=0){
                                     for(j=2;j<=100000;j++){ if(CLOSED[j].X==-1 & CLOSED[j].Y==-1){ open=j; break;}}
                                     if(CLOSED[i].X+1!=PARENT[i].X & CLOSED[i].X+1<=max)
                                     {CLOSED[open].X=CLOSED[i].X+1; CLOSED[open].Y=CLOSED[i].Y; PARENT[open].X=CLOSED[i].X; PARENT[open].Y=CLOSED[i].Y;} 
                                     for(j=2;j<=100000;j++){ if(CLOSED[j].X==-1 & CLOSED[j].Y==-1){ open=j; break;}}
                                     if(CLOSED[i].X-1!=PARENT[i].X & CLOSED[i].X-1>=min)
                                     {CLOSED[open].X=CLOSED[i].X-1; CLOSED[open].Y=CLOSED[i].Y; PARENT[open].X=CLOSED[i].X; PARENT[open].Y=CLOSED[i].Y;}
                                     for(j=2;j<=100000;j++){ if(CLOSED[j].X==-1 & CLOSED[j].Y==-1){ open=j; break;}}
                                     if(CLOSED[i].Y+1!=PARENT[i].Y & CLOSED[i].Y+1<=max)
                                     {CLOSED[open].Y=CLOSED[i].Y+1; CLOSED[open].X=CLOSED[i].X; PARENT[open].X=CLOSED[i].X; PARENT[open].Y=CLOSED[i].Y;}
                                     for(j=2;j<=100000;j++){ if(CLOSED[j].X==-1 & CLOSED[j].Y==-1){ open=j; break;}}
                                     if(CLOSED[i].Y-1!=PARENT[i].Y & CLOSED[i].Y-1>=min)
                                     {CLOSED[open].Y=CLOSED[i].Y-1; CLOSED[open].X=CLOSED[i].X; PARENT[open].X=CLOSED[i].X; PARENT[open].Y=CLOSED[i].Y;}
                                                                                      
                                     for(j=2;j<=100000;j++){ if(CLOSED[j].X==-1 & CLOSED[j].Y==-1){ open=j; break;}}
                                     if(CLOSED[i].X+1!=PARENT[i].X & CLOSED[i].X+1<=max & CLOSED[i].Y+1!=PARENT[i].Y & CLOSED[i].Y+1<=max)
                                     {CLOSED[open].X=CLOSED[i].X+1; CLOSED[open].Y=CLOSED[i].Y+1; PARENT[open].X=CLOSED[i].X; PARENT[open].Y=CLOSED[i].Y;} 
                                     for(j=2;j<=100000;j++){ if(CLOSED[j].X==-1 & CLOSED[j].Y==-1){ open=j; break;}}
                                     if(CLOSED[i].X+1!=PARENT[i].X & CLOSED[i].X+1<=max & CLOSED[i].Y-1!=PARENT[i].Y & CLOSED[i].Y-1>=min)
                                     {CLOSED[open].X=CLOSED[i].X+1; CLOSED[open].Y=CLOSED[i].Y-1; PARENT[open].X=CLOSED[i].X; PARENT[open].Y=CLOSED[i].Y;} 
                                     for(j=2;j<=100000;j++){ if(CLOSED[j].X==-1 & CLOSED[j].Y==-1){ open=j; break;}}
                                     if(CLOSED[i].X-1!=PARENT[i].X & CLOSED[i].X-1>=min & CLOSED[i].Y-1!=PARENT[i].Y & CLOSED[i].Y-1>=min)
                                     {CLOSED[open].X=CLOSED[i].X-1; CLOSED[open].Y=CLOSED[i].Y-1; PARENT[open].X=CLOSED[i].X; PARENT[open].Y=CLOSED[i].Y;} 
                                     for(j=2;j<=100000;j++){ if(CLOSED[j].X==-1 & CLOSED[j].Y==-1){ open=j; break;}}
                                     if(CLOSED[i].X-1!=PARENT[i].X & CLOSED[i].X-1>=min & CLOSED[i].Y+1!=PARENT[i].Y & CLOSED[i].Y+1<=max)
                                     {CLOSED[open].X=CLOSED[i].X-1; CLOSED[open].Y=CLOSED[i].Y+1; PARENT[open].X=CLOSED[i].X; PARENT[open].Y=CLOSED[i].Y;}                             
                                                                                      
                                     i++;
                                     for(j=1;j<=100000;j++){ if(CLOSED[j].X==krajx & CLOSED[j].Y==krajy){ check=j; break;}} 
                                     }
                                     else {i++; check=1;}  
                                                                                                              
           }
    cout<<check<<" "<< i<<"\n";
                       
    
    int k=1;                   
    while(check>1){
                   RUTA[k].X=CLOSED[check].X;
                   RUTA[k].Y=CLOSED[check].Y;
                   for(j=1;j<=100000;j++){ if(CLOSED[j].X==PARENT[check].X & CLOSED[j].Y==PARENT[check].Y){ check=j; break;}}
                   k++;
                   }
    RUTA[k].X=CLOSED[1].X;
    RUTA[k].Y=CLOSED[1].Y;          
         
    
    for (i=k;i>=1;i--){
                       cout<< RUTA[i].X<<","<< RUTA[i].Y<<"  ";
                       }
   

    
    system("PAUSE");
    return EXIT_SUCCESS;
}



Weird thing is it actually works... but as distance increases so does lag and eventually just freezes everything, i can get results for points 0,0 and 12,12 beyond that it's just lag... pocetokx/y being point A and krajx/y being point B...

Please give me some tips or anything so i could improve my code
Last edited on
Why do you have all those for and if statements that all do the same thing? aren't you supposed to keep track of an open set in in A* that you get the most promising node from each time?
they don't do the same thing, they move for different locations. x+, y+,x-,y-,x+y+,x-y-,x+y-,x-y+...

I don't see the difference between keeping a seperate open set and this, the open NODE in here is CLOSED[>i]
Topic archived. No new replies allowed.