当前位置:网站首页>Stroke Practice - 62 Valid Sudokus
Stroke Practice - 62 Valid Sudokus
2022-08-10 11:34:00 【qq_43403657】
62 有效的数独
1.问题描述
判断一个 9x9 的数独是否有效.只需要根据以下规则,验证已经填入的数字是否有效即可:
数字 1-9 在每一行只能出现一次.
数字 1-9 在每一列只能出现一次.
数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次.
上图是一个部分填充的有效的数独.
数独部分空格内已填入了数字,空白格用 ‘.’ 表示.
示例 1:
输入:
53…7…
6…195…
.98…6.
8…6…3
4…8.3…1
7…2…6
.6…28.
…419…5
…8…79
输出: true
示例 2:
输入:
83…7…
6…195…
.98…6.
8…6…3
4…8.3…1
7…2…6
.6…28.
…419…5
…8…79
输出: false
解释: 除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同.
但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的.
说明:
一个有效的数独(部分已被填充)不一定是可解的.
只需要根据以上规则,验证已经填入的数字是否有效即可.
给定数独序列只包含数字 1-9 和字符 ‘.’ .
给定数独永远是 9x9 形式的.
可使用以下main函数:
int main()
{
vector<vector<char> > board;
char ch;
for(int i=0; i<9; i++)
{
vector<char> aLine;
for(int j=0; j<9; j++)
{
cin>>ch;
aLine.push_back(ch);
}
board.push_back(aLine);
}
bool res=Solution().isValidSudoku(board);
cout<<(res?"true":"false")<<endl;
return 0;
}
2.输入说明
输入9行,每行9个字符,只包含数字 1-9 和字符 ‘.’
3.输出说明
输出true或false
4.范例
输入
83…7…
6…195…
.98…6.
8…6…3
4…8.3…1
7…2…6
.6…28.
…419…5
…8…79
输出
false
5.代码
#include<iostream>
#include<map>
#include<string>
#include<unordered_map>
#include<algorithm>
#include<string.h>//memset函数
using namespace std;
bool cmp(pair<char, int> a, pair<char, int>b)
{
return a.second == b.second?a.first<b.first:a.second>b.second;//The most crucial part of the question,注意写法!
}
string OrderbyChar(string s)
{
//给定一个字符串,请将字符串里的字符按照出现的频率降序排列,如果频率相同,Then according to the characterASCII码升序排列.
string res;//结果字符串
unordered_map<char, int>hash;//记录每个字符出现次数
vector<pair<char, int> > v;//使用pair记录对应关系
for (auto c : s)//遍历字符串
hash[c]++;
for (auto it : hash)
v.emplace_back(it);//directly to eachhash元素插入v中
sort(v.begin(), v.end(), cmp);
for (int i = 0; i < v.size(); i++)
{
while (v[i].second--)
res += v[i].first;
}
return res;
}
bool isValidSudoku(vector<vector<char> > board)
{
//规则1:数字 1-9 在每一行只能出现一次
//规则2:数字 1-9 在每一列只能出现一次
//规则3:数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次
int rows[9][9];//rows[i][index]代表第i行中index出现的次数
int columns[9][9];//columns[j][index]代表第j列中index出现的次数
int subboxes[3][3][9];//subboxes[i][j][index]Represents the small nine-square gridindex出现的次数
//初始化
memset(rows, 0, sizeof(rows));
memset(columns, 0, sizeof(columns));
memset(subboxes, 0, sizeof(subboxes));
//进行遍历,统计出现次数
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
char ch = board[i][j];
if (ch != '.')//处理数字
{
int index = ch - '0' - 1;//This is to prevent subscript overflow,将所有index减一
rows[i][index]++;
columns[j][index]++;
subboxes[i/3][j/3][index]++;
//Each time a number is processed, it is checked whether the number of occurrences of each number is otherwise the rule1-3
if (rows[i][index] > 1 || columns[j][index] > 1 || subboxes[i / 3][j / 3][index] > 1)
return false;
}
}
}
return true;
}
int main()
{
vector<vector<char> > board;
char ch;
for (int i = 0; i < 9; i++)
{
vector<char> aLine;
for (int j = 0; j < 9; j++)
{
cin >> ch;
aLine.push_back(ch);
}
board.push_back(aLine);
}
bool res = isValidSudoku(board);
cout << (res ? "true" : "false") << endl;
return 0;
}
边栏推荐
- OneFlow source code parsing: operator instructions executed in a virtual machine
- 第二十二章 源代码文件 REST API 参考(四)
- 内存问题难定位,那是因为你没用ASAN
- mysql appears: ERROR 1524 (HY000): Plugin '123' is not loaded
- AUTOCAD - reducing spline curve control points, the advanced CAD practice (3)
- The impact of development mode on testing
- 从源码角度分析UUID的实现原理
- OSSCore 开源解决方案介绍
- Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户
- Several small projects that I have open sourced over the years
猜你喜欢
![[Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists](/img/06/9d49fc99ab684f03740deb2abc38e2.png)
[Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists

自媒体爆款标题怎么写?手把手教你写热门标题

Alibaba最新神作!耗时182天肝出来1015页分布式全栈手册太香了

Spss-多元回归案例实操

基于UiAutomator2+PageObject模式开展APP自动化测试实战

MLX90640 红外热成像仪测温传感器 手机 APP 软件 RedEye 连接详细

微信小程序,全局变量一个地方改变了其他地方的状态也跟着改变。

Redis设计与实现

什么是幂等性?四种接口幂等性方案详解!

Pycharm终端出现PS问题、conda或activate不是内部命令问题..
随机推荐
Programmers pursue technology to consolidate basic learning route suggestions
今天面了个腾讯拿38K出来的大佬,让我见识到了基础的天花板
振弦传感器及核心VM系列振弦采集模块
2023版揽胜运动曝光,安全、舒适一个不落
STM32 encapsulation ESP8266 a key configuration function: implementations of AP mode and the STA mode switch, server and the client to create
POJ 3101 Astronomy (Mathematics)
Get started quickly and conquer three different distributed architecture calling schemes
WeChat applet, global variables change in one place and the state in other places also changes.
Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户
3款不同类型的自媒体免费工具,有效提高创作、运营效率
Weilai-software development engineer side record
微信小程序,全局变量一个地方改变了其他地方的状态也跟着改变。
【小程序 | 启航篇】一文打通任督二脉
HCIP ---- VLAN
什么是幂等性?四种接口幂等性方案详解!
基于UiAutomator2+PageObject模式开展APP自动化测试实战
Alibaba最新神作!耗时182天肝出来1015页分布式全栈手册太香了
LeetCode_443_压缩字符串
LeetCode_152_乘积最大子数组
【无标题】