使用贪心法(回溯)寻找熄灯游戏的解决方案

Pre*_*4_7 4 c++ algorithm

我正在尝试使用回溯方法找到熄灯游戏的解决方案。我无法理解这个过程的算法。我的方法是枚举从 0 到 2 n 2 - 1 的所有整数,并将每个整数转换为具有 n*n 位的二进制数。然后,将其分成 n 2 个二进制数字(0 表示灯灭,1 表示灯亮)并将它们分配到 × n 的网格中,例如:我编写了以下代码:-

    void find_solution(int dest[][MAX_SIZE], int size) {

    int y = pow(size,size);
    int remainder;

    for (int x = 0; x<pow(2,y); x++){
        int i = 1;
        int binary_number = 0;
        int n = x;
        while (n!=0) {
            remainder = n%2;
            n/=2;
            binary_number += remainder*i;
            i *= 10;
        }
        int binary_number_digits[size][size];
        for (int k = 0; k<size; k++) {
            for (int l = 0; l<size; l++) {
                binary_number_digits[k][l] = binary_number%10;
                binary_number/=10;
            }
        }
        int count = 0;
        for (int i = 0; i<size; i++) {
            for (int j = 0; j<size; j++) {
                if (binary_number_digits[i][j] == dest[i][j]) {
                    count++;
                }
                if (count <= 4 && count > 0) {
                    if (binary_number_digits[i][j] == 1) {
                        cout << i << j;
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我已将十进制数字转换为二进制数字并将其存储在数组中,并检查它们是否与随机生成的 n*n 网格匹配。如果它是 1,则打印该坐标 (x,y)。任何人都可以帮我用这个算法解决问题。谢谢!

f9c*_*534 5

需要以下观察结果(如Wiki上所列):

  • 在解决方案中,每个灯最多必须按下一次。这是因为按奇数次相当于按一次灯,按偶数次相当于不按一次。

  • 按下灯的顺序并不重要。这是从上一点得出的:切换一盏灯会改变它的邻居,但对于邻居的最终结果来说,只有改变偶数或奇数次才重要。

由此我们可以得出结论,我们可以将一个解决方案表示为与棋盘大小相同的0-1矩阵,其中1表示该解决方案中该位置的光应该被按下。然后,暴力算法将检查所有 nxn 0-1 矩阵,看看其中是否有任何一个能够解决初始棋盘问题。

在您的实现中,您执行第一步(生成所有代表按下灯的方式的 nxn 0-1 矩阵)。您错过了检查其中哪些解决了棋盘问题的步骤。

我会使用std::bitset稍微简化二进制数处理。

(以下代码还需要 C++17 来实现std::Optional。)

#include <bitset>
#include <iostream>
#include <optional>
#include <random>

template <size_t N>
class board_t {
public:
  void print() const {

    for (size_t i = 0; i < data.size(); i++) {
      std::cout << data[i];

      if (i % N == N - 1) {
        std::cout << std::endl;
      }
    }
  }

  void randomize() {
    std::random_device device;
    std::default_random_engine generator{device()};
    std::bernoulli_distribution bernoulli(0.5);

    for (size_t i = 0; i < data.size(); i++) {
      data[i] = bernoulli(generator);
    }
  }

  /**
   * Brute-force all possible ways of pressing the lights.
   */
  std::optional<board_t<N>> solve() const {
    board_t<N> press{};

    do {
      board_t<N> applied{this->apply(press)};

      if (applied.data.none()) {
        return press;
      }

      press.increment();

      /**
       * Aborts when incrementing press overflows back to the initial
       * solution of not pressing any lamp.
       */
    } while (press.data.any());

    /**
     * Return empty std::optional when no solution was found.
     */
    return {};
  }

private:
  /**
   * Indicates which lights are on.
   */
  std::bitset<N * N> data;

  /**
   * Interpret the board as a N*N bit binary number and increment it by one.
   */
  void increment() {
    for (size_t i = 0; i < data.size(); i++) {
      if (data[i]) {
        data[i] = false;
      } else {
        data[i] = true;
        break;
      }
    }
  }

  /**
   * Press each light indicated by press.
   */
  board_t<N> apply(const board_t<N>& press) const {
    board_t<N> copy{*this};

    for (size_t y = 0; y < N; y++) {
      for (size_t x = 0; x < N; x++) {

        size_t offset = x + y * N;

        if (press.data[offset]) {
          copy.data.flip(offset);

          /**
           * Check neighbors.
           */
          if (x > 0) {
            copy.data.flip(offset - 1);
          }

          if (x < N - 1) {
            copy.data.flip(offset + 1);
          }

          if (y > 0) {
            copy.data.flip(offset - N);
          }

          if (y < N - 1) {
            copy.data.flip(offset + N);
          }
        }
      }
    }

    return copy;
  }
};

int main(void) {

  constexpr size_t N = 3;

  board_t<N> board{};

  board.randomize();
  board.print();

  auto solution{board.solve()};

  if (solution) {
    std::cout << "Solution:" << std::endl;
    solution->print();
  } else {
    std::cout << "No solution!" << std::endl;
  }

  return 0;
}
Run Code Online (Sandbox Code Playgroud)