C++中传递二维数组的问题

不知为何,我无法让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; }

回答:

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

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

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注