NOJ 1074 Hey Judge(DFS回溯)
Problem 1074: Hey Judge
Time Limits: 1000 MS Memory Limits: 65536 KB
64-bit interger IO format: %lld Java class name: Main
Description
Judge Nicole collected 7 ideas for problems of different levels, she wants to create 5 problems for the next contest, one for each difficulty level, from A to E (difficulty 1 to 5). Given the difficulty level of the problems she currently has, she can merge the ideas of two problems, one of level x, and the other of level y to get a problem of level x + y.
For example, Judge Nicole can merge two problems of difficulties A and D, to get one problem of difficulty E (1 + 4 = 5).
Merging more than two problems into one will produce a problem with a long statement which is hard to explain, so she won’t do this (i.e., each problem is merged with another at most once). Also, she can’t merge a resultant problem again, and she can't use the same problem twice.
On the next contest, Output YES if the collected questions include A to E, otherwise the output NO.
Input
The first line of input contains an integer T (1 ≤ T ≤ 300), the number of test cases.
Each test case will contain only one string S of length 7. Each letter of the string represents the difficulty level of a problem (from A to E), 'A' is the easiest and 'E' is the hardest.
Output
For each test case print "YES" if she can prepare a contest using the current problems, otherwise print "NO".
Sample Input
3
EBEABDA
CEDEACA
BDAAEAA
Output for Sample Input
YES
NO
YES
周赛的题目,裸字典树那题ZZ叶子忘记判断一直WA,这题又理解成贪心瞎搞搞调试半天WA,DP那题又不会,最后可惜地爆0结尾了,还是too young too naive……DFS回溯暴搜的题目,下午突然有灵感发现题目并不难。
由于情况太多不知道到底当前的字母是由另外两个合成的还是用原来就有的,感觉不是贪心,那就直接用DFS分情况搜索,假设某个难度的字母就是用原来的、或者是其他两个合成的,两种情况回溯地搜一下就好
代码:
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=15;
char s[N];
int vis[N],ok[N];
bool flag;
inline int getid(const char &x)
{
return x-'A'+1;
}
void dfs(int now)
{
int tempflag=1;
for (int i=1; i<=5; ++i)
{
if(!ok[i])
{
tempflag=0;
break;
}
}
if(tempflag)
flag=true;
if(flag||now>=6)
return ;
for (int i=0; i<7; ++i)
{
if(vis[i])
continue;
if(getid(s[i])==now)
{
vis[i]=1;
ok[now]=1;
dfs(now+1);
ok[now]=0;
vis[i]=0;
}
for (int j=0; j<7; ++j)
{
if(vis[j]||i==j)
continue;
int sum=getid(s[i])+getid(s[j]);
if(sum==now)
{
vis[i]=1;
vis[j]=1;
ok[now]=1;
dfs(now+1);
vis[i]=0;
vis[j]=0;
ok[now]=0;
}
}
}
}
int main(void)
{
int n;
scanf("%d",&n);
while (n--)
{
fill(s,s+N,'\0');
fill(vis,vis+N,0);
fill(ok,ok+N,0);
scanf("%s",s);
flag=false;
dfs(1);
puts(flag?"YES":"NO");
}
return 0;
}
NOJ 1074 Hey Judge(DFS回溯)的更多相关文章
- 蓝桥杯 算法提高 8皇后·改 -- DFS 回溯
算法提高 8皇后·改 时间限制:1.0s 内存限制:256.0MB 问题描述 规则同8皇后问题,但是棋盘上每格都有一个数字,要求八皇后所在格子数字之和最大. 输入格式 一个8*8 ...
- 素数环(dfs+回溯)
题目描述: 输入正整数n,把整数1,2...n组成一个环,使得相邻两个数和为素数.输出时从整数1开始逆时针排列并且不能重复: 例样输入: 6 例样输出: 1 4 3 2 5 6 1 6 5 2 3 4 ...
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 2181 哈密顿绕行世界问题(经典DFS+回溯)
哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU1016 Prime Ring Problem(DFS回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- uva 193 Graph Coloring(图染色 dfs回溯)
Description You are to write a program that tries to find an optimal coloring for a given graph. Col ...
- P1074 靶形数独 dfs回溯法
题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教,Z 博士拿出了他最近发明的“靶 ...
- 剪格子---(dfs回溯)
如图p1.jpg所示,3 x 3 的格子中填写了一些整数. 我们沿着图中的红色线剪开,得到两个部分,每个部分的数字和都是60. 本题的要求就是请你编程判定:对给定的m x n 的格子中的整数,是否可以 ...
- poj1270Following Orders(拓扑排序+dfs回溯)
题目链接: 啊哈哈.点我点我 题意是: 第一列给出全部的字母数,第二列给出一些先后顺序. 然后按字典序最小的方式输出全部的可能性.. . 思路: 整体来说是拓扑排序.可是又非常多细节要考虑.首先要按字 ...
随机推荐
- [bzoj3813]奇数园
仿佛现在已经完成了做题之前先开个坑的习惯,也许是为了逼迫自己去刷一些神题吧...然并卵,该剩的好多坑还是剩着呢. [bzoj3813]一道线段树好题.已经把数论忘光光了. 欧几里德算法 扩展欧几里德算 ...
- Linux(CentOS)中安装MongoDB
1. 下载对应操作系统和机器的版本 网址: https://www.mongodb.com/download-center?jmp=docs 我的系统为CentOS-6.6 ,64位,所以选择红帽子版 ...
- java中 ==与equals 有什么区别?
1.==既可以比较基本类型变量,又可比较引用类型变量,而equals只能比较引用类型变量: 2.equals方法支持重写,如果未重写equals方法,则比较引用变量时与==都是比较变量所指向的对象地址 ...
- 4D时间管理
时间管理的4D原则 时间矩阵:每个人,每天要做的事情大致分为四类:一类:必须做的,不做会产生不良后果的二类:需要做的,为将来或者今后阶段做准备工作的三类:可以不做,但又不得不做的,例如通过简约形式 ...
- 【原】iOS学习43即时通信之XMPP(2)
本篇是 即时通信之XMPP(2) 接上次 即时通信之XMPP(1) 1. 好友列表 1> 初始化好友花名册 // 获取管理好友的单例对象 XMPPRosterCoreDataStorage *r ...
- HTML5 Geolocation 构建基于地理位置的 Web 应用
HTML5 中的新功能 HTML5 是最新一代的 HTML 规范,是 W3C 与 WHATWG 合作的结果,目前仍外于开发中.自从上一代 HTML4,Web 世界已经发生了巨大的变化,HTML5 的到 ...
- 使用jQuery 的.on() 提交表单
示例: $(function () { $(document).on('submit', '#FormId', function () { var val = $("#Name") ...
- qt编译常见错误
一.fatal error: QWidget: 没有那个文件或目录 类似于找不到文件目录的,在.pro文件中添加 QT +=\ widgets 类似就可以编译通过
- java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries
在已经搭建好的集群环境Centos6.6+Hadoop2.7+Hbase0.98+Spark1.3.1下,在Win7系统Intellij开发工具中调试Spark读取Hbase.运行直接报错: ? 1 ...
- upload控件上传json文件合并的两种方法
方法一: byte[] byte1 = FileUpload1.FileBytes; byte[] byte2 = FileUpload2.FileBytes; byte[] a1 = Encodin ...