我在一个文件中读到一个数组.它正在读取每个char,问题出现在它还在文本文件中读取换行符.
这是一个数独板,这是我在char中读取的代码:
bool loadBoard(Square board[BOARD_SIZE][BOARD_SIZE])
{
ifstream ins;
if(openFile(ins)){
char c;
while(!ins.eof()){
for (int index1 = 0; index1 < BOARD_SIZE; index1++)
for (int index2 = 0; index2 < BOARD_SIZE; index2++){
c=ins.get();
if(isdigit(c)){
board[index1][index2].number=(int)(c-'0');
board[index1][index2].permanent=true;
}
}
}
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
就像我说的那样,当它遇到\n时,它会读取文件,显示在屏幕上,但是顺序不正确
我希望我更加关注Uni的数学课程.:)
如何为裸三元组实现这个数学公式?
Naked Triples
取三个单元格C = {c1,c2,c3}共享一个单位U.取三个数字N = {n1,n2,n3}.如果C中的每个单元都有候选ci⊆N那么我们就可以从U中的其他单元中删除所有的ni∈N.**
我有一个方法,取一个单位(例如一个Box,一行或一列)作为参数.该单元包含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 { …Run Code Online (Sandbox Code Playgroud) 我最近一直致力于回溯数独求解算法,目前我想询问我应该如何将我的solve()方法从void更改为boolean.
我正在使用一个非常简单的回溯算法,它目前工作正常,但我宁愿有一个布尔值而不是一个空格,因为有一个printstack不是很好...
谢谢!
public class Backtracking{
static int backtrack = 0;
//check if valid in row
protected static boolean validInRow(int row, int value)
{
for( int col = 0; col < 9; col++ )
if( board[row][col] == value )
return false ;
return true ;
}
//check if valid in column
protected static boolean validInCol(int col, int value)
{
for( int row = 0; row < 9; row++ )
if( board[row][col] == value )
return false ;
return true ; …Run Code Online (Sandbox Code Playgroud) 我正在为我正在制作的这个数独求解器的GUI工作.我设法打印出了没有问题的电路板.但是,我想知道如何将3x3区域与某种较粗或有色线区分开来.
基本上类似于下图的东西.

下面是我已经实现的代码.谢谢!
Board = new JPanel(new GridLayout(9, 9));
for(int i= 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
board[i][j] = new JLabel();
board[i][j].setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
Font font = new Font("Arial", Font.PLAIN, 20);
board[i][j].setFont(font);
board[i][j].setForeground(Color.WHITE);
board[i][j].setBackground(Color.WHITE);
board[i][j].setOpaque(true);
board[i][j].setHorizontalAlignment(JTextField.CENTER);
Board.add(board[i][j]);
}
}
Run Code Online (Sandbox Code Playgroud) 我做功课一道数独题求解,但我遇到了一些困难.该代码现在周期过去的解决方案,虽然它确实达到它容易难题,并为困难的难题,它卡住几个9的没有明显的原因.我将不胜感激任何帮助.(check_cell确定放置是否有效.)
一些代码:
def solve_helper(self, row, col):
# Try placing a number in each column of current row
board = self.the_board
if board[row][col] != 0:
?????
elif board[row][col] == 0:
for i in range(1,10):
print("Setting value at i with ") + str (i) + (" located at " ) + str(row) + str(col)
self.set_cell(row, col, i)
self.guesses = self.guesses + 1
if self.check_cell(row, col):
if self.solve_helper(row, col): return True
else:
self.set_cell(row, col, 0)
else:
return self.mover(row,col)
return False
def …Run Code Online (Sandbox Code Playgroud) 数独的“简单/朴素回溯强力算法”、“直接深度优先搜索”是众所周知并已实现的。
并且似乎不存在不同的实现。(当我第一次写这个问题时..我想说我们可以完全标准化它,但措辞很糟糕..)
我认为这个人很好地描述了该算法:/sf/answers/145284891/
编辑:所以让我用伪代码更详细地说明它......
var field[9][9]
set the givens in 'field'
if brute (first empty grid) = true then
output solution
else
output no solution
end if
function brute (cx, cy)
for n = 1 to 9
if (n doesn't present in row cy) and (n doesn't present in column cx) and (n doesn't present in block (cx div 3, cy div 3)) then
let field[cx][cy] = n
if (cx, cy) this is the last empty grid …Run Code Online (Sandbox Code Playgroud) 我正在构思一个名为multi-sudoku的数独变种的解算器,其中多个板重叠如下:

如果我正确理解游戏,您必须以这样的方式解决每个网格,即任何两个或更多网格之间的重叠是每个网格解决方案的一部分.
我不确定我应该怎么想这个.任何人都有任何提示/概念线索?此外,如果想到人工智能中的任何主题,我也想听听.
theory artificial-intelligence solver sudoku constraint-programming
我正在尝试编写一个可以解决数独的算法。现在,我的代码可以工作,直到 supplyGrid 没有数字为止。当它发生时,它应该返回并尝试另一个号码,对吗?老实说,我不知道如何实现这一目标。
var grid = [
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, …Run Code Online (Sandbox Code Playgroud)