稱號(hào): hdoj 1226 超級(jí)password?
分析:這題屬于隱式圖搜索,狀態(tài)不是非常明顯,須要自己建立。
事實(shí)上搜索說(shuō)白了就是暴力。
這個(gè)題目就是,首先對(duì)給出的能夠組成的全部的數(shù)依次枚舉。長(zhǎng)度從小到大。
比方第一組例子,由于0不能出如今首位。那么我們枚舉首位為1 和 7 看看漫步滿足,
滿足的話枚舉第二位10 11 17 以及 70 71 77 順便保存他們?nèi)∮?n 之后的值,這樣就能夠剪枝,搜索過(guò)的就不用反復(fù)搜索了。
要求最早出現(xiàn)的BFS就可以,第一個(gè)搜到的就是。
注意長(zhǎng)度不大于500
AC代碼:
#include <cstdio> #include <algorithm> #include <cstring> #include <string> #include <iostream> #include <vector> #include <cmath> #include <queue> using namespace std; const int inf = 0x3f3f3f3f; const int N = 15; int a[20]; int flag[60000]; struct Node { string s; int ss; }; Node ans; int n,c,m; char solve(int x) { if(x>=0 && x<=9) return x+'0'; return x-10+'A'; } bool BFS() { memset(flag,0,sizeof(flag)); queue<Node> q; Node now,next; for(int i=0;i<m;i++) { if(a[i]!=0) { now.s = solve(a[i]); now.ss = (a[i]%n); if(now.ss == 0) { ans = now; return true; } if(flag[now.ss]==0) { flag[now.ss] = 1; q.push(now); } } } while(!q.empty()) { now = q.front(); q.pop(); //cout<<now.s<<" "<<now.ss<<endl; if(now.ss == 0) { ans = now; return true; } for(int i=0;i<m;i++) { next.s= now.s+solve(a[i]); next.ss = (now.ss*c+a[i])%n; // cout<<"NEXT:"<<next.s<<" "<<next.ss<<" "<<a[i]<<endl; if(flag[next.ss]==0) { flag[next.ss] = 1; q.push(next); } } } return false; } int main() { //freopen("Input.txt","r",stdin); int T; scanf("%d",&T); while(T--) { scanf("%d%d%d",&n,&c,&m); for(int i=0;i<m;i++) { char c[10]; scanf("%s",c); if(c[0]>='A' && c[0]<='F') a[i] = (c[0]-'A')+10; else a[i] = c[0]-'0'; } sort(a,a+m); if(n==0) { if(a[0]==0) puts("0"); else puts("give me the bomb please"); continue; } if(BFS() && ans.s.size()<=500) cout<<ans.s<<endl; else puts("give me the bomb please"); } return 0; }
版權(quán)聲明:本文博客原創(chuàng)文章。博客,未經(jīng)同意,不得轉(zhuǎn)載。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
