1. 程式人生 > >HDU 1015 Safecracker【數值型DFS】

HDU 1015 Safecracker【數值型DFS】

print position bili iostream floor always mem csdn aps

Safecracker

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3966 Accepted Submission(s): 2028


Problem Description === Op tech briefing, 2002/11/02 06:42 CST ===
"The item is locked in a Klein safe behind a painting in the second-floor library. Klein safes are extremely rare; most of them, along with Klein and his factory, were destroyed in World War II. Fortunately old Brumbaugh from research knew Klein‘s secrets and wrote them down before he died. A Klein safe has two distinguishing features: a combination lock that uses letters instead of numbers, and an engraved quotation on the door. A Klein quotation always contains between five and twelve distinct uppercase letters, usually at the beginning of sentences, and mentions one or more numbers. Five of the uppercase letters form the combination that opens the safe. By combining the digits from all the numbers in the appropriate way you get a numeric target. (The details of constructing the target number are classified.) To find the combination you must select five letters v, w, x, y, and z that satisfy the following equation, where each letter is replaced by its ordinal position in the alphabet (A=1, B=2, ..., Z=26). The combination is then vwxyz. If there is more than one solution then the combination is the one that is lexicographically greatest, i.e., the one that would appear last in a dictionary."

v - w^2 + x^3 - y^4 + z^5 = target

"For example, given target 1 and letter set ABCDEFGHIJKL, one possible solution is FIECB, since 6 - 9^2 + 5^3 - 3^4 + 2^5 = 1. There are actually several solutions in this case, and the combination turns out to be LKEBA. Klein thought it was safe to encode the combination within the engraving, because it could take months of effort to try all the possibilities even if you knew the secret. But of course computers didn‘t exist then."

=== Op tech directive, computer division, 2002/11/02 12:30 CST ===

"Develop a program to find Klein combinations in preparation for field deployment. Use standard test methodology as per departmental regulations. Input consists of one or more lines containing a positive integer target less than twelve million, a space, then at least five and at most twelve distinct uppercase letters. The last line will contain a target of zero and the letters END; this signals the end of the input. For each line output the Klein combination, break ties with lexicographic order, or ‘no solution‘ if there is no correct combination. Use the exact format shown below." Sample Input 1 ABCDEFGHIJKL 11700519 ZAYEXIWOVU 3072997 SOUGHT 1234567 THEQUICKFROG 0 END Sample Output LKEBA YOXUZ GHOST no solution 【參考】:http://blog.csdn.net/pengwill97/article/details/54882698 【題意】:在字符串中找五個字符可以滿足上面的式子。存在多組,輸出字典序最大的,沒有的話,輸出no solution。 【分析】:下面的for循環非常像dfs地圖的四向搜索
,但是len指的是數據中給定的字母序列的長度。那麽就指,下一個搜索的目標要在所有的字母序列中找,哪些可以作為搜索目標呢?首先就是這個字母沒有被選定過(!visit[i] )並且現在解還沒有找到(!judge)。 進入if後,首先把數組b中depth的位置賦值為a[i],代表我數組b選定了你a中i這個位置的數字(或者說是字母),並且在visit中置為選擇過了,dfs(depth+1)繼續尋找下一個位置的搜索目標。別忘了最後把visit[i]置為0(無後效性)。相比前邊2題,此題的收獲就在於:原先的地圖四向搜索,也可以變成這樣從幾個字符,數字中尋找可行的解。活學活用,非常重要呀!不要忘記題目要求:找到一組字典序最大的解即可。首先是遞歸邊界,如果找到了解(judge為真),停止遞歸;亦或是當depth為5(代表找到了5個數字)的時候,用check函數判斷一下是否滿足題目要求。若都不滿足遞歸邊界,繼續搜索。 【代碼】: 技術分享
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char a[15], b[10];
int visit[15];
int n,num,len;
bool judge = false;

bool cmp(char a, char b)
{
    return a>b;
}
void init()
{
    len = strlen(a);
    judge = false;
    memset(visit,0,sizeof(visit));
    sort(a,a+len,cmp);
    for(int i = 0;i<len;++i){
        a[i] = a[i] -A + 1;
    }
}
void lnit()
{
    for(int i = 0;i<5;++i)
        b[i] = b[i]+A-1;
}
bool check()
{
    if(n == b[0] - b[1]*b[1] + b[2]*b[2]*b[2] - b[3]*b[3]*b[3]*b[3] + b[4]*b[4]*b[4]*b[4]*b[4]){
        judge = true;
        return true;
    }else return false;
}
void dfs(int depth)
{
    //遞歸邊界
    if(judge) return;
    if(depth == 5) {check(); return;}
    for(int i = 0;i<len;++i){
        if(!visit[i]&&!judge){
            b[depth] = a[i];
            visit[i] = 1;
            dfs(depth+1);
            visit[i] = 0;
        }
    }
}
int main()
{

    //freopen("in.txt","r",stdin);
    while(scanf("%d %s",&n,a)&& !(n==0 && !strcmp(a,"END"))){
        init();
        dfs(0);
        lnit();
        if(judge)printf("%s\n",b);
        else printf("no solution\n");
    }
    return 0;
}
數值型DFS

HDU 1015 Safecracker【數值型DFS】