【问题标题】:Trouble passing 2D array in C++ 8 Puzzle在 C++ 8 拼图中传递二维数组时遇到问题
【发布时间】:2014-02-08 02:13:22
【问题描述】:

由于某种原因,我无法让 distance 函数工作。有人可以向我解释我做错了什么吗?

主要功能:

#include <iostream>
#include <cmath> 
using namespace std;

int distance(int**, int);

 int main()
 {
 int m;
 int goal [3][3] = { {1,2,3},{4,5,6},{7,8,0}};
 const int N = 3;
 int intial[N][N];
 cout << "Enter in values: \n";
 for(int i=0; i<3; i++)    //This loops on the rows.
{
    for(int j=0; j<3; j++) //This loops on the columns
    {
        cin >> intial[i][j];  
    }
}


 cout << goal[0][0] << "\n" ;
 m = distance(intial,N);
 system("PAUSE");
 return 0;
 }

曼哈顿距离计算!

int distance(int** array,int N)
 {

  int MSum = 0;
  for (int x = 0; x < N; x++){ //Transversing rows(i)
      for (int y = 0; y < N; y++) { //traversing colums (j)
        int value = array[x][y]; // sets int to the value of space on board
            if (value != 0) { // Don't compute MD for element 0
               int targetX = (value - 1) / N; // expected x-coordinate (row)
               int targetY = (value - 1) % N; // expected y-coordinate (col)
               int dx = x - targetX; // x-distance to expected coordinate
               int dy = y - targetY; // y-distance to expected coordinate
               MSum += abs(dx) +abs(dy);   
            }
        }

  }
  int m = MSum;
  return m;

 }

【问题讨论】:

  • 你能编译成功没有任何错误吗?你能找出计算Manhattan Distance的源和目标吗? int distance(int** array,int N) 是否要计算所有距离的总和?
  • 问题实际上是要求在 C/C++ 中传递二维数组。不是计算曼哈顿距离。语法错误。参考stackoverflow.com/questions/8767166/…

标签: c++ artificial-intelligence sliding-tile-puzzle


【解决方案1】:

就逻辑而言,这看起来基本正确。您传入的数组应该与函数声明和实现兼容。这是一个示例声明:

int distance(int array[3][3],int N);

【讨论】:

  • 试过了,但它给了我一个错误如下:[链接器错误] undefined reference to `distance(int (*) [3], int)'
  • 您是否也尝试更改函数实现?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-29
  • 2014-01-25
  • 2013-10-26
  • 1970-01-01
  • 1970-01-01
  • 2011-04-20
  • 2011-07-28
相关资源
最近更新 更多