ProDeveloperTutorial.com

教程和编程解决方案
菜单
  • Shell脚本
  • 系统设计
  • Linux系统编程
  • 4g LTE
  • 编码问题
  • C
  • C ++
  • DSA
  • GIT

检查给定的板是否是有效的Sudoku,C ++中的解决方案说明

前开发者教程 十月21,2018

以下是数独有效的规则:

  1. 每行必须包含数字1-9,不能重复。
  2. 每列必须包含数字1-9,且不能重复。
  3. 9个中的每个×网格的3个子框必须包含数字1-9,且不能重复。

 

Example 1:
Input:
[
  ["5","3",".",".","7",".",".",".","."],
  ["6",".",".","1","9","5",".",".","."],
  [".","9","8",".",".",".",".","6","."],
  ["8",".",".",".","6",".",".",".","3"],
  ["4",".",".","8",".","3",".",".","1"],
  ["7",".",".",".","2",".",".",".","6"],
  [".","6",".",".",".",".","2","8","."],
  [".",".",".","4","1","9",".",".","5"],
  [".",".",".",".","8",".",".","7","9"]
]
Output: true

解决方案说明:

解决方案非常简单,如下所述:

在这里,我们必须检查每个字段的3个条件。

  1. 每行应有1到9之间的数字,不能重复。
  2. 每列应有1到9之间的数字,不能重复。
  3. 每个正方形的数字应从1到9,且不得重复。

 

为此,我们采用3个Boolean 9 x 9布尔值矩阵来存储Row,Column和Square的值,并将所有值初始化为false。

 

现在,我们遍历电路板,并更新如下所示的布尔矩阵:

  1. 对于任何数值“ n”,我们检查row [i],column [j],square [k]的值是否为true,则表示数字“ n”已出现在前面。因此,我们返回false。

其他

  1. 将row [i] [n],column [j] [n],square [k] [n]设置为true。
  2. 同样,我们遍历整个开发板,如果没有重复,则返回true。

重要:

我们可以从外部for循环获取行。

我们可以从内部for循环获取列。

 

但是,如何获得平方的值?

以下是获取特定数字所在的平方值的计算。

1.	i controls which group of squares we fall in
2.	j controls which square within the group
See below for reference


Rows 0 - 2 -> Squares 0 - 2 | Col 0 - 2 -> Square 0, Col 3 - 5 -> Square 1, Col 6 - 8 -> Square 2

Rows 3 - 5 -> Squares 3 - 5 | Col 0 - 2 -> Square 3, Col 3 - 5 -> Square 4, Col 6 - 8 -> Square 5

Rows 6 - 8 -> Squares 6 - 8 | Col 0 - 2 -> Square 6, Col 3 - 5 -> Square 7, Col 6 - 8 -> Square 8

因此:

i/3*3 will give the segment of the block.
j/3 will give the column inside the segment of the block.

C ++解决方案

#include<iostream>
#include<vector>
using namespace std;


bool isValidSudoku(vector<vector<char> > &board)
{
	int row[9][9] = {0};
	int column[9][9] = {0};
	int square[9][9] = {0};

	for(int i = 0; i < board.size(); ++ i)
		for(int j = 0; j < board[i].size(); ++ j)
			if(board[i][j] != '.')
			{
				//get the integer value of the number by doing board[i][j] - '0'
				//need -1 because the index of array is 0~8 by subtracting 1 from the above int value
				int num = board[i][j] - '0' - 1; 
				int k = i / 3 * 3 + j / 3;
				if(row[i][num] || column[j][num] || square[k][num])
					return false;
				row[i][num] = column[j][num] = square[k][num] = 1;
			}

	return true;
}

int main() 
{ 
   vector<vector<char> > board = { { '5', '3', '.', '.', '7', '.', '.', '.', '.' }, 
                         { '6', '.', '.', '1', '9', '5', '.', '.', '.' }, 
                         { '.', '9', '8', '.', '.', '.', '.', '6', '.' }, 
                         { '8', '.', '.', '.', '6', '.', '.', '.', '3' }, 
                         { '4', '.', '.', '8', '.', '3', '.', '.', '1' }, 
                         { '7', '.', '.', '.', '2', '.', '.', '.', '6' }, 
                         { '.', '6', '.', '.', '.', '.', '2', '8', '.' }, 
                         { '.', '.', '.', '4', '1', '9', '.', '.', '5' }, 
                         { '.', '.', '.', '.', '1', '.', '.', '7', '9' } }; 
  
    bool result = isValidSudoku(board);
    if (result)
    	cout<<"The sudoku is valid"<<endl;
    其他
    	cout<<"The sudoku is not valid"<<endl;

    return 0; 
}

输出:

The sudoku is not valid

 

 

该网站上可用的教程列表:

C编程20+章C ++编程80+章
100多个编码问题数据结构和算法85+章
系统设计20+章Shell脚本编写12章
4g LTE 60+章节最常见的编码问题
5G NR 50+章Linux系统编程20+章
分享
电子邮件
鸣叫
领英
Reddit
绊倒
Pinterest的
上一篇文章
下一篇

关于作者

前开发者教程

每天我们都会讨论竞争性编程问题,请加入我们的网站:   电报频道

ProDeveloperTutorial.com

教程和编程解决方案
版权© 2020 ProDeveloperTutorial.com
从以下课程获得热门课程: 教育性的

<span id="B3PbFPj" class="Bm9j0Ux"><mark id="mRykvJS"></mark></span>

<textarea class="xLAkYRD"><address id="dVuByg2"></address></textarea>
  • <wbr id="RweVgTO" class="RtpFiEO"><button id="aMXHUu1" class="aXNEj6H"><summary id="ribGmI7" class="rHI3llX"></summary></button></wbr>


            <hgroup id="t1bHcNN" class="tu08hzl"><tfoot id="ghaZsuf" class="gcUS3Gr"></tfoot></hgroup>

              <audio class="n7tvjKv"><pre id="s8w5Ndk" class="ssbr6wG"></pre></audio>


              <bdi id="ScVsWMu" class="S4QXGP9"></bdi>