寒假集训——搜索 B - Sudoku
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
char s[10][10]; int panduan(int row,int cew)
{
for(int i=0;i<4;i++)
{
if(s[row][i]==s[row][cew]&&i!=cew) return 0;
}
for(int j=0;j<4;j++)
{
if(s[j][cew]==s[row][cew]&&j!=row) return 0;
}
int mrow=row;
int mcew=cew;
if(row%2==1) row--;
if(cew%2==1) cew--;
for(int i=row;i<row+2;i++)
{
for(int j=cew;j<cew+2;j++)
{
if(s[i][j]==s[mrow][mcew]&&i!=mrow&&j!=mcew) return 0;
}
}
return 1;
} void dfs(int step)
{
if(step==16)
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
printf("%c",s[i][j]);
}
printf("\n");
}
} int row=step/4;
int cew=step%4;
if(s[row][cew]=='*')
{
for(int j=1;j<=4;j++)
{
s[row][cew]=j+'0';
if(panduan(row,cew)) dfs(step+1);
s[row][cew]='*';
}
}
else dfs(step+1);
} int main()
{
int cas=0;
int t;
cin>>t;
while(t--)
{
for(int i=0;i<4;i++) scanf("%s",s[i]);
printf("Case #%d:\n",++cas);
dfs(0);
}
return 0;
}
Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself. It looks like the modern Sudoku, but smaller.
Actually, Yi Sima was playing it different. First of all, he tried to generate a 4×44×4 board with every row contains 1 to 4, every column contains 1 to 4. Also he made sure that if we cut the board into four 2×22×2 pieces, every piece contains 1 to 4.
Then, he removed several numbers from the board and gave it to another guy to recover it. As other counselors are not as smart as Yi Sima, Yi Sima always made sure that the board only has one way to recover.
Actually, you are seeing this because you've passed through to the Three-Kingdom Age. You can recover the board to make Yi Sima happy and be promoted. Go and do it!!!
InputThe first line of the input gives the number of test cases, T(1≤T≤100)T(1≤T≤100). TT test cases follow. Each test case starts with an empty line followed by 4 lines. Each line consist of 4 characters. Each character represents the number in the corresponding cell (one of '1', '2', '3', '4'). '*' represents that number was removed by Yi Sima.
It's guaranteed that there will be exactly one way to recover the board.OutputFor each test case, output one line containing
Case #x:, where xx is the test case number (starting from 1). Then output 4 lines with 4 characters each. indicate the recovered board.Sample Input
3
****
2341
4123
3214
*243
*312
*421 题目:B - Sudoku
思路:
这个题目其实就是一个小一点的数独,因为很小,所以可以用枚举去搜索,完全不用担心会超时。
方法很简单就是枚举每一个*位置为1,2,3,4;然后再回溯。
具体:
再main函数里面读入,然后进入搜索函数dfs,有一个step,如果step==16就结束了
根据step可以判断出行列,然后搜这个位置,如果是*就枚举,否则就step++,进入下一个dfs
注意要写一个数独的判断函数。
寒假集训——搜索 B - Sudoku的更多相关文章
- 寒假集训——搜索 D - Cubes for Masha
#include <stdio.h> #include <stdlib.h> #include <iostream> #include <string.h&g ...
- CSU-ACM寒假集训选拔-入门题
CSU-ACM寒假集训选拔-入门题 仅选择部分有价值的题 J(2165): 时间旅行 Description 假设 Bobo 位于时间轴(数轴)上 t0 点,他要使用时间机器回到区间 (0, h] 中 ...
- HZNU-ACM寒假集训Day3小结 搜索
简单搜索 1.DFS UVA 548 树 1.可以用数组方式实现二叉树,在申请结点时仍用“动态化静态”的思想,写newnode函数 2.给定二叉树的中序遍历和后序遍历,可以构造出这棵二叉树,方法是根据 ...
- 2022寒假集训day2
day1:学习seach和回溯,初步了解. day2:深度优化搜索 T1 洛谷P157:https://www.luogu.com.cn/problem/P1157 题目描述 排列与组合是常用的数学方 ...
- 2014 UESTC暑前集训搜索专题解题报告
A.解救小Q BFS.每次到达一个状态时看是否是在传送阵的一点上,是则传送到另一点即可. 代码: #include <iostream> #include <cstdio> # ...
- 寒假训练——搜索 K - Cycle
A tournament is a directed graph without self-loops in which every pair of vertexes is connected by ...
- 寒假训练——搜索 E - Bloxorz I
Little Tom loves playing games. One day he downloads a little computer game called 'Bloxorz' which m ...
- poj3984《迷宫问题》暑假集训-搜索进阶
K - 迷宫问题 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit ...
- 中南大学2019年ACM寒假集训前期训练题集(基础题)
先写一部分,持续到更新完. A: 寒衣调 Description 男从戎,女守家.一夜,狼烟四起,男战死沙场.从此一道黄泉,两地离别.最后,女终于在等待中老去逝去.逝去的最后是换尽一生等到的相逢和团圆 ...
随机推荐
- Jenkins : 安装 master 和 slave
目录 安装 master 安装 slave 设置 master 与 slave 的通信方式 添加 slave 配置 在 salve 上安装 jre 安装并配置 Jenkins salve Jenkin ...
- MySQL之视图、触发器、事务、存储、函数、流程控制
一.视图 视图就是一个虚拟表,我们把复杂的sql语句后看到的虚拟表封装起来,给他取个名字,当我们下次使用的时候,就不用再去写复杂的sql语句,直接调用封装后的视图名字,就可以得到我们想要的表,然后就可 ...
- Java框架之Spring(二)
前一篇博客讲述了Spring的一些基础概念,下面我们来创建第一个Spring程序吧. 步骤如下: 1) 导包 2) 配置文件 附没有提示的情况 MyEclipse ->File and Edit ...
- Tomcat报failed to parse the expression [${xxx}]异常(javax.el.ELException)的解决方法
Tomcat 7 'javax.el.ELException' 的解决方式tomcat 7对EL表达式的语法要求比较严格,例如"${owner.new}"因包含关键字new就会导致 ...
- Hybris IMPEX.
1.Impex是基于java Model的一种面向对象的数据操作手段,因此写impex代码前需要理清java Model之间的依赖关系. 2.基本语法:mode type[modifier=val ...
- jQuery性能优化的一些参考建议
JQ3.1 文档下载地址:https://pan.baidu.com/s/1c2vMQdy 一.注意定义jQuery变量的时候添加var关键字 这个不仅仅是JQ,在JS中都是必须的 二.如果有多个变量 ...
- 使用idea创建一个maven工程
使用idea创建一个maven工程 一.创建maven工程 二.输入工程名 三.指定maven仓库 四.点击finish 五.修改工程结构(file->project structure) 六. ...
- 【读书笔记】iOS-自定义URL Scheme注意事项
如果两个不同的应用注册了同样的URL Scheme,那么后安装的应用会响应符合这种协议格式的URL. 如果你的应用的iPhone和iPad版是各自独立的(即不是Universal类型的),那么你就不应 ...
- Windows下 webpack4.0 的安装
这里我们通过npm来进行安装 1. 安装 webpack // 全局安装webpack npm install webpack -g 2. 通过 webpack -v 命令查看当前安装的版本 此时如果 ...
- SSM 开发 Tars
目录结构 tars生成的文件当成 controller 来调用 service ,service 调用 mapper POM 注意如果 mybatis是3.4.1 spring 是4.1.14的话, ...