ACM学习历程—UESTC 1222 Sudoku(矩阵)(2015CCPC H)
题目链接: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)的更多相关文章
- ACM学习历程—UESTC 1215 Secrete Master Plan(矩阵旋转)(2015CCPC A)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1215 题目大意就是问一个2*2的矩阵能否通过旋转得到另一个. 代码: #include <iostre ...
- ACM学习历程—UESTC 1218 Pick The Sticks(动态规划)(2015CCPC D)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 题目大意就是求n根木棒能不能放进一个容器里,乍一看像01背包,但是容器的两端可以溢出容器,只要两端的木 ...
- ACM学习历程—UESTC 1217 The Battle of Chibi(递推 && 树状数组)(2015CCPC C)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 题目大意就是求一个序列里面长度为m的递增子序列的个数. 首先可以列出一个递推式p(len, i) = ...
- ACM学习历程—UESTC 1226 Huatuo's Medicine(数学)(2015CCPC L)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目就是构造一个对称的串,除了中间的那个只有1个,其余的两边都是对称的两个,自然答案就是2*n-1. ...
- ACM学习历程—UESTC 1219 Ba Gua Zhen(dfs && 独立回路 && xor高斯消元)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1219 题目大意是给了一张图,然后要求一个点通过路径回到这个点,使得xor和最大. 这是CCPC南阳站的一道题 ...
- ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...
- ACM学习历程—SNNUOJ1215 矩阵2(二分 && dfs)
http://219.244.176.199/JudgeOnline/problem.php?id=1215 这是这次微软和百度实习面试的一道题,题目大意就是:有一个n*m的矩阵,已知它每一行都是不严 ...
- ACM学习历程—SNNUOJ1214 矩阵1(二分)
题目链接:http://219.244.176.199/JudgeOnline/problem.php?id=1214 这是这次微软实习面试的一道题,题目大意就是:有一个n*m的矩阵,已知它每一行都是 ...
- ACM学习历程——HDU5015 233 Matrix(矩阵快速幂)(2014陕西网赛)
Description In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 2 ...
随机推荐
- MFC添加菜单资源与菜单执行函数的两种命令形式
添加资源->新建一个菜单资源->选择相应的对话框 菜单的执行函数命令形式: COMMAD 是指点击菜单后的执行命令 UPDATE_COMMAND_UI 是指点击菜单后菜单状态的函数
- 《Hive编程指南》问题
1.Hive不支持记录级别的更新.插入或删除? 2.sort by 和 order by 的区别? https://blog.csdn.net/jthink_/article/details/3890 ...
- Js 抱错:::SyntaxError: identifier starts immediately after numeric literal
SyntaxError: identifier starts immediately after numeric literal 今天写了个onclick()方法,有这样的一个变量4028b88161 ...
- 初步jmeter安装与使用
前言,最近公司做了面向全国用户的教育平台,由于测试人员以功能测试为主,于是接口代码压测就被开发揽了,这就开始倒腾jmeter了,其实我想对于java,我更愿意用Python的工具,毕竟我爬虫时用的Py ...
- 免费好用的Diff和Merge工具大总结
总结:比较下来:diffmerge和P4merge最好用,kdiff比较专业些,支持自动merge. 一 csdiff 下载:http://www.componentsoftware.com/Prod ...
- 在vi或vim上查找字符串
从开头搜索 在命令模式下,输入/你要查找的字符 按下回车,可以看到vim把光标移动到该字符处 再按n(小写)查看下一个匹配 按N(大写)查看上一个匹配, capslock切换大小写,也可以在小写状态下 ...
- rails常用函数
1.rails g controller Users rails g model User 2.user.reload.email reload 使用数据库中的数据重新加载对象
- HDU - 1134 Game of Connections 【DP】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1134 题意 给出一个n 然后有2n个点 给两个点连一条边,最后连N条边,要求所有的边不能够交叉 问最多 ...
- uCGUI 按键窗口切换机制
前段时间在做一个窗口项目,这个项目菜单项过多,在管理起来比较麻烦.想做一个高效移植又方便的一个切换机制.后来在网上多方查找这方面资料,但是感觉比较少.后来自己整理出了这个结构,希望对后来朋友有所帮助. ...
- mini2440移植uboot 2014.04(四)
我修改的代码已经上传到github上,地址:https://github.com/qiaoyuguo/u-boot-2014.04-mini2440.git 参考文章: <mini2440移植u ...