题目链接: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. 对Mybatis的理解

    首先Mybatis是一个对象关系映射(Object Relational Mapping,简称ORM)框架,是为了解决面向对象与关系数据库存在的互不匹配的现象.也就是说Mybatis的关注点在于对象与 ...

  2. Python装饰器 计时器记录方法执行性能

    import time def timeit(func): def wrapper(): start = time.clock() func() end =time.clock() print 'us ...

  3. 大华NVR设备接分别入宇视摄像机Onvif和RTSP主子码流的方案说明

    需求提要 1.各个内网现场有多种网络摄像机IPC和网络硬盘录像机NVR设备: 2.需要将这些设备统一接入到云端中心平台,进行统一的视频直播和录像回放管理: 3.由于目前IPC设备都属于高清设备,主码流 ...

  4. debian切换sh shell到bash shell

    1 安装dpkg-reconfigure命令 切换到root账户即可. 2 dpkg-reconfigure dash 选择no

  5. mongo explain分析详解

    1 为什么要执行explain,什么时候执行 explain的目的是将mongo的黑盒操作白盒化. 比如查询很慢的时候想知道原因. 2 explain的三种模式 2.1 queryPlanner 不会 ...

  6. maven filters 和 resource

    1 filter 1.1 用途 对多个配置文件进行选择. 1.2 选择的依据 1.3 使用的方式 第一,在<resource>标签下面加<filtering>标签,并且< ...

  7. 浅谈命令查询职责分离(CQRS)模式---转载

    在常用的三层架构中,通常都是通过数据访问层来修改或者查询数据,一般修改和查询使用的是相同的实体.在一些业务逻辑简单的系统中可能没有什么问题,但是随着系统逻辑变得复杂,用户增多,这种设计就会出现一些性能 ...

  8. python爬虫之request and BeautifulSoup

    1.爬虫的本质是什么? 模仿浏览器的行为,爬取网页信息. 2.requests 1.get请求 无参数实例 import requests ret = requests.get('https://gi ...

  9. 【HTTP】HTPP学习笔记

    1.了解web及网络基础 HTTP的诞生 TCP/IP协议族 应用层 FTP文件传输协议 HTTP超文本传输协议 DNS域名系统:IP地址<--->域名 传输层 TCP传输控制协议 三次握 ...

  10. Spark0.9.0机器学习包MLlib-Optimization代码阅读

           基于Spark的一个生态产品--MLlib,实现了经典的机器学算法,源码分8个文件夹,classification文件夹下面包含NB.LR.SVM的实现,clustering文件夹下面包 ...