题意:数独。

析:由于只是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. Ajax学习笔记(2)--load()方法

    <head runat="server"> <title></title> <script src="http://localh ...

  2. 九度OJ 1033:继续xxx定律 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4987 解决:1201 题目描述:     当n为3时,我们在验证xxx定律的过程中会得到一个序列,3,5,8,4,2,1,将3称为关键数, ...

  3. BZOJ2539: [Ctsc2000]丘比特的烦恼

    BZOJ2539: [Ctsc2000]丘比特的烦恼 Description 随着社会的不断发展,人与人之间的感情越来越功利化. 最近,爱神丘比特发现,爱情也已不再是完全纯洁的了. 这使得丘比特很是苦 ...

  4. Flask:基本结构

    python有两个比较出名的网络框架,一个是django,一个是flask. 之前的django文章里面介绍了django的各种用法,这个系列开始介绍flask的用法.相比与django,flask更 ...

  5. PHP使用Apache中的ab测试网站的压力性能

    打开Apache服务器的安装路径(我用的是 WampServer),在bin目录中有一个ab.exe的可执行程序,它就是要介绍的压力测试工具. 在Windows系统的命令行下,进入ab.exe程序所在 ...

  6. PR 批量导入

    REPORT  ZMM_UPLOAD_PR. DATA: BEGIN OF GT_DATA1 OCCURS 0,            BSART   TYPE STRING, "凭证类型  ...

  7. Java7、Java8 安装卸载问题

    win7 系统,同时安装了JDK7和JDK8,卸载了JDK8之后,cmd命令行输入:java -version ,本以为显示java版本1.7,结果弹错:has value '1.7',but '1. ...

  8. java参数传递------真心是值传递

    未完待续 不同意的请尽管去深入看一下. 对象是引用传递没错,参数传递是值传递.

  9. 怎么升级iOS10教程

    在前两天的开发者大会上刚推出了iOS10,我介绍一下怎么升级到iOS10的办法.所有人只用一个iPhone就可以升级到iOS10,不需要电脑,也不需要开发者账号. http://bbs.feng.co ...

  10. git用远程库的内容覆盖本地

    git fetch --all 下载远程的库的内容到本地,不做任何的合并(怎么合并可以自己选择) git reset --hard origin/master 撤销本地.暂存区.版本库(用远程服务器的 ...