寒假集训——搜索 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 男从戎,女守家.一夜,狼烟四起,男战死沙场.从此一道黄泉,两地离别.最后,女终于在等待中老去逝去.逝去的最后是换尽一生等到的相逢和团圆 ...
随机推荐
- 跨域请求中预检请求options之坑
一.前言 因为跨域请求,浏览器可能(后面讲)会发送一次options请求,如果处理不好,跨域还是会gg的. 之前很少涉及跨域,涉及也是简单请求(下面阮老师文章中区别热简单请求和复杂请求),所以基本不会 ...
- vue里碰到 $refs 的问题
记录困惑自己一个简单的问题...(瞬间感觉官方文档的强大) 在自己做的一个项目中,遇到一个列表页,根据id能进入详情页(动态匹配路由),详情页是单独的一个组件,在这个详情的组件里,我想获取内容给你区域 ...
- springboot+cloud 学习(六)Docker
注:由于本人用的是window10系统,docker的版本是Docker Community Edition(Docker CE ). 什么是Docker 按官网的话说,Docker是一个世界领先的容 ...
- JavaWeb学习 (八)————HttpServletResponse对象(二)
一.HttpServletResponse常见应用——生成验证码 1.1.生成随机图片用作验证码 生成图片主要用到了一个BufferedImage类,
- “未能加载文件或程序集“XXX”或它的某一个依赖项。试图加载格式不正确的程序”问题的解决
发布到win7 64位旗舰版iis上时,报:“未能加载文件或程序集“BC.Common”或它的某一个依赖项.试图加载格式不正确的程序”. 该DLL的本地复制没有设置为true(在项目引用里找到该引用, ...
- 数据结构-Python3.7<三>
上一篇:流程控制-Python<二> 因为列表具有pop.append.insert方法,因此列表可以当作堆.栈使用.由于性能问题,不建议当作堆.(堆:队列优先,先进先出(FIFO—fir ...
- Spring中四种实例化bean的方式
本文主要介绍四种实例化bean的方式(注入方式) 或者叫依赖对象实例化的四种方式.上面的程序,创建bean 对象,用的是什么方法 ,用的是构造函数的方式 (Spring 可以在构造函数私有化的情况下把 ...
- Cylinder Candy(积分)
Cylinder Candy Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge Edward the confectioner is ...
- 最优-scroll事件的监听实现
1. 背景和目标 前端在监听scroll这类高频率触发事件时,常常需要一个监听函数来实现监听和回调处理.传统写法上利用setInterval或setTimeout来实现. 为了减小 CPU 开支,往往 ...
- 洛谷P3722 [AH2017/HNOI2017]影魔(线段树)
题意 题目链接 Sol 题解好神仙啊qwq. 一般看到这种考虑最大值的贡献的题目不难想到单调数据结构 对于本题而言,我们可以预处理出每个位置左边第一个比他大的位置\(l_i\)以及右边第一个比他大的位 ...