作文以记之~有效数独

0、前言

依然是一题解,具体题目可 点击此处 进行查看!具体代码以及其他内容可 点击此处 进行查看!而写这篇博客的目的在于加深自己的代码印象,具体内容如下!

1、题目描述

简单说就是判断当前数独中的数据元素是否满足数独的规则,即是不是满足行、列、3*3的九宫格中同一数字不可出现两次。
在这里插入图片描述

2、解题思路

2.1 利用二维数组

2.1.1 思路

在这里插入图片描述

2.1.2 程序代码

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

void showboard(vector<vector<char>>& board)
{
	cout << "数独元素('.'代表没填数字):" << endl;
	for (int i = 0; i < 9; i++)
	{
		for (int j = 0; j < 9; j++)
		{
			cout << board[i][j] << " ";
		}
		cout << endl;
	}
	cout << endl;
}

/*方法1 -- 利用2维数组*/
bool isValidSudoku(vector<vector<char>>& board) {
	int row[9][10] = { 0 };
	int col[9][10] = { 0 };
	int box[9][10] = { 0 };
	for (int i = 0; i < 9; i++)
	{
		for (int j = 0; j < 9; j++)
		{
			if (board[i][j] == '.') continue;
			int tmp = board[i][j] - '0';
			if ((row[i][tmp]) != 0 || (col[j][tmp]) != 0 || (box[j / 3 + (i / 3) * 3][tmp]) != 0)
				return false;
			row[i][tmp] = 1;
			col[j][tmp] = 1;
			box[j / 3 + (i / 3) * 3][tmp] = 1;
		}
	}
	return true;
}

void test()
{
	// 定义数独
	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' },
	{ '.', '.', '.', '.', '8', '.', '.', '7', '9' } };

	showboard(board);
	cout << "\n判断当前数独是否有效:" << boolalpha << isValidSudoku(board) << endl << endl;
}

int main()
{
	test();
	system("pause");
	return 0;
}

2.1.3 运行结果

在这里插入图片描述

2.2 利用位运算

2.2.1 思路

相较于上一种方法,此处的方法只不过是将2维数组变为了一维数组,然后在数组的数据中通过左移对应位置来表示数独中对应数据所存在的位置。具体可看下述代码!

2.2.2 程序代码

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

void showboard(vector<vector<char>>& board)
{
	cout << "数独元素('.'代表没填数字):" << endl;
	for (int i = 0; i < 9; i++)
	{
		for (int j = 0; j < 9; j++)
		{
			cout << board[i][j] << " ";
		}
		cout << endl;
	}
	cout << endl;
}

/*方法2 -- 利用位运算*/
bool isValidSudoku(vector<vector<char>>& board) {
	int row[9] = { 0 };
	int col[9] = { 0 };
	int box[9] = { 0 };
	for (int i = 0; i < 9; i++)
	{
		for (int j = 0; j < 9; j++)
		{
			if (board[i][j] == '.') continue;
			int tmp = 1 << (board[i][j] - '0');
			if ((row[i] & tmp) != 0 || (col[j] & tmp) != 0 || (box[j / 3 + (i / 3) * 3] & tmp) != 0)
				return false;
			row[i] |= tmp;
			col[j] |= tmp;
			box[j / 3 + (i / 3) * 3] |= tmp;
		}
	}
	return true;
}

void test()
{
	// 定义数独
	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' },
	{ '.', '.', '.', '.', '8', '.', '.', '7', '9' } };

	showboard(board);
	cout << "\n判断当前数独是否有效:" << boolalpha << isValidSudoku(board) << endl << endl;
}

int main()
{
	test();
	system("pause");
	return 0;
}

2.2.3 运行结果

在这里插入图片描述

3、总结

本题中通过两种方法解决问题,我觉得值得在意的就是二维数组与一维数组之间的互通,利用位运算是一个好办法!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值