寒假集训——搜索 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 男从戎,女守家.一夜,狼烟四起,男战死沙场.从此一道黄泉,两地离别.最后,女终于在等待中老去逝去.逝去的最后是换尽一生等到的相逢和团圆 ...
随机推荐
- ArcGIS紧凑型切片读取与应用1-解析(附源码)
1.前言 ArcGIS 发布的切片服务分为紧凑型切片和传统的分散型切片以及最新ArcGIS por新增的矢量切片.传统的分散型切片面临的问题是文件个数太多,部署拷贝过程十分的耗时,紧凑型切片是对分散型 ...
- [AHOI2008] 紧急集合
Description 欢乐岛上有个非常好玩的游戏,叫做"紧急集合".在岛上分散有N个等待点,有N-1条道路连接着它们,每一条道路都连接某两个等待点,且通过这些道路可以走遍所有的等 ...
- java的四个基本特征
现实生活中的事物被抽象成对象,把具有相同属性和行为的对象被抽象成类,再从具有相同属性和行为的类中抽象出父类. 封装 隐藏对象的属性和实现细节,仅仅对外公开接口. 封装的有优点: 1.便于使用者正确.方 ...
- windows下mysql-5.7.18安装
在官网下载了mysql压缩包,按照官网:https://dev.mysql.com/doc/refman/5.7/en/windows-installation.html的教程安装,但是出错,后面根据 ...
- Clock Pictures(kmp + Contest2075 - 湖南多校对抗赛(2015.04.26))
Problem H: Clock Pictures Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 73 Solved: 18[Submit][Stat ...
- DOM之城市二级联动
1.HTML内容 <select id="province"> <option>请选择</option> <option>山东省&l ...
- Ajax实现的城市二级联动二
上一篇联动一种只是城市用ajax请求获取并渲染,这里将省份也用ajax请求并渲染 1.HTML <select id="province"> <option> ...
- homebrew 安装 formula 的不同历史版本——以安装 node 为例
homebrew 安装 formula 的不同历史版本--以安装 node 为例 系统环境 macOS Mojave 10.14 Homebrew 1.8.0 Homebrew/homebrew-co ...
- 遇到了ImportError: libmysqlclient_r.so.16: cannot open shared object file: No such file or directory
解决方法如下: 1. 通过命令查找libmysqlclient_r.so.16 在什么地方,一般是在/usr/lib64/mysql/下面 2. 做一个链接到/usr/lib64 下: ln -s / ...
- 安卓开发_浅谈Notification(通知栏)
Notification通知栏是显示在手机状态的消息,代表一种全局效果的通知 快速创建一个Notification的步骤简单可以分为以下四步: 第一步:通过getSystemService()方法得到 ...