forked from xtaci/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathastar_demo.cpp
More file actions
58 lines (46 loc) · 1015 Bytes
/
Copy pathastar_demo.cpp
File metadata and controls
58 lines (46 loc) · 1015 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "astar.h"
#define N 128
#define MARK 0xEE
int main(void)
{
using namespace alg;
Array2D<unsigned char> grid(N,N);
grid.clear(0);
srand(time(NULL));
int i,j;
for (i=N/4;i<=3*N/4;i++) {
grid(3*N/4,i) = AStar::WALL;
grid(i,3*N/4) = AStar::WALL;
}
grid(0,0) = 0;
grid(N-1,N-1) = 0;
printf("search a path from (0,0) to (%d,%d)\n", N-1,N-1);
for (i=0;i<N;i++) {
for(j=0;j<N;j++){
if (grid(i,j) == AStar::WALL) { printf(" "); }
else printf(".");
}
printf("\n");
}
printf("path:\n");
AStar astar(grid);
AStar::AStarResult * as = astar.run(0,0, N-1,N-1);
for(i=0;i<as->num_nodes;i++){
printf("(%d,%d)\t",as->path[i*2], as->path[i*2+1]);
grid(as->path[i*2],as->path[i*2+1]) = MARK;
}
printf("\n");
for (i=0;i<N;i++) {
for(j=0;j<N;j++){
if (grid(i,j) == AStar::WALL) { printf(" "); }
else if (grid(i,j) == MARK) {printf("X");}
else printf(".");
}
printf("\n");
}
printf("\n");
return 0;
}