The task is to expand latin rectangle to latin square. Latin square is a grid where in rows and columns there are no repeating numbers (or symbols), and latin rectangle is some part of it.
My program generates nxm latin rectangle and expands it to NxN latin square (dimensions are provided by user). There is a problem with methods responsible for adding next number to the rectangle:
private int AddColumnNumber(int[,] rec, int rw, int cl)
{
// variable init
var list = new List<int>();
int[] L_ia = new int[N];
// iterate through grid and get how many times each number appears
for (int i = 0; i < rec.GetLength(0); i++)
{
for (int j = 0; j < rec.GetLength(1); j++)
{
L_ia[rec[i, j] - 1]++;
}
}
// add all numbers that appear in row to the list
for (int i = 0; i < rec.GetLength(0); i++)
{
list.Add(rec[i, cl]);
}
// create list of available numbers (those that didn't appear already in row)
var available = Enumerable.Range(1, N).Except(list);
// find least appearing numbers
int t = Int32.MaxValue;
for (int i = 0; i < N; i++)
{
if (L_ia[i] < t)
{
t = L_ia[i];
}
}
// find which one of available numbers are least popular in grid
available = available.Where(item => L_ia[item - 1] == t);
var result = available.First();
// CheckRow - boolean method that checks if given number (result) exists in certain row (rw) of given rectangle (rec)
// CheckColumn - boolean method that checks if given number (result) exists in certain column (cl) of given rectangle (rec)
// if it isn't, then it's all clear and I can use that value
if ((CheckRow(result, rw, rec) && CheckColumn(result, cl, rec)))
{
return result;
}
// if not, ignore the value
else
{
available = available.Where(item => item != result);
// here codes go back to the point of choosing another first item from available list
}
if(available.Count() == 0)
{
return 0;
}
}
Method for expanding rows is similar
during program execution it sometimes go to the loop - when expanding and trying to get another value it sometimes blocks when it gets (for instance) two same values in new column. I need help resolving this problem or another way of expanding rows and columns
Aucun commentaire:
Enregistrer un commentaire