Combination Lock

链接

分析:在无限改之后终于过了,里面要处理的细节比较多,最后可以抽象成字符串,用set来去重

  1 /*
  2     PROB:combo
  3     ID:wanghan
  4     LANG:C++
  5 */
  6 #include "iostream"
  7 #include "cstdio"
  8 #include "cstring"
  9 #include "string"
 10 #include "vector"
 11 #include "set"
 12 using namespace std;
 13 int n,num;
 14 vector<int> Next;
 15 set<string> h;
 16 vector<int> p[4];
 17 vector<int> q[4];
 18 int a1,a2,a3,b1,b2,b3;
 19 void init()
 20 {
 21     for(int i=1;i<=n;i++)
 22         Next.push_back(i);
 23     for(int i=1;i<=n;i++)
 24         Next.push_back(i);
 25     if(Next.size()<=5){
 26         for(int i=1;i<=5;i++)
 27             Next.push_back(1);
 28     }
 29 }
 30 string Rev(int num){   //转化成字符串
 31     string res="";
 32     while(num){
 33         int mod=num%10;
 34         res+=(mod+'0');
 35         num/=10;
 36     }
 37     int i=0,j=res.length()-1;
 38     while(i<j){
 39         swap(res[i],res[j]);
 40         i++,j--;
 41     }
 42     return res;
 43 }
 44 void solve1(int ans,int flag){
 45     int pos;
 46     for(int i=0;i<Next.size();i++){
 47         if(ans==Next[i]){
 48             pos=i; break;
 49         }
 50     }
 51     if(pos<2) pos+=max(n,2);
 52     p[flag].push_back(Next[pos-2]);
 53     p[flag].push_back(Next[pos-1]);
 54     p[flag].push_back(Next[pos]);
 55     p[flag].push_back(Next[pos+1]);
 56     p[flag].push_back(Next[pos+2]);
 57     /*cout<<flag<<":"<<endl;
 58     for(int i=0;i<p[flag].size();i++)
 59         cout<<p[flag][i]<<endl;
 60     cout<<endl;*/
 61 }
 62 void solve2(int ans,int flag){
 63     int pos;
 64     for(int i=0;i<Next.size();i++){
 65         if(ans==Next[i]){
 66             pos=i; break;
 67         }
 68     }
 69     if(pos<2) pos+=max(2,n);
 70     q[flag].push_back(Next[pos-2]);
 71     q[flag].push_back(Next[pos-1]);
 72     q[flag].push_back(Next[pos]);
 73     q[flag].push_back(Next[pos+1]);
 74     q[flag].push_back(Next[pos+2]);
 75     /*cout<<flag<<":"<<endl;
 76     for(int i=0;i<q[flag].size();i++)
 77         cout<<q[flag][i]<<endl;
 78     cout<<endl;*/
 79 }
 80 int main()
 81 {
 82     freopen("combo.in","r",stdin);
 83     freopen("combo.out","w",stdout);
 84     cin>>n;
 85     init();
 86     cin>>a1>>a2>>a3>>b1>>b2>>b3;
 87     solve1(a1,0);
 88     solve1(a2,1);
 89     solve1(a3,2);
 90     solve2(b1,0);
 91     solve2(b2,1);
 92     solve2(b3,2);
 93     for(int i=0;i<p[0].size();i++){
 94         for(int j=0;j<p[1].size();j++){
 95             for(int k=0;k<p[2].size();k++){
 96                 string tt="";
 97                 tt+=Rev(p[0][i]);
 98                 tt+=Rev(p[1][j]);
 99                 tt+=Rev(p[2][k]);
100                 h.insert(tt);
101             }
102         }
103     }
104     for(int i=0;i<q[0].size();i++){
105         for(int j=0;j<q[1].size();j++){
106             for(int k=0;k<q[2].size();k++){
107                 string yy="";
108                 yy+=Rev(q[0][i]);
109                 yy+=Rev(q[1][j]);
110                 yy+=Rev(q[2][k]);
111                 h.insert(yy);
112             }
113         }
114     }
115     set<string>::iterator it;
116     /*for(it=h.begin();it!=h.end();it++)
117         cout<<*it<<endl;*/
118     cout<<h.size()<<endl;
119     return 0;
120 }
View Code

 

转载于:https://www.cnblogs.com/wolf940509/p/7041235.html

