B
B
Boris2013-12-13 10:22:18
C++ / C#
Boris, 2013-12-13 10:22:18

How can I set the size of the map through the console, so as not to use const int (Algorithm A is old)?

Here is a link to the general code of the A star algorithm: code.activestate.com/recipes/577457-a-star-shortest-path-algorithm/
Tried to use dynamic arrays (made the following changes):
int n;
intm;
static int **map;
static int **closed_nodes_map;
static int **open_nodes_map;
static int **dir_map;
....
int main()
{
cout<<"enter n"<>n;
cout<<"enter m"<>m;
map = new int*[n];
for(int i = 0; i < n; i++)
{
map[i] = new int [m];
}
closed_nodes_map = new int* [n];
for(int i = 0; i < n; i++)
{
closed_nodes_map[i] = new int [m];
open_nodes_map = new int*[n];
for(int i = 0; i < n; i++)
{
open_nodes_map[i] = new int [m];
}
dir_map = new int*[n];
for(int i = 0; i < n; i++)
{
dir_map[i] = new int [m];
...
And on startup, after typing n and m, an error occurs: Unhandled exception at '0x010d29fb' in 'TestAStar.exe': 0xC0000005: Access violation writing '0xfdfdfe25'. How to solve this problem?
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
jcmvbkbc, 2013-12-13
@jcmvbkbc

// reset the node maps
    for(y=0;y<m;y++)
    {
        for(x=0;x<n;x++)
        {
            closed_nodes_map[x][y]=0;
            open_nodes_map[x][y]=0;
        }
    }

/******  после этих циклов x == n и y == m  *******/

    // create the start node and push into list of open nodes
    n0=new node(xStart, yStart, 0, 0);
    n0->updatePriority(xFinish, yFinish);
    pq[pqi].push(*n0);

/******  и эта запись -- мимо массива  *******/
    open_nodes_map[x][y]=n0->getPriority(); // mark it on the open nodes map

T
Teivaz, 2013-12-15
@Teivaz

For such an array, you need to allocate memory in a loop, or immediately in one large block, but write pointers to the array with a step per line.

int** Allocate2dArray(int w, int h)
{
  int** a;
  // сначала выделяем память под указатели на столбцы
  a = new int*[w];
  for (int i = 0; i < w; ++i)
  {
    // выделяем память под каждый столбец
    a[i] = new int[h];
  }
  return a;
}

or so
int** Allocate2dStraight(int w, int h)
{
  int** a;
  a = new int*[w];
  // одним махом выделяем память под все столбцы
  *a = new int[w * h];
  for (int i = 1; i < w; ++i)
  {
    // и просто раздаём адреса
    a[i] = a[i-1] + w;
  }
  return a;
}

and remove in reverse order.

J
jcmvbkbc, 2013-12-13
@jcmvbkbc

cout<<"enter n"<>n;
cout<<"enter m"<>m;

What it is? I suspect that either someone chewed on the markup, or n and m remained uninitialized.
Further code seems to be ok.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question