标签:搜索 dfs acm algorithm string
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1015
深搜简单题
代码如下:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; string a; int t; int v,w,x,y,z; int vis[20]; int b[6]; int flag; int cmp(int a,int b) { return a>b; } void dfs(int k) { int i; if(k==5) { if(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]==t) { flag=1; } return ; } for(i=0;i<a.size();i++) { if(!vis[i]&&!flag) { vis[i]=1; b[k]=a[i]-‘A‘+1; dfs(k+1); vis[i]=0; } } return; } int main() { int i; while(cin>>t>>a,t!=0||a!="END") { flag=0; memset(vis,0,sizeof(vis)); sort(a.begin(),a.end(),cmp); dfs(0); if(flag) { for(i=0;i<5;i++) printf("%c",b[i]-1+‘A‘); cout<<endl; } else cout<<"no solution"<<endl; } return 0; }
HDU-1015-Safecracker,布布扣,bubuko.com
标签:搜索 dfs acm algorithm string
原文地址:http://blog.csdn.net/pwaiyuan/article/details/24967663