题意:数独。

析:由于只是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. python使用记录

    #2017-7-17 1.用len()函数可以获得list元素的个数; len()可以获取字符串长度 2. list正向0开始索引,,逆向-1开始索引; 也可以把元素插入到指定的位置,比如索引号为1的 ...

  2. ubuntu16.04+cuda8.0+cudnn5.0+caffe

    ubuntu安装过程(硬盘安装)http://www.cnblogs.com/zhbzz2007/p/5493395.html“但是千万不要用麒麟版!!!比原版体验要差很多!!!”开关机的时候电脑最上 ...

  3. 我的Android进阶之旅------>解决如下错误failed to copy 'Settings2.apk' to '/system/app//Settings2.apk': Read-only

    push apk的时候报错 ouyangpeng@oyp-ubuntu:~/apk升级$ adb push Settings2.apk /system/app/ failed to copy 'Set ...

  4. Ceph集群rbd-mirror A、B区域备份实施方案

    Ceph集群rbd-mirror A.B区域备份实施方案 备注:首先准备两个集群, 并确认其状态,集群的准备过程在这就不做陈述 1.查看集群状态 A区域 [root@ceph2111 ceph]# c ...

  5. Pentaho BIServer Community Edtion 6.1 使用教程 第四篇 安装和使用Saiku 插件 进行 OLAP

    OLAP(On-Line Analytical Processing,联机分析处理)是一个使分析师.管理者和执行者从原始数据中用来快速.一致.交互访问的一种软件技术,从而真实的反映企业的数据情况.OL ...

  6. 编写你的第一个django应用程序2

    从1停止的地方开始,我们将设置数据库,创建您的第一个模型,并快速介绍django自动生成的管理站点 数据库设置 现在,打开mysite/settings.py.这是一个普通的python模块,其中模块 ...

  7. poj 1146 ID Codes (字符串处理 生成排列组合 生成当前串的下一个字典序排列 【*模板】 )

    ID Codes Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6229   Accepted: 3737 Descript ...

  8. SDUT OJ 螺旋矩阵

    螺旋方阵 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 n×n的螺旋方阵当n=5和n=3时分别是如下的形式 请给出一个程序,对于 ...

  9. Contiki Etimer 模块

    一.Etimer概述 Etimer提供产生时间事件(timed event)的机制,当设定好的timer到期时,将会给设定etimer的process发送一个PROCESS_EVENT_TIMER 事 ...

  10. tomcat 6.0 安装及配置

    前提:安装并配置好jdk 1.免安装版存放目录:D:\01Install\tomcat