HDU 1015 Safecracker

來源:互聯網
上載者:User

標籤:

解題思路:這題相當詭異,範例沒過,交了,A了,呵呵,因為理論上是可以通過的,所以

     我交了一發,然後就神奇的過了。首先要看懂題目。

 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cmath> 5 using namespace std; 6 const int maxn = 15; 7 int vis[maxn], A[maxn], flag, len, sum, p[maxn], n; 8 char str[maxn]; 9 10 int cmp(int x, int y)11 {12     return x > y;13 }14 15 void DFS(int cnt)16 {17     if(cnt == 5)18     {19         sum = pow(p[0], 1) - pow(p[1],2) + pow(p[2],3) - pow(p[3],4) + pow(p[4],5);20         if(sum == n) flag = 1; //搜到合格就跳出。21         return ;22     }23 24     for(int i = 0; i < len; i++)25     {26         //每個點有選和不選兩種情況,所以很自然想到回溯。27         if(!vis[i])28         {29             p[cnt] = A[i];//儲存路徑30             vis[i]  = 1;31             DFS(cnt + 1); 32             vis[i] = 0;33         }34         if(flag) return ; //這一步必不可少,表明搜到合格就要立即跳出。35     }36     return ;37 }38 39 int main()40 {41     while(~scanf("%d", &n))42     {43         memset(vis, 0, sizeof(vis));44         scanf("%s", str);45         if(strcmp(str, "END") == 0) break;46         len = strlen(str);47         for(int i = 0; i < len; i++) A[i] = str[i] - ‘A‘ + 1;48         sort(A, A + len, cmp); //直接從大到下排序,搜到第一個合格直接跳出。49 50         flag = 0;51         DFS(0);52         //flag等於1表示搜到了53         if(flag)    for(int i = 0; i < 5; i++) printf("%c", p[i] + 64); 54         else printf("no solution");55         printf("\n");56     }57     return 0;58 }
View Code

 

HDU 1015 Safecracker

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.