【问题标题】:implementing Brute Force in c++在 C++ 中实现蛮力
【发布时间】:2013-03-31 18:45:47
【问题描述】:

我有一个问题涉及优化包含 2 名玩家的游戏。 所以我们有一个带有许多边和一些顶点的无向连通图。 每个玩家都必须删除边,如果一个顶点被孤立,他的分数会增加 一个,然后你必须删除另一个边缘。该图以所谓的 “邻接矩阵”。对于数组中的每一个,两个顶点之间都有一个连接例如,如果我们有一个三角形,我们有以下数组:

0 1 1 第一行

1 0 1 第二行

1 1 0 第三行

行对应于顶点,列对应于它与其他顶点的可能连接。我们被分配使用递归函数来申请 蛮力。我花了很多心思来编写这个函数的代码。一世 实现了一些代码并编译并运行它。我完全不知道为什么它不 功能。我的这个函数的代码:

   class Matrix {
        public:
    int berekenZet (bool** array, bool player);
    int berekenScore (bool** array, int rij, int kolom);
    bool checkRij (bool** array, int rij);
    void setGetal (int nummer) {getal = nummer;};
    Matrix ();



private:
    int getal;


    int maxScoreA;
    int huidigScore;
    int maxverschil;
    int maxScoreB;
    int tellerHuidig;

矩阵::矩阵() {

  huidigScore = 0;
maxScoreA = 0;
maxScoreB = 0;

}

 bool Matrix::checkRij (bool** array, int rij) {


            for (int i=0; i<getal; i++) {
    if (array[rij][i] == true)
        return false;
  }
  return true;  

}

    int Matrix::berekenScore (bool** Array, int rij, int kolom) {
if (checkRij (Array, rij) == true )
    huidigScore ++;
if (checkRij (Array, kolom) == true) 
    huidigScore ++;
return huidigScore;

}

  int Matrix::berekenZet (bool** array, bool player) {
  int score = 0;
  int bestescore = -5;
  int slechtstescore = 5;
  int something;
  something = 0;

  for(int i = 0; i < getal; i++){
  for(int j = i + 1; j <getal; j++){
  huidigScore = 0;

  if(array[i][j]){
    something++;
    array[i][j] = false;
    array[j][i] = false;
    score = 0;
    if(player == true){
      score = score + berekenScore(array, i ,j ); 
      }
    else{
      score = score - berekenScore(array, i, j);
      }
      cout << "player" << player << endl;
      cout << "score" << score << endl;


    if(huidigScore == 0)
      score = score + berekenZet(array, !player);
    else
      score = score + berekenZet(array, player);


    if(player == true && score > bestescore)
      bestescore = score; 
    else if(player == false && score < slechtstescore)
      slechtstescore = score;



      array[i][j] == true;
      array[j][i] == true;

  } //if
}//for
}//for 


if(player == true && something != 0){
cout << "bestescore" << bestescore << endl;
return bestescore;
}//  if outside of double for loop
else if(player == false && something != 0){
cout << "slechtstescore" << slechtstescore << endl;
return slechtstescore;
} // else if outside of double for loop  

else if(something = 0){
cout << "bestescore" << bestescore << endl;         
return 0;

}  // determine whether array was empty when function was called           

bestescore 和 slechtstescore 是荷兰语的最佳和最差分数。 berekenScore 调整优势后获得的点数 被删除并将其保存在 huidigScore 中。所以基本上是 0、1 或 2。

我在 int main 中调用了这个函数:

cout << "Aantal takken: " << takken << endl;

a = matrix.berekenZet(Array, player);

我使用以下邻接矩阵运行它:

5

0 1 1 1 1
1 0 1 0 0
1 1 0 0 0
1 0 0 0 0
1 0 0 0 0

它只输出 5 次“分数”,所以它似乎忽略了函数中的 for 循环, bestescore 也曾经设置为几百万值,然后设置为 5。我不是 非常有经验的程序员,所以我可能只是错过了一些东西..??

【问题讨论】:

    标签: c++ brute-force array-algorithms


    【解决方案1】:

    在您的示例中,您只有 5 条边,因此 score 仅输出五次。嵌套循环

    for(int i = 0; i for(int j = i + 1; j

    遍历所有可能的边,条件 if(array[i][j]) 过滤掉真正的边,所以循环只会执行 |E|次。

    另外,在你设置的循环中

    array[i][j] = false;
    array[j][i] = false;
    

    然后

    array[i][j] == true;
    array[j][i] == true;
    

    因此一次只删除一条边。您可能还需要一个同时删除多个边缘的循环来获得蛮力实现。

    编辑:您似乎没有在每次通话时都重置 huidigScore。我建议您按如下方式更改您的辅助函数:

    int Matrix::berekenScore (bool** Array, int rij, int kolom) 
    {
    int huidigScore = 0; 
    if (checkRij (Array, rij) == true )
      huidigScore ++; 
    if (checkRij (Array, kolom) == true) 
      huidigScore ++; 
    return huidigScore;
    }
    

    上述函数将 huidigScore 作为私有变量。现在,代替下面的代码 sn -p:

    if(player == true){
      score = score + berekenScore(array, i ,j ); 
      }
    else{
      score = score - berekenScore(array, i, j);
      }
      cout << "player" << player << endl;
      cout << "score" << score << endl;
    

    你可以拥有

    int huidigScore = berekenScore(array, i ,j )
    if(player == true){
      score = score + huidigScore; 
      }
    else{
      score = score - huidigScore;
      }
      cout << "player" << player << endl;
      cout << "score" << score << endl;
    

    一般建议:尽可能避免使用公共变量。它们使程序的推理和调试变得复杂。

    【讨论】:

    • 好的,谢谢你的回复,我明白你的解释,但我还是不明白为什么它只输出“分数”5次
    • ke 非常感谢您的回复,我理解您的解释,但是我仍然不明白为什么它只输出 5 次“分数”,因为每个函数都有一个双循环。我知道由于 if(array[i][j]) 条件,它会跳过所有错误的数组元素。我认为在第五个函数调用中,最后的 2 个数组元素应该设置为 false。但随后 aray = true;语句应该确保当一个值返回到前一个函数时,它可以在最后一个函数之前的第四个函数中删除这些数组元素。我不知道
    • 抱歉给您带来不便,我已经在原始问题中发布了其余代码。这包括 berekenScore 和 checkRij,都包含在 berekenScore 中
    • 好的。我看到一个直接的问题。您似乎没有重置全局变量 huidigScore。我建议您改为将 huidigScore 作为参数(通过引用传递)到 berekenScore,它在函数开头将其设置为零。
    • 好吧,我有一个类,包括私有的 huidigScore 和所有三个函数:berekenScore berekenZet en checkRij。我将使用类代码更新我的原始问题。但是 huidigScore 确实可能有问题。你如何建议我通过 huidigScore 参考 berekenScore?我是否应该在 berekenZet 中初始化 huidigScore,将其设置为 0,然后通过引用将其传递给 berekenScore?
    猜你喜欢
    • 1970-01-01
    • 2017-05-07
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多