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;
}
时间: 2024-10-05 03:07:52

NOJ 1074 Hey Judge(DFS回溯)的相关文章

蓝桥杯 算法提高 8皇后&#183;改 -- DFS 回溯

  算法提高 8皇后·改   时间限制:1.0s   内存限制:256.0MB 问题描述 规则同8皇后问题,但是棋盘上每格都有一个数字,要求八皇后所在格子数字之和最大. 输入格式 一个8*8的棋盘. 输出格式 所能得到的最大数字和 样例输入 1 2 3 4 5 6 7 89 10 11 12 13 14 15 1617 18 19 20 21 22 23 2425 26 27 28 29 30 31 3233 34 35 36 37 38 39 4041 42 43 44 45 46 47 48

CodeForces 550B Preparing Olympiad(DFS回溯)

[题目链接]:click here~~ [题目大意] 一组题目的数目(n<=15),每个题目有相应的难度,问你选择一定的题目(大于r个且小于l个)且选择后的题目里最小难度与最大难度差不小于x,求选择方案数. [解题思路]: DFS+回溯. 先发一发比较拙的代码: #include <bits/stdc++.h> using namespace std; const int N=1e5+10; int num[N],mum[N]; int n,m,q,t,l,r; int top,ans,

HDU4499 Cannon DFS 回溯的应用

题意就是给你一个n*m的棋盘,然后上面已经有了 棋子,并给出这些棋子的坐标,但是这些棋子是死的就是不能动,然后让你在棋盘上面摆炮,但是炮之间不能互相吃,吃的规则我们斗懂得 炮隔山打嘛,问你最多能放几个炮 肯定是搜索了,n,m最大才5,可能挺久没做了,对于回溯反而把握不好了,写了好久调试了好久,才过 #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #

POJ2488-A Knight&#39;s Journey(DFS+回溯)

题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36695   Accepted: 12462 Description Background The knight is getting bored of seeing the same black and white squares again and again

poj1321——dfs回溯

POJ 1321  DFS回溯+递归枚举 棋盘问题 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24813   Accepted: 12261 Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. Input 输入含有多组测试数据. 每组数据的第一行

hduoj2553——N皇后问题,dfs回溯

hduoj 2553  dfs,回溯 N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10297    Accepted Submission(s): 4634 Problem Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的

hdu4499 Cannon (DFS+回溯)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4499 Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 363    Accepted Submission(s): 214 Problem Des

HDU 1015 dfs回溯

题目真长.....看了好长时间才看懂.. 就是给你一个32位数字和一个最多15个字符的字符串,从字符串中选出5个字符,若满足题中给的那个式子,输出字典序最大的那5个字符,若不满足,输出no solution. 为了解决字典序问题,在输入字符串后,把字符串按从大到小排一下序,搜索一下若满足条件输出即可. 贴代码. 1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 #include <

CodeForces 550B Preparing Olympiad(DFS回溯+暴力枚举)

[题目链接]:click here~~ [题目大意] 一组题目的数目(n<=15),每一个题目有对应的难度,问你选择一定的题目(大于r个且小于l个)且选择后的题目里最小难度与最大难度差不小于x,求选择方案数. [解题思路]: DFS+回溯. 先发一发比較拙的代码: #include <bits/stdc++.h> using namespace std; const int N=1e5+10; int num[N],mum[N]; int n,m,q,t,l,r; int top,ans