https://scut.online/p/31

还是不知道为什么RE了。的确非常玄学。

重构之后就没问题了。果然写的越复杂,分的情况越乱就越容易找不到bug。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll; int cnt[15];
int cnt2[15]; bool dfs(int x, int rest, bool dui) {
if(rest == 0)
return true;
if(cnt2[x] == 0)
return dfs(x + 1, rest, dui);
if(cnt2[x] == 1) {
if(x <= 7 && cnt2[x + 1] >= 1 && cnt2[x + 2] >= 1) {
cnt2[x]--, cnt2[x + 1]--, cnt2[x + 2]--;
if(dfs(x + 1, rest - 3, dui))
return true;
cnt2[x + 2]++, cnt2[x + 1]++, cnt2[x]++;
}
return false;
}
if(cnt2[x] == 2) {
if(!dui) {
cnt2[x] -= 2;
if(dfs(x + 1, rest - 2, true))
return true;
cnt2[x] += 2;
}
if(x <= 7 && cnt2[x + 1] >= 2 && cnt2[x + 2] >= 2) {
cnt2[x] -= 2, cnt2[x + 1] -= 2, cnt2[x + 2] -= 2;
if(dfs(x + 1, rest - 6, dui))
return true;
cnt2[x + 2] += 2, cnt2[x + 1] += 2, cnt2[x] += 2;
}
return false;
}
if(cnt2[x] >= 3) {
if(!dui) {
cnt2[x] -= 2;
if(dfs(x, rest - 2, true))
return true;
cnt2[x] += 2;
}
cnt2[x] -= 3;
if(dfs(x, rest - 3, dui))
return true;
cnt2[x] += 3;
if(x <= 7 && cnt2[x + 1] >= 1 && cnt2[x + 2] >= 1) {
cnt2[x] --, cnt2[x + 1] --, cnt2[x + 2] --;
if(dfs(x, rest - 3, dui))
return true;
cnt2[x + 2] ++, cnt2[x + 1] ++, cnt2[x] ++;
}
return false;
}
} vector<int> ans; void check(int x) {
memcpy(cnt2, cnt, sizeof(cnt2));
cnt2[x]++;
if(cnt2[x] > 4)
return;
int cnttwo = 7;
for(int i = 1; i <= 9; ++i)
cnttwo -= (cnt2[i] == 2);
if(cnttwo == 0 || dfs(1, 14, false))
ans.push_back(x);
} int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
int T;
char s[20];
scanf("%d", &T);
while(T--) {
scanf("%s", s + 1);
memset(cnt, 0, sizeof(cnt));
for(int i = 1; i <= 13; ++i)
cnt[s[i] - '0']++;
ans.clear();
for(int i = 1; i <= 9; ++i)
check(i);
if(ans.empty())
ans.push_back(-1);
for(auto i : ans)
printf("%d", i);
puts("");
}
}

SCUT - 31 - 清一色 - dfs的更多相关文章

  1. hadoop实战之分布式模式

    环境 192.168.1.101 host101 192.168.1.102 host102 1.安装配置host101 [root@host101 ~]# cat /etc/hosts |grep ...

  2. hadoop集群配置实例

    1)ssh配置 http://allthingshadoop.com/2010/04/20/hadoop-cluster-setup-ssh-key-authentication/ 2) 修改打开文件 ...

  3. 转载:数位DP模板

    // pos = 当前处理的位置(一般从高位到低位) 2 // pre = 上一个位的数字(更高的那一位) 3 // status = 要达到的状态,如果为1则可以认为找到了答案,到时候用来返回, 4 ...

  4. hadoop单机环境搭建

    [在此处输入文章标题] Hadoop单机搭建 1. 工具准备 1) Hadoop Linux安装包 2) VMware虚拟机 3) Java Linux安装包 4) Window 电脑一台 2. 开始 ...

  5. hadoop 管理命令dfsadmin

    hadoop 管理命令dfsadmin dfsadmin 命令用于管理HDFS集群,这些命令常用于管理员. 1. (Safemode)安全模式 动作 命令 把集群切换到安全模式 bin/hdfs df ...

  6. Hadoop 2.7.3 完全分布式维护-动态增加datanode篇

    原有环境 http://www.cnblogs.com/ilifeilong/p/7406944.html  IP       host JDK linux hadop role 172.16.101 ...

  7. hadoop学习笔记(四):hdfs常用命令

    一.hadoop fs 1.创建目录 [root@master hadoop-]# hadoop fs -mkdir /testdir1 [root@master hadoop-]# hadoop f ...

  8. hdu4848 求到达每一个点总时间最短(sum[d[i]])。

    開始的时候是暴力dfs+剪枝.怎么也不行.后来參考他人思想: 先求出每一个点之间的最短路(这样预处理之后的搜索就能够判重返回了).截肢还是关键:1最优性剪枝(尽量最优:眼下的状态+估计还有的最小时间& ...

  9. BZOJ 1053 [HAOI2007]反素数ant(约数个数)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1053 [题目大意] 于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6 ...

随机推荐

  1. Keras MAE和MSE source code

    def mean_squared_error(y_true, y_pred): if not K.is_tensor(y_pred): y_pred = K.constant(y_pred) y_tr ...

  2. JavaScript变量和字面量

    一.什么是变量? 首先了解一下什么是内存:内存就是保存程序在运行过程中,所需要用到的数据8bit(比特是表示信息的最小单位). 8bit=1byte 1024byte=1MB 1024MB=1GB 1 ...

  3. (18)C++项目练习一(功能会不断扩展)--------【聊天工具】

    1.准备使用Qt和C++做一个远程(基于互联网的)聊天工具,需要实现以下功能 (1)多对多聊天功能 (2)文件传输功能 (3)注册.登录功能 (4)加好友.同意好友功能 (5)好友列表.黑名单功能(分 ...

  4. permutation 1

    permutation 1 #include<bits/stdc++.h> using namespace std; ]; void init() { ; i<=; i++)A[i] ...

  5. 解决:父类中的@NotNull无效以及@Notnull 验证list对象无效

    解决方法如图: controller层 vo.param层 父类验证注解要使用@NotEmpty 不能使用 @NotNull,否则验证无效的,反正笔者是没有成功过

  6. RAC & MVVM 学习资料整理

    最后更新:2017-01-23 参考链接: MVVM奇葩说 MVVM 介绍 Model-View-ViewModel for iOS [译] 唐巧--被误解的 MVC 和被神化的 MVVM React ...

  7. linux 下 一步一步安装odb

    Introduction This guide presents step-by-step instructions for installing the ODB system on UNIX-lik ...

  8. 架构-数据库访问-SQL语言进行连接数据库服务器-DB-Library:DB-Library

    ylbtech-数据库访问-SQL语言进行连接数据库服务器-DB-Library:DB-Library 1.返回顶部 1. 在基于三层构架的信息系统开发中,应用服务器要利用SQL语言进行连接数据库服务 ...

  9. leetcode 142. 环形链表 II(c++)

    给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 - ...

  10. Spring命名空间引入方法

    spring 整合了各种工具,并且spring提供了对各种工具的xml scheme 的配置方式,简化了开发. 但是对于各种工具的xml命名空间的引入,我一直很郁闷,不知道应该怎样引入,今天经过摸索发 ...