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. puppet使用rsync模块

    puppet使用rsync模块同步目录和文件   环境说明: OS :    CentOS5.4                         i686puppet版本:               ...

  2. HTML中的表单<form>标签

    一.HTML表单 HTML 表单用于搜集不同类型的用户输入. HTML 表单包含表单元素,表单元素指的是不同类型的 input 元素.复选框.单选按钮.提交按钮等等. 关于表单的更多内容可以参考htt ...

  3. java mar --->JSONArray.fromObject

    <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</art ...

  4. ubuntu1804隐藏顶部工作栏

    先安装 sudo apt-get install gnome-shell-extension-autohidetopbar 然后安装 sudo apt-get install gnome-shell- ...

  5. Floyd求解最短路

    Floyd算法适用于求解全源最短路.也就是能够求解任意两点间的最短路径并且是适用于含有负权边的图,但是含有负环则不行了!空间复杂度为O(n2).时间复杂度为O(n3).其具体的原理在百度能够找到很多, ...

  6. [CF535D]Tavas and Malekas 题解

    题意简述 有一个空着的长度为\(n\)的字符串\(ans\),再给出一个长度为\(m\)的序列\(a\),现要在序列中每个元素\(a_i\)的位置开始都规定必须为一个给定的字符串\(s\).问字符串\ ...

  7. rman备份/恢复

    全备脚本 cat rman_full.sh #!/bin/bash export ORACLE_BASE=/opt/oracle export ORACLE_HOME=$ORACLE_BASE/pro ...

  8. React Native商城项目实战10 - 个人中心中间内容设置

    1.新建一个MineMiddleView.js,专门用于构建中间的内容 /** * 个人中心中间内容设置 */ import React, { Component } from 'react'; im ...

  9. CentOS7 日常操作

    A 安装netstat1.首先配置好本机的yum源: yum repolist all2.利用netstat命令,却提示:-bash: netstat: command not found3.执行yu ...

  10. ASP Loading

    ASP 页面加载等待效果.如 显示"请稍后页面正在加载...",加载完成后隐藏这个loading. <%Response.buffer=false%> <html ...