Sure, I can help you with that! Here's a C++ program that uses recursion and backtracking to find the correct lock combination: ```cpp #include <iostream> #include <vector> #include <cstdlib> #include <ctime> using namespace std; const int NUM_DIGITS = 5; void generateRandomCombination(vector<int>& combination) { for (int i = 0; i < NUM_DIGITS; i++) { combination.push_back(rand() % 10); } } void printCombination(const vector<int>& combination) { for (int i = 0; i < NUM_DIGITS; i++) { cout << combination[i]; } cout << endl; } bool tryCombination(vector<int>& currentCombination, const vector<int>& targetCombination, int digitIndex) { if (digitIndex == NUM_DIGITS) { return currentCombination == targetCombination; } for (int i = 0; i < 10; i++) { currentCombination[digitIndex] = i; if (tryCombination(currentCombination, targetCombination, digitIndex + 1)) { return true; } } return false; } int main() { srand(time(0)); vector<int> targetCombination; generateRandomCombination(targetCombination); cout << "Target combination: "; printCombination(targetCombination); vector<int> currentCombination(NUM_DIGITS, 0); if (tryCombination(currentCombination, targetCombination, 0)) { cout << "Found the correct combination: "; printCombination(currentCombination); } else { cout << "Unable to find the correct combination." << endl; } return 0; } ``` The `generateRandomCombination` function generates a random lock combination and stores it in a vector. The `printCombination` function simply prints out a given combination. The `tryCombination` function takes in the current combination being tested, the target combination to be found, and the current digit index that is being tested. It recursively tries all possible combinations of digits for the current index, and returns true if the correct combination is found. In the `main` function, we first generate a random target combination, print it out, and initialize the current combination to all zeros. We then call `tryCombination` with the current combination, target combination, and starting index of 0. If the correct combination is found, we print it out. Otherwise, we print a message saying that we were unable to find the correct combination. Here are three example recursive level calls for the recursion tree call with input values: ``` tryCombination({0, 0, 0, 0, 0}, {3, 2, 5, 8, 1}, 0) ├──tryCombination({0, 0, 0, 0, 3}, {3, 2, 5, 8, 1}, 1) │ ├──tryCombination({0, 0, 0, 2, 3}, {3, 2, 5, 8, 1}, 2) │ │ ├──tryCombination({0, 0, 5, 2, 3}, {3, 2, 5, 8, 1}, 3) │ │ │ ├──tryCombination({3, 0, 5, 2, 3}, {3, 2, 5, 8, 1}, 4) // returns true │ │ │ └──tryCombination({4, 0, 5, 2, 3}, {3, 2, 5, 8, 1}, 4) │ │ └──tryCombination({0, 1, 5, 2, 3}, {3, 2, 5, 8, 1}, 3) │ │ ├──tryCombination({3, 1, 5, 2, 3}, {3, 2, 5, 8, 1}, 4) // returns true │ │ └──tryCombination({4, 1, 5, 2, 3}, {3, 2, 5, 8, 1}, 4) │ └──tryCombination({0, 2, 5, 2, 3}, {3, 2, 5, 8, 1}, 3) │ ├──tryCombination({3, 2, 5, 2, 3}, {3, 2, 5, 8, 1}, 4) // returns true │ └──tryCombination({4, 2, 5, 2, 3}, {3, 2, 5, 8, 1}, 4) ├──tryCombination({0, 0, 0, 0, 4}, {3, 2, 5, 8, 1}, 1) │ ├──tryCombination({0, 0, 0, 2, 4}, {3, 2, 5, 8, 1}, 2) │ │ ├──tryCombination({0, 0, 5, 2, 4}, {3, 2, 5, 8, 1}, 3) │ │ │ ├──tryCombination({3, 0, 5, 2, 4}, {3, 2, 5, 8, 1}, 4) // returns true │ │ │ └──tryCombination({4, 0, 5, 2, 4}, {3, 2, 5, 8, 1}, 4) │ │ └──tryCombination({0, 1, 5, 2, 4}, {3, 2, 5, 8, 1}, 3) │ │ ├──tryCombination({3, 1, 5, 2, 4}, {3, 2, 5, 8, 1}, 4) // returns true │ │ └──tryCombination({4, 1, 5, 2, 4}, {3, 2, 5, 8, 1}, 4) │ └──tryCombination({0, 2, 5, 2, 4}, {3, 2, 5, 8, 1}, 3) │ ├──tryCombination({3, 2, 5, 2, 4}, {3, 2, 5, 8, 1}, 4) // returns true │ └──tryCombination({4, 2, 5, 2, 4}, {3, 2, 5, 8, 1}, 4) └──tryCombination({0, 0, 0, 0, 5}, {3, 2, 5, 8, 1}, 1) ├──tryCombination({0, 0, 0, 2, 5}, {3, 2, 5, 8, 1}, 2) │ ├──tryCombination({0, 0, 5, 2, 5}, {3, 2, 5, 8, 1}, 3) │ │ ├──tryCombination({3, 0, 5, 2, 5}, {3, 2, 5, 8, 1}, 4) // returns true │ │ └──tryCombination({4, 0, 5, 2, 5}, {3, 2, 5, 8, 1}, 4) │ └──tryCombination({0, 1, 5, 2, 5}, {3, 2, 5, 8, 1}, 3) │ ├──tryCombination({3, 1, 5, 2, 5}, {3, 2, 5, 8, 1}, 4) // returns true │ └──tryCombination({4, 1, 5, 2, 5}, {3, 2, 5, 8, 1}, 4) └──tryCombination({0, 2, 5, 2, 5}, {3, 2, 5, 8, 1}, 3) ├──tryCombination({3, 2, 5, 2, 5}, {3, 2, 5, 8, 1}, 4) // returns true └──tryCombination({4, 2, 5, 2, 5}, {3, 2, 5, 8, 1}, 4) ``` In this example, we're trying to find the correct combination for the lock with the target combination {3, 2, 5, 8, 1}. The recursion tree shows the different combinations being tried at each recursive call. At each level, the function tries all possible digits for the current index, and recursively calls itself with the next digit index. If a combination is found that matches the target combination, the function returns true and stops recursing.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值