寒假集训——搜索 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 男从戎,女守家.一夜,狼烟四起,男战死沙场.从此一道黄泉,两地离别.最后,女终于在等待中老去逝去.逝去的最后是换尽一生等到的相逢和团圆 ...
随机推荐
- netty源码解解析(4.0)-10 ChannelPipleline的默认实现--事件传递及处理
事件触发.传递.处理是DefaultChannelPipleline实现的另一个核心能力.在前面在章节中粗略地讲过了事件的处理流程,本章将会详细地分析其中的所有关键细节.这些关键点包括: 事件触发接口 ...
- FFmpeg流媒体处理-收流与推流
本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10623968.html 1. 简介 流媒体是使用了流式传输的多媒体应用技术.如下是维基百 ...
- Linux C++开发学习(一)
一.简单输出 简单的小程序 1.安装g++ sudo install g++ 2.编写c++程序,helloworld.cpp #include<iostream> using names ...
- JPA与EJB3的关系
JPA是基于Java持久化的解决方案,主要是为了解决ORM框架的差异,它的出现在某种程度上能够解决目前ORM框架之间不能够兼容的问题,对开发人员来说,能够更好的在JPA规范下进行系统开发. JPA全称 ...
- 性能监控(6)–JAVA下的jinfo命令
jinfo可以用来查看正在运行的java应用程序的扩展参数,设置支持在运行时,修改部分参数. Jinfo的语法为: Usage: jinfo [option] <pid> (to conn ...
- fast-spring-boot快速开发项目
Introduction fast-spring-boot 集成Spring Boot 2.1,Mybatis,Mybatis Plus,Druid,FastJson,Redis,Rabbit MQ, ...
- List排序Collections.sort 重写compare
static List<Integer> intList = Arrays.asList(2,5,7, 3, 1); public static void main(String[] ar ...
- 最新版本elasticsearch本地搭建入门篇
最新版本elasticsearch本地搭建入门篇 项目介绍 最近工作用到elasticsearch,主要是用于网站搜索,和应用搜索. 工欲善其事,必先利其器. 自己开始关注elasticsearch, ...
- 设计模式之享元模式(Flyweight)
享元模式顾名思义就是羽量级模式或者蝇级模式,形容体量小的应用,该模式主要的设计目的是为了迎合系统大量相似数据的应用而生,减少用于创建和操作相似的细碎对象所花费的成本.大量的对象会消耗高内存,享元模式给 ...
- 转载--tomcat调优
转载 https://www.cnblogs.com/wangsen/p/9337969.html