6

ユニに戻って数学の授業にもっと注意を払うことを望みました。:)

ネイキッドトリプルにこの数式を実装するにはどうすればよいですか?

ネイキッドトリプル
ユニットUを共有する3つのセルC={c1、c2、c3}を取ります。3つの数値N = {n1、n2、n3}を取ります。Cの各セルが候補としてci⊆Nを持っている場合、Uの他のセルからすべてのni∈Nを削除できます。**

ユニット(ボックス、行、列など)をパラメーターとして受け取るメソッドがあります。ユニットには9個のセルが含まれているため、一度に3個のセルのすべての組み合わせを比較する必要があります。ボックスから、さらに計算するためにそれらをスタックまたはコレクションに入れます。

次のステップは、これらの3つのセルの組み合わせを1つずつ取得し、それらの候補を3つの数値と比較することです。繰り返しますが、これらの3つの数字は、1から9までの任意の組み合わせにすることができます。必要なのはそれだけです。

しかし、どうすればよいでしょうか。いくつの組み合わせが得られますか?セルに対して3x9 = 27の組み合わせを取得し、次に数値(N)に対して同じ組み合わせを取得しますか?

従来のC#ループでこれをどのように解決しますか?ラムダ式はありません。私はすでに十分に混乱しています:)

コード: ここでクラスを表すために、クラスを短くする必要がありました。

public class Cell : INotifyPropertyChanged
    {

public ObservableCollection<ObservableCollection<Candidate>> CandidateActual {...}

public int Id { ... }

//Position of the Cell inside a box if applicable
public int CellBoxPositionX { get; private set; }  
public int CellBoxPositionY { get; private set; }

//Position of the Cell inside the game board
public int CellBoardPositionX { get; private set; }
public int CellBoardPositionY { get; private set; }

//Position of the Box inside the game board
public int BoxPositionX { get; private set; }
public int BoxPositionY { get; private set; }

public int CountCandidates { ... }    
public int? Value { ...}

public Candidate this[int number]
        {
            get
            {
                if (number < 1 || number > PossibleValues.Count)
                {
                    throw new ArgumentOutOfRangeException("number", number, "Invalid Number Index");
                }

                switch (number)
                {
                    case 1:
                        return CandidateActual[0][0];
                    case 2:
                        return CandidateActual[0][1];
                    case 3:
                        return CandidateActual[0][2];
                    case 4:
                        return CandidateActual[1][0];
                    case 5:
                        return CandidateActual[1][1];
                    case 6:
                        return CandidateActual[1][2];
                    case 7:
                        return CandidateActual[2][0];
                    case 8:
                        return CandidateActual[2][1];
                    case 9:
                        return CandidateActual[2][2];
                    default:
                        return null;
                }
            }
        }
}

候補者

public class Candidate : INotifyPropertyChanged
    {

        private int? _value;

        public int? Value { ... }

    }

箱:

public class Box : INotifyPropertyChanged
    {

public ObservableCollection<ObservableCollection<Cell>> BoxActual { ... }

public Cell this[int row, int column]
        {
            get
            {
                if(row < 0 || row >= BoxActual.Count)
                {
                    throw new ArgumentOutOfRangeException("row", row, "Invalid Row Index");
                }
                if(column < 0 || column >= BoxActual.Count)
                {
                    throw new ArgumentOutOfRangeException("column", column, "Invalid Column Index");
                }
                return BoxActual[row][column];
            }
        }
}

ボード

public class Board : INotifyPropertyChanged 
    {

 public ObservableCollection<ObservableCollection<Box>> GameBoard {...}

public Cell this[int boardRowPosition, int boardColumnPosition]
        {
            get
            {
                int totalSize = GameBoard.Count*GameBoard.Count();
                if (boardRowPosition < 0 || boardRowPosition >= totalSize) 
                    throw new ArgumentOutOfRangeException("boardRowPosition", boardRowPosition, "Invalid boardRowPosition index");
                if (boardColumnPosition < 0 || boardColumnPosition >= totalSize)
                    throw new ArgumentOutOfRangeException("boardColumnPosition", boardColumnPosition, "Invalid boardColumnPosition index");
                return
                    GameBoard[boardRowPosition/GameBoard.Count][boardColumnPosition/GameBoard.Count][
                        boardRowPosition%GameBoard.Count, boardColumnPosition%GameBoard.Count];
            }
        }



        public Box this[int boardRowPosition, int boardColumnPosition, bool b]
        {
            get
            {
                int totalSize = GameBoard.Count * GameBoard.Count();
                if (boardRowPosition < 0 || boardRowPosition >= totalSize)
                    throw new ArgumentOutOfRangeException("boardRowPosition", boardRowPosition, "Invalid boardRowPosition index");
                if (boardColumnPosition < 0 || boardColumnPosition >= totalSize)
                    throw new ArgumentOutOfRangeException("boardColumnPosition", boardColumnPosition, "Invalid boardColumnPosition index");
                return
                    GameBoard[boardRowPosition / GameBoard.Count][boardColumnPosition / GameBoard.Count];
            }
        }
}

助けてくれてありがとう、

4

1 に答える 1

2

疑似コードアルゴリズム; 私のCは少し錆びています。

候補の値から、考えられる3つの数字の組み合わせをすべて見つけることをお勧めします。候補の数に応じて、このような組み合わせは6から504の範囲になります(n!/(3!*(n-3)!)で指定)。

それぞれについて、ユニット内のすべてのセルを循環し、それらが組み合わせにない番号がないという条件に一致するかどうかを確認します。特定の組み合わせが3つ以上ある場合は、それを適用できます。

combos = (array containing 3-long combination of candidates)
for each combo in combos                 # iterate through every combo
  matches = new array                    # initialize a blank array
  for each cell in unit
    if (cell does not contain candidates other than the ones in your current combo)
      matches.add(cell)                  # this is a match!
    end
  end

  if matches.size >= 3                   # naked triple found! (three matches for given combo)
    for each cell in unit
      if (cell is not in matches)
        (delete every candidate in current combo in this cell)
      end
    end
  end
  delete matches                         # clear up memory
end

お役に立てれば!必要に応じて、このコードをC-ifyします。とにかくCをブラッシュアップするつもりでした。

また、まだご存じない方のために説明すると、ロジックを手動でプログラミングする必要のない、コンピューターを使用して数独パズルを解くためのはるかに簡単な方法があります。しかし、私はあなたがそれをやろうとしている方法はかなり高貴だと思います。


可能なすべてのコンボの配列を生成する

これを行うには多くの方法があり、最良の方法があるかもしれません。自分で真剣に調べたことはありません。私はグーグルをお勧めします:組み合わせアルゴリズム...私は実際にCで1つの解決策を見つけました。

候補者の数が適切であることを確認するためのチェックを必ず含めてください。n = 3の場合、可能な候補の組み合わせは1つだけであり、アルゴリズムがそれを見つける必要があります。n=1およびn=2の場合、NakedTriplesは適用されません。

于 2010-06-11T21:02:34.860 に答える