题意:数独。

析:由于只是4*4,完全可以暴力,要注意一下一些条件,比如2*2的小方格也得是1234

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 10 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
char s[maxn][maxn];
int a[maxn][maxn]; bool judge(int r, int c, int x){
int cnt = 0;
for(int i = 0; i < 4; ++i)
if(a[r][i] == x) ++cnt;
if(cnt > 0) return false;
cnt = 0;
for(int i = 0; i < 4; ++i)
if(a[i][c] == x) ++cnt;
return cnt < 1;
} bool solve(){
set<int> sets;
for(int i = 0; i < 2; ++i)
for(int j = 0; j < 2; ++j)
sets.insert(a[i][j]);
if(sets.size() != 4) return false;
sets.clear();
for(int i = 2; i < 4; ++i)
for(int j = 2; j < 4; ++j)
sets.insert(a[i][j]);
if(sets.size() != 4) return false;
sets.clear();
for(int i = 2; i < 4; ++i)
for(int j = 0; j < 2; ++j)
sets.insert(a[i][j]);
if(sets.size() != 4) return false;
sets.clear();
for(int i = 0; i < 2; ++i)
for(int j = 2; j < 4; ++j)
sets.insert(a[i][j]);
if(sets.size() != 4) return false;
return true;
} bool dfs(int r, int c){
if(a[r][c]){
if(r == 3 && c == 3) return solve();
return c == 3 ? dfs(r+1, 0) : dfs(r, c+1);
}
for(int i = 1; i < 5; ++i) if(judge(r, c, i)){
a[r][c] = i;
int rr = r, cc = c;
if(r == 3 && c == 3){
if(solve()) return true;
a[r][c] = 0;
continue;
}
if(c == 3) ++rr, cc = 0;
else ++cc;
if(dfs(rr, cc)) return true;
a[r][c] = 0;
}
return false;
} int main(){
int T; cin >> T;
for(int kase = 1; kase <= T; ++kase){
for(int i = 0; i < 4; ++i) scanf("%s", s+i);
memset(a, 0, sizeof a);
for(int i = 0; i < 4; ++i)
for(int j = 0; j < 4; ++j)
if(s[i][j] != '*') a[i][j] = s[i][j] - '0';
dfs(0, 0);
printf("Case #%d:\n", kase);
for(int i = 0; i < 4; ++i, printf("\n"))
for(int j = 0; j < 4; ++j)
printf("%d", a[i][j]);
}
return 0;
}

  

HDU 5547 Sudoku (暴力)的更多相关文章

  1. HDU - 5547 Sudoku(数独搜索)

    Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself ...

  2. HDU 5547 Sudoku(DFS)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5547 题目: Sudoku Time Limit: 3000/1000 MS (Java/Others ...

  3. E - Sudoku HDU - 5547 (搜索+暴力)

    题目链接:https://cn.vjudge.net/problem/HDU-5547 具体思路:对于每一位上,我们可以从1到4挨着去试, 具体判断这一位可不可以的时候,看当前这一位上的行和列有没有冲 ...

  4. The 2015 China Collegiate Programming Contest H. Sudoku hdu 5547

    Sudoku Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

  5. (hdu)5547 Sudoku (4*4方格的 数独 深搜)

    Problem Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game ...

  6. HDU 5547 暴力

    Sudoku Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

  7. HDU - 5547 数独(回溯法)

    题目链接:HDU-5547 http://acm.hdu.edu.cn/showproblem.php?pid=5547 正所谓:骗分过样例,暴力出奇迹. 解题思想(暴力出奇迹(DFS+回溯)): 1 ...

  8. HDU 5510 Bazinga 暴力匹配加剪枝

    Bazinga Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5510 ...

  9. HDU 5522 Numbers 暴力

    Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5522 ...

随机推荐

  1. 【BZOJ4198】[Noi2015]荷马史诗 贪心+堆

    [BZOJ4198][Noi2015]荷马史诗 Description 追逐影子的人,自己就是影子. ——荷马 Allison 最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅 ...

  2. 关于TransactionScope 使用

    在去年的项目中使用了TransactionScope,现在总结下TransactionScope的使用说明 一.TransactionScope是.Net Framework 2.0之后,新增了一个名 ...

  3. JSON格式之GSON解析

    JSON格式之GSON解析 最近在做websocket相关,项目需要JSON解析.相较之下感觉google的GSON解析不错. JAVA后台 Gson提供了fromJson()方法来实现从Json相关 ...

  4. iOS应用上架报错解决

    ERROR ITMS-90087: "Unsupported Architectures. The executable for LiveStorage.app/Frameworks/Spe ...

  5. nohup COMMAND > FILE

    nohup  --help nohup(1) - Linux man page https://linux.die.net/man/1/nohup

  6. Python爬虫-- selenium库

    selenium库 selenium 是一套完整的web应用程序测试系统,包含了测试的录制(selenium IDE),编写及运行(Selenium Remote Control)和测试的并行处理(S ...

  7. Android笔记之引用aar

    把要引用的aar文件复制到目录app\libs中(我要引用的aar名为xybigdatasdk-release-out2.2.6.aar) 在build.gradle (Module: app)中添加 ...

  8. 我的Android进阶之旅------>Android资源文件string.xml中\u2026的意思

    今天看了一个string.xml文件,对其中的一行代码中包含的\u2026不是很理解,后来查阅资料后发现了其中的意思. 代码如下: <resources xmlns:xliff="ur ...

  9. Nginx + Tomcat 应用证书启用 SSL

    第一部分 简述 - 附:相关概念 1 Nginx 是什么? - 2 Tomcat 是什么? - 3 SSL 是什么? Secure Sockets Layer,现在应该叫"TLS" ...

  10. Django—工程创建以及models数据库易错点

    Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...