题目链接:http://acm.uestc.edu.cn/#/problem/show/1226

题目大意就是构造一个行列和每个角的2*2都是1234的4*4矩阵。

用dfs暴力搜索,不过需要每一步进行判断是否已经出现了重复,如果最后再判断的话复杂度有点高。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long using namespace std; char str[][];
int a[][];
bool vis[];
bool flag; bool judge(int x[][])
{
for (int i = ; i < ; ++i)
{
memset(vis, false, sizeof(vis));
for (int j = ; j < ; ++j)
{
if (!x[i][j]) continue;
if (vis[x[i][j]]) return false;
else vis[x[i][j]] = true;
}
}
for (int j = ; j < ; ++j)
{
memset(vis, false, sizeof(vis));
for (int i = ; i < ; ++i)
{
if (!x[i][j]) continue;
if (vis[x[i][j]]) return false;
else vis[x[i][j]] = true;
}
}
for (int i = ; i <= ; i += )
{
for (int j = ; j <= ; j += )
{
memset(vis, false, sizeof(vis));
for (int xx = ; xx <= ; xx++)
{
for (int yy = ; yy <= ; yy++)
{
if (!x[i+xx][j+yy]) continue;
if (vis[x[i+xx][j+yy]]) return false;
else vis[x[i+xx][j+yy]] = true;
}
}
}
}
return true;
} void input()
{
for (int i = ; i < ; ++i)
{
scanf("%s", str[i]);
for (int j = ; j < ; ++j)
{
if (str[i][j] == '*')
a[i][j] = ;
else
a[i][j] = str[i][j]-'';
}
}
} void dfs(int i, int j)
{
if (flag) return;
i += j/;
j %= ;
if (i == )
{
if (judge(a))
{
for (int i = ; i < ; ++i)
{
for (int j = ; j < ; ++j)
printf("%d", a[i][j]);
printf("\n");
}
flag = true;
}
return;
}
if (!judge(a)) return;
if (!a[i][j])
{
for (int x = ; x <= ; ++x)
{
a[i][j] = x;
dfs(i, j+);
a[i][j] = ;
}
}
else dfs(i, j+);
} void work()
{
flag = false;
dfs(, );
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
for (int times = ; times <= T; ++times)
{
printf("Case #%d:\n", times);
input();
work();
}
return ;
}

ACM学习历程—UESTC 1222 Sudoku(矩阵)(2015CCPC H)的更多相关文章

  1. ACM学习历程—UESTC 1215 Secrete Master Plan(矩阵旋转)(2015CCPC A)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1215 题目大意就是问一个2*2的矩阵能否通过旋转得到另一个. 代码: #include <iostre ...

  2. ACM学习历程—UESTC 1218 Pick The Sticks(动态规划)(2015CCPC D)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 题目大意就是求n根木棒能不能放进一个容器里,乍一看像01背包,但是容器的两端可以溢出容器,只要两端的木 ...

  3. ACM学习历程—UESTC 1217 The Battle of Chibi(递推 && 树状数组)(2015CCPC C)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 题目大意就是求一个序列里面长度为m的递增子序列的个数. 首先可以列出一个递推式p(len, i) =  ...

  4. ACM学习历程—UESTC 1226 Huatuo's Medicine(数学)(2015CCPC L)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目就是构造一个对称的串,除了中间的那个只有1个,其余的两边都是对称的两个,自然答案就是2*n-1. ...

  5. ACM学习历程—UESTC 1219 Ba Gua Zhen(dfs && 独立回路 && xor高斯消元)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1219 题目大意是给了一张图,然后要求一个点通过路径回到这个点,使得xor和最大. 这是CCPC南阳站的一道题 ...

  6. ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...

  7. ACM学习历程—SNNUOJ1215 矩阵2(二分 && dfs)

    http://219.244.176.199/JudgeOnline/problem.php?id=1215 这是这次微软和百度实习面试的一道题,题目大意就是:有一个n*m的矩阵,已知它每一行都是不严 ...

  8. ACM学习历程—SNNUOJ1214 矩阵1(二分)

    题目链接:http://219.244.176.199/JudgeOnline/problem.php?id=1214 这是这次微软实习面试的一道题,题目大意就是:有一个n*m的矩阵,已知它每一行都是 ...

  9. ACM学习历程——HDU5015 233 Matrix(矩阵快速幂)(2014陕西网赛)

    Description In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 2 ...

随机推荐

  1. H - Coins

    H - Coins Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descripti ...

  2. jvm的字符串池

    1 jvm中是有专门的字符串池的内存空间的,这块空间和栈和堆不同. 2 String s = "string constant"; 这个时候,如果string pool中没有&qu ...

  3. 洛谷P3943 星空

    洛谷P3943 星空 题目背景 命运偷走如果只留下结果, 时间偷走初衷只留下了苦衷. 你来过,然后你走后,只留下星空. 题目描述 逃不掉的那一天还是来了,小 F 看着夜空发呆. 天上空荡荡的,没有一颗 ...

  4. PAT 1055. 集体照 (25)

    拍集体照时队形很重要,这里对给定的N个人K排的队形设计排队规则如下: 每排人数为N/K(向下取整),多出来的人全部站在最后一排: 后排所有人的个子都不比前排任何人矮: 每排中最高者站中间(中间位置为m ...

  5. Apache Shiro 使用手册(一)Shiro架构介绍(转发:http://kdboy.iteye.com/blog/1154644#bc2399255)

    一.什么是Shiro Apache Shiro是一个强大易用的Java安全框架,提供了认证.授权.加密和会话管理等功能: 认证 - 用户身份识别,常被称为用户“登录”: 授权 - 访问控制: 密码加密 ...

  6. virt-v2v 使用指南

    virt-v2v 使用指南 1.定义. virt-v2v将外部的虚拟化平台上的虚拟机转化到可以运行的KVM平台上.它可以读取在VMware.Xen运行Hyper-V和其他虚拟机管理程序上的Window ...

  7. 数组中去除重复的对象的简单方法and&&的使用

    const arr = [ { name:'tom', age:15 }, { name:'rose', age:17 }, { name:'tom', age:11 }, { name:'rose' ...

  8. ARDUINO解析GPS数据,读取出来GPRMC帧数据

    测试板子使用ARDUINO MEGA2560 #include <TimerOne.h> #define GPSSerial Serial1 #define DebugSerial Ser ...

  9. Python基础(1)_python介绍、简单运算符

    Python执行一个程序分为三个阶段 阶段一:先启动python解释器 阶段二:python解释器把硬盘中的文件读入到内存中 阶段三:python解释器解释执行刚刚读入内存的代码 二.编程语言的分类: ...

  10. html5 canvas做的图表插件

    用highchart的时候发现它是用svg来画图的,那么用canvas来做怎么样的. 以前做AS图表插件的时候,绘制图画主要用容器的Graphics对象来绘制,而canvas的context和Grap ...