4

回答: 私の疑似は再帰的な面であいまいですが、このビデオは以下のリソースとともに役立ちます

http://www.youtube.com/watch?v=p-gpaIGRCQI

http://norvig.com/sudoku.html

数独パズルに関して、このバックトラック再帰アルゴリズムの実装を把握できません。

再帰バックトラッキングを使用して数独パズルを解こうとしています。私が取り組んでいる問題の領域を考えると、私はまだ頭の中で一般的なアルゴリズムをラップすることができません.

私が使用しようとしているバックトラッキング アルゴリズムは標準的なようですが、ロジックをたどることができず、その下で何が起こっているのかを知ることができません。

バックトラッキングアルゴリズムとその定義が含まれています。

編集:「再び、クラス定義を取り出し、宣言を残して疑似コードを立てました」

これを利用した擬似コードを次に示します。

疑似コード (C++ 実装) バックトラック ゲーム (81,9) // ゲームの入力と値のすべての可能な組み合わせを表す

    //All info is loading into a vector of size 81 with the initial state 
    //puzzle = the initial state 9x9 grid from left to right of integers  
        vector <int> puzzle
while(!not solved && not the end of the vector){
     for(int i =puzzle.begin::iterator i , puzzle.end()) //from 0-80 of the vector until end
        if puzzle[i] != 0
             //leave alone, original state of board
        else
              if (valid move) //a guess is allowed in this column/row/square of the board  
                   solution[i] = puzzle_guess[i]  //guess a move and insert 
              else  // one of previous guesses were wrong
                   game.prune(i); //backtracks, or reverses guesses until valid move
}

// ゲームの初期状態

    4 0 0  6 0 5  2 0 3
    0 0 0  0 4 9  0 7 5
    0 0 0  1 0 7  6 0 0

    6 0 1  0 0 0  4 8 7
    0 8 0  0 0 0  0 3 0
    2 7 4  0 0 0  5 0 6

    0 0 8  7 0 3  0 0 0
    3 1 0  9 6 0  0 0 0
    7 0 9  2 0 8  0 0 1

ありがとうございました

知っておくべき唯一の手がかりは、バックトラック ゲーム (81,9) を使用した宣言です。

#ifndef BACKTRACK_H
#define BACKTRACK_H

#include <vector>
#include <algorithm>

class BackTrack {
public:
  typedef std::vector<unsigned>::const_iterator const_iterator;
  typedef std::vector<unsigned>::const_iterator iterator;

  BackTrack (unsigned nVariables, unsigned arity=2);
  // Create a backtracking state for a problem with
  // nVariables variables, each of which has the same
  // number of possible values (arity).

  template <class Iterator>
  BackTrack (Iterator arityBegin,
         Iterator arityEnd);
  // Create a backtracking state in which each variable may have
  // a different number of possible values. The values are obtained
  // as integers stored in positions arityBegin .. arityEnd as per
  // the usual conventions for C++ iterators. The number of
  // variables in the system are inferred from the number of
  // positions in the given range.

  unsigned operator[] (unsigned variableNumber) const;
  // Returns the current value associated with the indicated
  // variable.

  unsigned numberOfVariables() const;
  // Returns the number of variables in the backtracking system.

  unsigned arity (unsigned variableNumber) const;
  // Returns the number of potential values that can be assigned
  // to the indicated variable.

  bool more() const;
  // Indicates whether additional candidate solutions exist that
  // can be reached by subsequent ++ or prune operaations.

  void prune (unsigned level);
  // Indicates that the combination of values associated with
  // variables 0 .. level-1 (inclusive) has been judged unacceptable
  // (regardless of the values that could be given to variables
  // level..numberOfVariables()-1.  The backtracking state will advance
  // to the next solution in which at least one of the values in the
  // variables 0..level-1 will have changed.

  BackTrack& operator++();
  // Indicates that the combination of values associated with
  // variables 0 .. nVariables-1 (inclusive) has been judged unacceptable.
  // The backtracking state will advance
  // to the next solution in which at least one of the values in the
  // variables 0..level-1 will have changed.

  BackTrack operator++(int);
  // Same as other operator++, but returns a copy of the old backtrack state


  // Iterator operations for easy access to the currently assigned values
  const_iterator begin() const {return values.begin();}
  iterator begin()             {return values.begin();}

  const_iterator end() const {return values.end();}
  iterator       end()       {return values.end();}

private:
  bool done;
  std::vector<unsigned> arities;
  std::vector<unsigned> values;

};
inline
unsigned BackTrack::operator[] (unsigned variableNumber) const
  // Returns the current value associated with the indicated
  // variable.
{
  return values[variableNumber];
}

inline
unsigned BackTrack::numberOfVariables() const
  // Returns the number of variables in the backtracking system.
{
  return values.size();
}

inline
unsigned BackTrack::arity (unsigned variableNumber) const
  // Returns the number of potential values that can be assigned
  // to the indicated variable.
{
  return arities[variableNumber];
}


inline
bool BackTrack::more() const
  // Indicates whether additional candidate solutions exist that
  // can be reached by subsequent ++ or prune operaations.
{
  return !done;
}

template <class Iterator>
BackTrack::BackTrack (Iterator arityBegin,
              Iterator arityEnd):
  // Create a backtracking state in which each variable may have
  // a different number of possible values. The values are obtained
  // as integers stored in positions arityBegin .. arityEnd as per
  // the usual conventions for C++ iterators. The number of
  // variables in the system are inferred from the number of
  // positions in the given range.
  arities(arityBegin, arityEnd), done(false) 
{
  fill_n (back_inserter(values), arities.size(), 0);
}


#endif
4

2 に答える 2

2

Sudoku のポイントは、膨大な数の状態があることです。9^81 は 78 桁の数です。したがって、左上のフィールドから開始して右下に向かって処理する「愚かな」バックトラッキング アルゴリズムは、一見「無限」のループに陥る可能性があります。

したがって、人間が行うように数独を解くことをお勧めします。既に入力されている数字が 1 つの特定の値のみを許可するフィールドを見つけ、そのフィールドに入力します。次に、そのようなフィールドを探します。有効な値が 1 つだけの空のフィールドがもう存在しない場合は、最大で 2 つ (または一般的に可能な最小数) の値を許可するフィールドを探し、それらの値の 1 つを試して、再帰を続行します。矛盾が生じた場合はバックトラックし、いくつかの選択肢があるフィールドの次の値を試します。

擬似コード:

solve(game)
    if (game->is_solved())
        game->print()
        return SUCCESS
    else
        next_square = game->find_most_constrained_square()
        foreach value = next_square->possible_values
            mygame = copyof(game)
            mygame->move(next_square, value)
            if solve(mygame) return SUCCESS
        endforeach
        return FAILURE
    endif

関数 find_most_constrained_square() は、空のフィールドごとにカウントし、そこにまだいくつの異なる数字を入れることができるかを数え、可能な数が最も少ないフィールドへのインデックスを返します。これは、選択肢が 0 のフィールドである可能性もあります。

その修正された再帰を使用すると、低速のコンピューターで低速の言語を使用する場合でも、Sudoko の問題をすばやく解決できます。foreach 内部ループで作成されたゲーム ステートのさまざまなコピーを破棄することを忘れないでください。

于 2013-08-11T10:44:56.413 に答える