题意:数独。

析:由于只是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. EasyPlayerPro windows播放器在播放RTMP视频显示重复异常问题解决

    问题来源 2017.12.18 今日有杭州某教育领域客户反馈EasyPlayerPro在播放一个rtmp源时,画面显示异常的问题.截图如下: 问题复现 一番思考, 将显示格式改为D3D显示, 正常, ...

  2. varnish代理缓存服务器的安装与使用

    1. 下载解压 cd /usr/local/src/ wget https://codeload.github.com/varnishcache/varnish-cache/zip/master ch ...

  3. vi中如何替换某字符成“回车”?

    vi中如何替换某字符成“回车”? 在 vi 中::s/,/^M/g (you need to type CTRL-V <CR> to get a ^M here)VIM - Vi IMpr ...

  4. JS性能优化——数据存取

    首先,了解几个概念: 字面量:它只代表自身,不存储在特定的位置.JavaScript中的字面量有:字符串.数字.布尔值.对象.数组.函数.正则,以及特殊的null和undefined值 本地变量:使用 ...

  5. 获取HDC的几种方法

    当需要在显示器上(当然包括打印机等设备上)绘图时,或者写文字的时候,需要取得设备的上下文句柄,即HDC,本文以下都称为HDC.那么,有哪些办法取得HDC呢? 1 BeginPain()和EndPain ...

  6. 《CSS权威指南(第三版)》---第二章 选择器

    本章的主要内容是,怎么获取文档中的元素给予渲染: 1.元素选择器: 2.ID选择器: 3.CLSSS选择器: 4.通配选择器:*; 5.属性选择器:selector[] 6.部分属性选择器: sele ...

  7. 织梦CMS博客风格模板

    织梦CMS博客风格模板,织梦CMS,博客模板,CMS模板.程序模板. 模板地址:http://www.huiyi8.com/sc/7248.html

  8. google IO大会

    怎么参加一次 Google I/O?大概要多少预算? Google I/O(参加Goole I/O 是我的一个梦想,因为我是Google死忠,想亲自去Google总部看看,所以想知道这些) 费用构成: ...

  9. codeforces 558A A. Lala Land and Apple Trees(水题)

    题目链接: A. Lala Land and Apple Trees time limit per test 1 second memory limit per test 256 megabytes ...

  10. 关系逻辑运算符---------&&和||

    1.&&符号 常规用法没什么好说的,我们来说说其不同于java的特殊之处 (1)&&符号究竟返回什么 我们知道,0,null,defined,null,NaN等都可以转 ...