您的位置:首页 > Web前端

hdoj 1015 Safecracker(窃贼) Dfs

2017-08-07 18:25 232 查看

题目真是啰嗦=-=

(A=1, B=2, ..., Z=26)


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

输入:tarrget+一段字符串
输出:满足条件的字符串(多个按字典序)

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define res(v,w,x,y,z) v-w*w+x*x*x-y*y*y*y+z*z*z*z*z
using namespace std;
int num[5][30],s[30];
long long n;
char a[50];
bool vis[30];
bool cmp(char a,char b)
{
return !(a<b);
}
bool dfs( int k)
{
if(k==5)
{
long long temp=res(s[0],s[1],s[2],s[3],s[4]);
if(temp==n)
return true;
return false;
}
else
{
for(int i=0;i<strlen(a);i++)
{
if(vis[i]==false)		//标记
{
vis[i]=true;
s[k]=a[i];
if(dfs(k+1))
return true;
vis[i]=false;
}
}
}
return false;
}
int main()
{
while(scanf("%lld %s",&n,a))
{
if(n==0&&strcmp(a,"END")==0)
break;
for(int i=0;i<strlen(a);i++)
a[i]=a[i]-'A'+1;		//转换成整数类型
sort(a,a+strlen(a),cmp);	//排序
memset(s,0,sizeof(s));
memset(vis,false,sizeof(vis));
if(dfs(0))
for(int i=0;i<5;i++)
printf("%c",s[i]+'A'-1);
else
printf("no solution");
printf("\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdoj acm