2016-04-23 19 views
0

プログラミングに慣れていないので、C++で星印検索アルゴリズムを実装しようとしています。私はセグメンテーションフォールトを持っている:11私のポインタを初期化していないため。私はそれをいくつかの異なる方法で無駄にしようとしました。 私はまだ全体のポインタと動的メモリ割り当ての概念について混乱しています。多次元配列への動的ポインタの初期化

誰でも私を把握するのに役立つことができますか?ありがとうございました。

#include <iostream> 
    #include <vector> 
    #include <fstream> 
    #include <math.h> 
    #include <stdio.h> 
    #include <string> 
    #include <vector> 
    #include <iostream> 
    #include <fstream> 

    using namespace std; 


    // Definition of the heuristic. The heuristic in this problem is the distance between 
    // two coordinates 
    double heuristic(double x1, double y1, double x2, double y2) { 
     double dx, dy; 
     dx = x1 - x2; 
     dy = y1 - y2; 
     return sqrt(dx*dx - dy*dy); 
     //return sqrt(pow((x1 - x2), 2) + pow((y1 - y2), 2)); 
    } 

    // ----- A Star Search Algorithm (f = g + h)---- 
    double** a_star_search(double points[][2]) { 
     int count = 1; 
     double** points1 = NULL; 
    // points1[10][2]; 
     double x1 = points[0][0]; 
     double y1 = points[0][1]; 
     points1[count - 1][0] = x1; 
     points1[count - 1][1] = y1; 
     while (count <= 10) { 
      double tempx1; 
      double tempy1; 
      double distance = 10000000; 
      for (int i = 0; i < 10; i++) { 
       if (points[i][0] != 0 && points[i][1] != 0) { 
        double distance2 = heuristic(x1, y1, points[i][0], points[i][1]); 
        if (distance2 < distance) { 
         tempx1 = points[i][0]; 
         tempy1 = points[i][1]; 
         distance = distance2; 
        } 
       } 
      } 
      x1 = tempx1; 
      y1 = tempy1; 
      count++; 
      points1[count - 1][0] = x1; 
      points1[count - 1][1] = y1; 
     } 
     return points1; 
    } 

    int main() { 
     double points[7][2]; 
     int counter = 0; 
     ifstream infile("waypoints.txt"); 
     int a, b; 
     while (infile >> a >> b) 
     { 
      points[counter][0] = a; 
      points[counter][1] = b; 
      counter++; 
     } 
     points[6][0] = points[0][0]; 
     points[6][1] = points[0][1]; 
     double** points1 = a_star_search(points); 

     cout << "Initial Sequence: "; 
     for (int i = 0;i < 7;i++) { 
      cout << "(" <<points[i][0] << " , " << points[i][1] << "), "; 
     } 
     cout << "\n\nOptimized Sequence: "; 
     for (int i = 0;i < 7;i++) { 
      cout << "(" << points1[i][0] << " , " << points1[i][1] << "), "; 
     } 
     cout << "\n\nTotal Distance after A* search: "; 
     double totaldistance = 0; 
     for (int i = 0;i < 6;i++) { 
      double dis = heuristic(points1[i][0], points1[i][1], points1[i + 1][0], points1[i + 1][1]); 
      cout << dis << "+"; 
      totaldistance = totaldistance + dis; 
     } 
     cout<< "=" << totaldistance <<endl; 
    } 
+0

多くの方法を試しましたか?動的配列を割り当てる?あなたが見た場合、何千もの場所を行う方法を見つけるでしょうが、気にしないでください。動的配列の代わりに 'std :: vector'を試してみてください。あなたに多くの面倒を保存します。 – user4581301

答えて

0

あなたのa_star_search機能でNULLにそれを設定した後double** points1変数に動的にメモリを割り当てていません。 @ user4581301で指摘されているように、std::vectorを使用してください。これにより、コードを大幅に簡素化し、STLコンテナを学習する時間を費やす価値があります。

関連する問題