SCUT - 31 - 清一色 - dfs
2024-08-31 20:24:36
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("");
}
}
最新文章
- Database Replay和Consolidated Database replay
- 一个简单oop的changeTab
- net面试题
- cell 内部 设置width 总不对
- .net c# 视频剪切抓取缩略图
- cygwin-使用介绍
- NS2中trace文件分析
- HDU 3089 (快速约瑟夫环)
- javascript 判断浏览器的ie版本,替换html标签
- Android RecyclerView添加Header头部
- iOS开发——实战总结OC篇&;网易彩票开发知识点总结
- 基于单个 div 的 CSS 画图
- linux c: core dump
- c++设计模式-----抽象工厂模式
- HDU 5543 Pick The Sticks
- Android应用性能测试之CPU和内存占用
- 学习java的感受
- ubuntu修改grub,修改开机顺序,配置grub启动顺序
- Samba原理和配置
- Oracle 12c CDB PDB 安装/配置/管理