hdu1015

Posted bearcarl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu1015相关的知识,希望对你有一定的参考价值。

解题思路:1 五重循环

2 dfs, 就写下这个代码了(参考了discuss 中的解题)

#include<bits/stdc++.h>
using namespace std;


int s[20], vis[20], ans[10];
int n;
int len, ok = 0;

int check() {
    int a = ans[0], b = ans[1], c = ans[2], d = ans[3], e = ans[4];
    if(a - b*b + c*c*c - d*d*d*d + e*e*e*e*e == n)
        return 1;
    return 0;
        
}

int dfs(int d) {
    if(d == 5) {
        if(check())
            ok = 1;
        return 0;
    }
    for(int i = 0; i < len; i++) {
        if(!vis[i]) {
            vis[i] = 1;
            ans[d] = s[i];
            dfs(d + 1);
            if(ok == 1)
                return 0;
            vis[i] = 0;
        }
    }    
}

int main() {
    //freopen("ain.txt", "r", stdin);
    char str[20];    
    while(scanf("%d%s", &n, str) && strcmp(str, "END")) {
        ok = 0;
        memset(vis, 0, sizeof(vis));
        len = strlen(str);
        for(int i = 0; i < len; i++)
            s[i] = str[i] - A + 1;
        sort(s, s + len, greater<int>());
        dfs(0);
        if(ok) {
            for(int k = 0; k < 5; k++)
                printf("%c", ans[k] + A - 1); 
            cout << endl;
        }
        else 
            cout << "no solution" << endl;
    }
    return 0;
}

 

以上是关于hdu1015的主要内容,如果未能解决你的问题,请参考以下文章

hdu 1015 Safecracker

hdu 1015 Safecracker

[HDU 1015] Safecracker

hdu 1015(DFS)

hdu1015

HDU 1015 Safecracker