SCUT - 31 - 清一色 - dfs
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的更多相关文章
- hadoop实战之分布式模式
环境 192.168.1.101 host101 192.168.1.102 host102 1.安装配置host101 [root@host101 ~]# cat /etc/hosts |grep ...
- hadoop集群配置实例
1)ssh配置 http://allthingshadoop.com/2010/04/20/hadoop-cluster-setup-ssh-key-authentication/ 2) 修改打开文件 ...
- 转载:数位DP模板
// pos = 当前处理的位置(一般从高位到低位) 2 // pre = 上一个位的数字(更高的那一位) 3 // status = 要达到的状态,如果为1则可以认为找到了答案,到时候用来返回, 4 ...
- hadoop单机环境搭建
[在此处输入文章标题] Hadoop单机搭建 1. 工具准备 1) Hadoop Linux安装包 2) VMware虚拟机 3) Java Linux安装包 4) Window 电脑一台 2. 开始 ...
- hadoop 管理命令dfsadmin
hadoop 管理命令dfsadmin dfsadmin 命令用于管理HDFS集群,这些命令常用于管理员. 1. (Safemode)安全模式 动作 命令 把集群切换到安全模式 bin/hdfs df ...
- Hadoop 2.7.3 完全分布式维护-动态增加datanode篇
原有环境 http://www.cnblogs.com/ilifeilong/p/7406944.html IP host JDK linux hadop role 172.16.101 ...
- hadoop学习笔记(四):hdfs常用命令
一.hadoop fs 1.创建目录 [root@master hadoop-]# hadoop fs -mkdir /testdir1 [root@master hadoop-]# hadoop f ...
- hdu4848 求到达每一个点总时间最短(sum[d[i]])。
開始的时候是暴力dfs+剪枝.怎么也不行.后来參考他人思想: 先求出每一个点之间的最短路(这样预处理之后的搜索就能够判重返回了).截肢还是关键:1最优性剪枝(尽量最优:眼下的状态+估计还有的最小时间& ...
- BZOJ 1053 [HAOI2007]反素数ant(约数个数)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1053 [题目大意] 于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6 ...
随机推荐
- HC-42蓝牙模块-nRF52832-数传蓝牙
资料下载地址:链接:https://pan.baidu.com/s/1RRajrI5NvNY8tRVuYbOTug 提取码:31ho 我的蓝牙模块淘宝购买地址:https://detail.tm ...
- MySQL数据库4Python操作mysql、索引、慢查询日志
目录 一.Python 操作 mysql 1.1python 操作 mysql 1.2查询数据 1.3增加(添加.更新)数据 1.4修改数据 1.5删除数据 1.6SQL注入问题 1.6.1问题的引入 ...
- php----等比缩放图片
<?php /** * Created by PhpStorm. * User: admin * Date: 2019/11/19 * Time: 8:54 */ $filename = 'lo ...
- python-jsonpath、findall返回值提取
findall import re """ "d"表示取数字0-9, "D"表示不要数字, "w"在正则里面代 ...
- asp.net能否上传文件夹下所有文件
HTML部分 <%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="index.aspx. ...
- Java——容器(Map)
[Map接口]
- CodeForces - 474D (dp)
题目:https://vjudge.net/contest/326867#problem/B 题意:有很多个蛋糕,现在你有两种吃蛋糕的吃法,一次吃一个,定为A,一次吃k个定为B,然后问你吃m个蛋糕有多 ...
- 170831-关于JdbcTemplate声明式事务-操作步骤-例子
创建一个动态web工程 加入jar包 3.创建一份jdbc.properties文件 4.在spring配置文件中配置数据源 5.测试数据源: 6.配置jdbcTemplate: 7.创建Dao类 & ...
- 《SQL Server 2012 T-SQL基础》读书笔记 - 10.可编程对象
Chapter 10 Programmable Objects 声明和赋值一个变量: DECLARE @i AS INT; SET @i = 10; 变量可以让你暂时存一个值进去,然后之后再用,作用域 ...
- whu 1581 Union of cubes
题目链接: http://acm.whu.edu.cn/land/problem/detail?problem_id=1581 ------------------------------------ ...