一、输入:

输入一个3x3数独,字符'.'代表空
输入三个宫的域,每个宫包括三个位置,[0,0]表示0行0列

二、输出要求:

1.每个宫里最终123各出现一次,

2.数独中的行列里不出现重复字符;

输出满足条件的解

思想:先根据输入找到所有行列不重复的数独,再判断三个宫是否满足不重复;

  1 #include <iostream>
2 #include <bits/stdc++.h>
3 using namespace std;
4 int a[3][3];//存储三个宫区域
5 int cnt;//记录满足的个数
6 bool isValid(int row, int col, char val, vector<vector<char>>& board) {
7 for (int i = 0; i < 3; i++) { // 判断行里是否重复
8 if (board[row][i] == val) {
9 return false;
10 }
11 }
12 for (int j = 0; j < 3; j++) { // 判断列里是否重复
13 if (board[j][col] == val) {
14 return false;
15 }
16 }
17 return true;
18 }
19 bool func(vector<vector<char>> board){
20 for (int i = 0; i<3;i++){ //第i个宫
21 int vec[3];
22 memset(vec,0,sizeof(vec));
23 for (int j = 0; j < 3;j++){
24 if (vec[board[a[i][j]/3][a[i][j]%3]-'1']==1){
25 return false;
26 cout <<"失败"<< a[i][j] << endl;
27 }
28 vec[board[a[i][j] / 3][a[i][j] % 3]-'1'] = 1;
29 }
30 }
31 return true;
32 }
33
34 bool backtracking(vector<vector<char>>& board) {
35 int x, y;
36 for (int i = 0; i < board.size(); i++) { // 遍历行
37 for (int j = 0; j < board[0].size(); j++) { // 遍历列
38 x = i;
39 y = j;
40 if (board[i][j] != '.') continue;
41 for (char k = '1'; k <= '3'; k++) { // (i, j) 这个位置放k是否合适
42 if (isValid(i, j, k, board)) {
43 board[i][j] = k; // 放置k
44 if (backtracking(board)) {
45 return true; // 如果找到合2 适一组立刻返回
46 }
47 board[i][j] = '.'; // 回溯,撤销k
48 }
49 }
50 return false; // 9个数都试完了,都不行,那么就返回false
51 }
52 }
53 if(x==2&&y==2){ //判断是否满足宫
54 if(func(board)){
55 cout << "cnt++:" << endl;
56 cnt++;
57 for(int i=0;i<3;i++){
58 for(int j=0;j<3;j++){
59 cout<<board[i][j]<<" ";
60 }
61 cout << endl;
62 }
63 }
64 /*for(int i=0;i<3;i++){
65 for(int j=0;j<3;j++){
66 cout<<board[i][j]<<" ";
67 }
68 cout << endl;
69 }*/
70 }
71 return false;
72 //return true; // 遍历完没有返回false,说明找到了合适棋盘位置了
73 }
74
75 int main() {
76 cnt = 0;
77 vector<vector<char>>board={{0,0,0},{0,0,0},{0,0,0}};
78 for(int i=0;i<3;i++){
79 for(int j=0;j<3;j++){
80 char ch;
81 cin>>ch;
82 board[i][j]=ch;
83 //cout<<ch<<endl;
84 }
85 }
86 int x, y;
87 for (int i = 0; i < 3;i++){
88 for (int j = 0; j < 3;j++){
89 cin >> x >> y;
90 a[i][j] = 3 * x + y;
91 //cout << 3 * x + y;
92 }
93 }
94 cout << endl;
95 cout << "x" << endl;
96 backtracking(board);
97 int coun = cnt;
98 cout << "answer " << cnt << endl;
99 return 0;
100 }

测例1:

输入

..3 ... ...
0 0 1 0 1 1
0 1 0 2 1 2
2 0 2 1 2 2

输出:

cnt++:
1 2 3
2 3 1
3 1 2
cnt++:
2 1 3
1 3 2
3 2 1
answer 2

  

最新文章

  1. Mysqli基础知识
  2. 使用javabeen的好处
  3. ios8消息快捷处理——暂无输入框
  4. careercup-数学与概率 7.5
  5. linux 启动network后报错:device eth0 does not seem to be present, delaying initialization
  6. C++统计代码注释行数 &amp; 有效代码行数 &amp; 代码注释公共行 &amp; 函数个数
  7. 网络基础之IP地址与子网划分
  8. struts2 maven整合tiles3
  9. 谈谈JAVA程序的反编译
  10. 测试人员如何使用Git部署测试环境
  11. C++_day8pm_多态
  12. 基于stm32f427实现SVPWM控制永磁同步开环转动
  13. D1. Great Vova Wall (Version 1)
  14. Python中的filter()函数的用法
  15. 安装oracle10g“程序异常终止。发生内部错误。请将以下文件提供给oracle技术支持部门
  16. robot framework-databaselibaray库使用(python)
  17. mybatis generator配置,Mybatis自动生成文件配置,Mybatis自动生成实体Bean配置
  18. react实现的点击拖拽元素效果
  19. 在Ubuntu16.04集群上手工部署Kubernetes
  20. C# 多维数组 交错数组的区别,即 [ , ] 与 [ ][ ]的区别

热门文章

  1. transition过渡2D、3D效果
  2. 【第十八期】分享一个网易go面经
  3. 6U VPX i7 刀片计算机
  4. Solution -「CF 1622F」Quadratic Set
  5. Solution -「ARC 126E」Infinite Operations
  6. Solution -「多校联训」神
  7. node(s) didn‘t match node selector.
  8. C语言字符串输入输出函数(gets()函数、puts()函数、fgets()函数、fputs()函数)
  9. 来宾账户被视为安全威胁,Windows Server 2012 R2禁用Guest账户
  10. 5.string字符串