如何在方法的循环仍在工作期间动态地分配值并使它们在表单上更新?我的程序中发生的情况是,启动时它会挂起,直到完成(找到数独的解决方案),然后填充文本框。下面是主要方法:
bool SolveSudoku()
{
if (!FindUnassignedLocation()) return true;
for (int num = 1; num <= 9; num++)
{
if (NoConflicts(emptyRow, emptyCol, num))
{
Grid[emptyRow, emptyCol].Text = num.ToString();
Grid[emptyRow, emptyCol].BackColor = Color.White;
Grid[emptyRow, emptyCol].ForeColor = Color.Black;
if (SolveSudoku()) return true;
Grid[emptyRow, emptyCol].Text = "";
}
}
bool checkIfFirstBackTrack = true;
do {
if (checkIfFirstBackTrack)
{
backtrackCounter++;
checkIfFirstBackTrack = false;
}
if (emptyCol == 0 && emptyRow > 0)
{
emptyCol = 8;
emptyRow--;
}
else if (emptyCol > 0)
{
emptyCol--;
}
}
while(Preset[emptyRow, emptyCol] != "");
return false;
}
}