我希望我更加關注Uni中的數學課。 :)解決Sudoku中的裸三元組
如何實現這個裸數學公式的數學公式?
裸三同
取三個小區C = {C1,C2,C3}共享一個單元U.採取三個數字 N = {N1,N2,N3}。如果C中的每個單元格都有它的候選項,那麼我們可以從U中的其他單元格中刪除所有的 ni∈N。**
我有一個方法需要一個單元(例如一個框,一行或一列)作爲參數。該單元包含9個細胞,因此我需要從盒子中一次比較3個細胞的所有組合,可能將它們放入堆棧或集合中以供進一步計算。
下一步將逐個採取這些3細胞組合,並比較他們的候選人對3個數字。這3個數字再次可以是從1到9的任何可能組合。這就是我需要的。
但我該怎麼做?我會得到多少種組合?對於單元格,我得到3 x 9 = 27個組合,然後相同的數字(N)?
你會如何解決這個經典的C#循環?沒有Lambda表達請我已經很困惑:)
代碼: 我不得不削減類爲了代表他們在這裏。
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];
}
}
}
很多感謝您的幫助,
您的問題不完整。我們需要更多地瞭解你現有的代碼(例如Unit和Cell的類定義,以及如何維護候選人等)。否則,這只是一個邏輯謎題,根本不是一個編程問題。 – 2010-06-10 22:16:08
當然喬爾。希望我的代碼片段有所幫助。謝謝 – Houman 2010-06-10 22:43:19
hehe ahhhh好,編程將是一個挑戰; o) – Houman 2010-06-12 12:07:23