zoukankan      html  css  js  c++  java
  • hdu1015 Safecracker

     1 /*
     2  *  dfs 
     3  *    esc <------> capslock
     4  */
     5 
     6 #include <iostream>
     7 #include <string>
     8 #include <algorithm>
     9 #include <cstdio>
    10 using namespace std;
    11 int num[20],n;
    12 int l;
    13 bool success;
    14 string haha;
    15 bool vit[20];
    16 bool cmp(int a,int b){
    17     return a>b;
    18 }
    19 
    20 void init(string str){
    21     int i,j;
    22     l=str.length();
    23     success=0;
    24     for(i=0;i<l;++i){
    25         num[i]=str[i]-'A'+1;
    26         vit[i]=0;
    27     }
    28     sort(num,num+l,cmp);
    29     return ;
    30 }
    31 int count(string strr){
    32     int i,cnt=0;
    33     int str[5];
    34     for(i=0;i<5;++i)    str[i]=strr[i]-'A'+1;
    35     cnt=str[0]-str[1]*str[1]+str[2]*str[2]*str[2]-str[3]*str[3]*str[3]*str[3]+str[4]*str[4]*str[4]*str[4]*str[4];
    36     return cnt;
    37 }
    38 void dfs(int step,string res){
    39     int i,j;
    40     int temp;
    41     string str="0";
    42     if(success) return ;
    43     for(i=0;i<l;++i){
    44         str="0";
    45         if(vit[i]==1) continue;
    46         if(res=="0") str[0]=num[i]+'A'-1;
    47         else{
    48             str[0]=num[i]+'A'-1;
    49             str=res+str;
    50         }
    51         if(step==1) {
    52             vit[i]=1;
    53             dfs(2,str);
    54             vit[i]=0;
    55         }else if(step==2){
    56             vit[i]=1;
    57             dfs(3,str);
    58             vit[i]=0;
    59         }else if(step==3){
    60             vit[i]=1;
    61             dfs(4,str);
    62             vit[i]=0;
    63         }else if(step==4){
    64             vit[i]=1;
    65             dfs(5,str);
    66             vit[i]=0;
    67         }else if(step==5){
    68 //            cout<<str<<endl;
    69             temp=count(str);
    70             if(temp==n) {haha=str;success=1;return;}
    71         }
    72     }
    73     return ;
    74 }
    75 
    76 
    77 int main(){
    78     int i,j,t;
    79     string str;
    80     while(cin>>n>>str){
    81         if(n==0&&str=="END") break;
    82         t=0;
    83         init(str);
    84         dfs(1,"0");
    85         if(success==1){
    86             cout<<haha<<endl;
    87         }else cout<<"no solution
    ";
    88     }
    89     return 0;
    90 }
    91 
    92 
    93         
  • 相关阅读:
    leetcode116 Populating Next Right Pointers in Each Node
    leetcode171 Excel Sheet Column Number
    Js 之生成二维码插件(jquery.qrcode.js)
    Js 之cookie插件(jquery.cookie.js)
    Js 之常见手势操作插件 Hammer.js
    快速购买系统
    PHP + CI框架 + AdminLITE权限管理系统
    PHP 之查找字符串位置函数封装
    利用layer制作好看的弹出框
    PHP+CI框架+Memcache集成
  • 原文地址:https://www.cnblogs.com/symons1992/p/3330565.html
Copyright © 2011-2022 走看看