我需要一个衬垫(或靠近它)来验证给定的9个元素的数组不包含重复的数字1,2,3,...,9.重复零不计数(它们代表空单元格).
到目前为止我得到的最好的是:
var a = new int[9] {1,2,3,4,5,6,7,8,9};
var itIsOk = a.Join(a, i => i, j => j, (x, y) => x)
    .GroupBy(y => y).Where(g => g.Key > 0 && g.Count() > 1).Count() == 0;
如果你不想解决我的问题:),你至少可以告诉上述算法是否正常工作吗?
并且,是的,已经读过这个.
我在一个文件中读到一个数组.它正在读取每个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;
}
就像我说的那样,当它遇到\n时,它会读取文件,显示在屏幕上,但是顺序不正确
我最近一直致力于回溯数独求解算法,目前我想询问我应该如何将我的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 ; …我想在python中创建一个数独检查器:
ill_formed = [[5,3,4,6,7,8,9,1,2],
              [6,7,2,1,9,5,3,4,8],
              [1,9,8,3,4,2,5,6,7],
              [8,5,9,7,6,1,4,2,3],
              [4,2,6,8,5,3,7,9],  # <---
              [7,1,3,9,2,4,8,5,6],
              [9,6,1,5,3,7,2,8,4],
              [2,8,7,4,1,9,6,3,5],
              [3,4,5,2,8,6,1,7,9]]
easy = [[2,9,0,0,0,0,0,7,0],
       [3,0,6,0,0,8,4,0,0],
       [8,0,0,0,4,0,0,0,2],
       [0,2,0,0,3,1,0,0,7],
       [0,0,0,0,8,0,0,0,0],
       [1,0,0,9,5,0,0,6,0],
       [7,0,0,0,9,0,0,0,1],
       [0,0,1,2,0,0,3,0,6],
       [0,3,0,0,0,0,0,5,9]]
我期待这样的输入 - 一个包含9个列表的列表.零表示用户尚未填写的数字.它们可以连续,列或3x3多次出现.
def check_sudoku(grid):
if len(grid) == 9:
    numsinrow = 0
    for i in range(9):
        if len(grid[i]) == 9:
            numsinrow += 1
    if numsinrow == 9:
        for i in range(9):
            rowoccurence = [0,0,0,0,0,0,0,0,0,0]
            for j in range(9):
                rowoccurence[grid[i][j]] += 1
                temprow = rowoccurence[1:10]
                if temprow == [1,1,1,1,1,1,1,1,1]:
                    return True
                else:
                    return False
    else:
        return …数独的“简单/朴素回溯强力算法”、“直接深度优先搜索”是众所周知并已实现的。
并且似乎不存在不同的实现。(当我第一次写这个问题时..我想说我们可以完全标准化它,但措辞很糟糕..)
我认为这个人很好地描述了该算法:/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 …我正在构思一个名为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, …我需要检查用户插入的数字是否已经在列,行或"块"中(仍然在最后一部分工作).由于某些原因这些检查不起作用,我不明白为什么?
我在shell中编写了相同的代码,它运行得很好.
我的代码:
def is_valid_move(board,row, column, digit):
    if digit in board[row]:
        print "Row already contains", digit
        return (False)
    else:
        return (True)
    for i in range(9):
        if digit in board[i][row]:
            print "Colum already contains", digit
            return (False)
            break
        else:
            return (True)
board = [[3,7,0,0,5,0,0,0,0],
         [0,6,0,0,3,0,2,0,0],
         [0,2,9,4,0,0,0,7,8],
         [0,0,4,1,7,0,0,8,0],
         [0,0,6,3,0,5,9,0,0],
         [0,5,0,0,8,4,1,0,0],
         [7,1,0,0,0,8,5,6,0],
         [0,0,5,0,1,0,0,2,0],
         [0,0,0,0,9,0,0,1,3]]
a=is_valid_move(board,1, 2, 9)
print a
输出我得到:
True
任何想法如何检查数字是否已经在框中?
谢谢!