- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试准备数独项目。我现在有行和列控件。
如果我创建数组,则9 * 9程序在运行时崩溃,并且窗体窗口冻结。我通过单击“停止调试”按钮来停止它。但它与一小部分阵列完美配合。例如3 * 3。如果我创建6 * 6,有时效果更好,有时效果不好。但是9 * 9到目前为止我还没有成功。
有什么问题,您对这个问题有什么想法吗?
我是编程新手。如果您用一些例子来回答,我会很高兴。
提前致谢..
只是代码的一部分。我用英语更改了变量。
public bool rowControl(int[,] sudoku, int row, int value)
{
for (int i = 0; i < 9; i++)
{
if (sudoku[row, i] == value)
{
return false;
}
}
return true;
}
public bool columnControl(int[,] sudoku, int column, int value)
{
for (int i = 0; i < 9; i++)
{
if (sudoku[i, column == value)
{
return false;
}
}
return true;
}
private void button1_Click(object sender, EventArgs e)
{
for (int f = 0; f < 9; f++)
{
for (int i = 0; i < 9; i++)
{
while (rowControl(sudokuArr, f, random) == false || columnControl(sudokuArr, i, random) == false)
{
random= r.Next(1, 10);
}
sudokuArr[f, i] = random;
}
}
}
最佳答案
好的,我不知道您原来的问题是什么,但是修订后的问题是递归调用BSudoku
直到出现StackOverflow错误。此代码工作繁琐:
namespace Sudoku
{
public partial class Form1 : Form
{
static TextBox[,] graphicSudokuBoard;
int[,] field = new int[9, 9];
public void CreateSudokuBoard()
{
int x = 10, y = 10;
int tabIndex = 0, textBoxHeight = 0;
graphicSudokuBoard = new TextBox[9, 9];
for (int row = 0; row < 9; row++)
{
x = 10;
for (int col = 0; col < 9; col++)
{
TextBox textBox = new TextBox();
textBox.BorderStyle = BorderStyle.FixedSingle;
textBox.Font = new Font(textBox.Font.FontFamily, 24F, FontStyle.Bold);
textBox.MaxLength = 1;
textBox.TextAlign = HorizontalAlignment.Center;
textBox.Width = textBox.Height;
textBox.TabIndex = tabIndex++;
textBox.Location = new Point(x, y);
this.Controls.Add(textBox);
graphicSudokuBoard[row, col] = textBox;
textBoxHeight = textBox.Height;
x += textBox.Width - 1;
if (col % 3 == 2)
x++;
}
y += textBoxHeight - 1;
if (row % 3 == 2)
y++;
}
}
bool chkRow(int row, int value, int[,] arr)
{
for (int i = 0; i < 9; i++)
{
if (arr[row, i] == value)
{
return false;
}
}
return true;
}
bool chkColumn(int column, int value, int[,] arr)
{
for (int i = 0; i < 9; i++)
{
if (arr[i, column] == value)
{
return false;
}
}
return true;
}
bool chkSquare(int row, int column, int value, int[,] arr)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (arr[row - (row % 3) + i, column - (column % 3) + j] == value)
{
return false;
}
}
}
return true;
}
bool chkAll(int row, int column, int value, ref int[,] arr)
{
if (!chkRow(row, value, arr))
return false;
if (!chkColumn(column, value, arr))
return false;
if (!chkSquare(row, column, value, arr))
return false;
return true;
}
int[,] BSudoku(int value, int px, int py, int[,] field)
{
int[,] sudokuCells = new int[9, 9];
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
sudokuCells[j, i] = field[j, i];
}
}
for (int x = px; x < 9; x++)
{
for (int y = py; y < 9; y++)
{
if (sudokuCells[x, y] == 0)
{
for (int val = 1; val <= 9; val++)
{
if (val == 9 && !chkAll(x, y, val, ref sudokuCells))
{
sudokuCells[x, y] = 0;
if (x > 0 || y > 0)
{
if (x > 0 && y == 0)
{
x--;
y = 8;
}
else y--;
}
while(sudokuCells[x,y] == 9)
{
sudokuCells[x, y] = 0;
if (x > 0 || y > 0)
{
if (x > 0 && y == 0)
{
x--;
y = 8;
}
else y--;
}
}
sudokuCells[x, y]++;
}
if (chkAll(x, y, val, ref sudokuCells))
{
sudokuCells[x, y] = val;
graphicSudokuBoard[x, y].Text = sudokuCells[x, y].ToString();
break;
}
}
}
}
}
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
graphicSudokuBoard[j, i].Text = sudokuCells[j, i].ToString();
}
}
//MessageBox.Show("Done!");
return sudokuCells;
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
string s = graphicSudokuBoard[i, j].Text;
if (s == "") s = "0";
field[i, j] = Convert.ToInt32(s);
}
}
BSudoku(0, 0, 0, field);
}
public Form1()
{
InitializeComponent();
CreateSudokuBoard();
}
}
}
chk___
函数不能是静态的。一旦返回,循环变量就永远不会从0开始。我不记得我修复的所有内容,但是作为警告,大多数SO人都不会花时间来检查您的代码。上面的代码解决了StackOverflow问题以及许多其他问题。如果为它提供足够的值,它将返回正确的函数,但是仍然存在一些问题,它允许在行,列和正方形中包含多个值。另一件事,您可能想使用
ref
关键字将数组作为引用传递;这样可以大大加快代码的速度。作为最后的警告,因为这是蛮力求解器,所以您需要为它添加几个值,以使其运行得相当快。当为我填满三行时,它运行很快,但结果不正确。您应该能够解决该问题,而我没有时间弄清楚现在正在发生什么。
关于debugging - Sudoku Project在运行时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26619325/
FogBugz 有没有办法创建一个过滤器,它是: [All] [open] [cases] assigned to [Developer A] with the exception of 1 or m
我是 java hibernate 的新手,我无法理解 Projections.property 和 Projections.groupProperty 之间的区别,两者都给出相同的结果。请解释其中的
让我们再试一次。我发布这个是为了回答 2 个问题 MS Project 2007 是否需要 SharePoint(我希望没有)? 做 你喜欢 MS Project 开发团队 - 它是有用的还是 疼痛?
我们的项目结构如下: sharedlib (lib-project, containing classes, that are useful in many apps) -> main-project
我正在尝试获取有关如何将 MS Project 2010 连接到 MS Project Server 2010 的教程或分步说明。 我已经在我的服务器上安装了 Server 2008 R2(64 位)
Projections.distinct(Projections.count("objectId")) 和 Projections.countDistinct("objectId") 谁能告诉我它们之
可以在项目 “project-a” 中创建一个主题“my-topic-project-a”,这样它就可以公开可见(已完成通过将角色“pub/sub subscriber” 设置为“allUsers”)
我创建了一个简单的 Kafka 消费者,它返回 Flux对象(收到的消息),我正在尝试使用 StepVerifier 对其进行测试. 在我的测试中,我做了这样的事情: Flux flux = cons
我有一个项目,最初是在 Eclipse 中,然后我设法将它转换为 gradle,并且在 Android Studio 中一切正常。 但后来我没有在这个项目上工作一段时间,几个月后当我回到项目时,我很惊
我正在研究 CQRS/ES 架构。我们将多个异步投影并行运行到读取存储中,因为某些投影可能比其他投影慢得多,并且我们希望与写入端保持更多同步以实现更快的投影。 我试图了解有关如何生成读取模型以及这可能
第一次尝试使用 Java 持久性注释创建多对多关系。 场景:Java 类Project 包含子项目,它只是Projects 的List。没有反向(没有 super 项目)成员。所以我认为多对多关系对于
我有现有的 C 代码和现有的 Makefile,我想将其包装到 Eclipse C 项目中(使用 Eclipse 3.4 Ganymede)。代码组织如下: 主目录:/Project/Software
我有一个 Eclipse 项目,不久前我设法在 Android Studio 中工作。它使用 TouchDB 库/项目,我现在想升级到他们最新的产品 couchbase-lite-android,看起
我将项目定义为包含主干、分支、标签子目录的 SVN 目录。 在确定何时将项目拆分为两个或将多个项目合并为一个时,您使用什么标准? - 每个“项目”一个应用程序,具有用于公共(public)源和资源的共
尝试在 Visual Studio 2008 中构建解决方案时遇到此错误。这是什么意思? 最佳答案 这可能意味着文件 bin\project.dll被另一个进程(可能是另一个 Visual Studi
此宏将隐藏/关闭 VBE 主窗口: Sub VBEMainWindowHide() 'close VBE window: Application.VBE.Window.Visible
我正在寻求开发户外应用程序,但不确定 tango 平板电脑是否可以在户外使用。那里的其他深度设备往往在室外效果不佳,因为它们依赖于从设备转换的红外光,然后在它从场景中的物体反射回来后进行观察。我一直在
在标准 .csproj您可以进入属性并设置默认命名空间。如何在 .xproj 中实现这一点项目使用 project.json ? 最佳答案 使用 ASP.NET Core 1.0.1,您可以在 pro
当 Redmine 上注册的项目超过 5 个时,主页“最新项目”框中列出的项目按创建日期降序排序(最近创建的优先),将旧项目(可能更新频率更高)排除在列表之外. 有没有办法按事件从最高到最低列出前 5
我开始学习android开发,但我不知道如何将库添加到项目中。我使用安卓工作室。我创建了新项目,但项目结构中没有项目设置。 最佳答案 在“项目”窗口中右键单击您的包名称,然后选择“打开模块设置”。这应
我是一名优秀的程序员,十分优秀!