-
Notifications
You must be signed in to change notification settings - Fork 0
/
crackingthesafe.cpp
57 lines (56 loc) · 1.28 KB
/
crackingthesafe.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
string ret;
int dfs(int i,int n,int k,int val,string& s,map<string,bool>& memo)
{
if(i>=val)
{
ret=s;
return 0;
}
else
{
int q=1000;
if(i<n)
for(int j=0;j<k;++j)
{
s.push_back('0'+j);
q=min(q,dfs(i+1,n,k,val,s,memo));
if(q==0)
return 0;
s.pop_back();
}
else
{
if(i==n)
memo[s]=true;
string t;
if(n>1)
t=s.substr(i-n+1);
for(int j=0;j<k;++j)
{
t.push_back(j+'0');
if(memo.find(t)==memo.end()||memo[t]==false)
{
memo[t]=true;
s.push_back(j+'0');
q=min(q,dfs(i+1,n,k,val,s,memo));
if(q==0)
return 0;
s.pop_back();
memo[t]=false;
}
t.pop_back();
}
if(i==n)
memo[s]=false;
}
return q;
}
}
string crackSafe(int n, int k)
{
int val=pow(k,n)+n-1;
map<string,bool> memo;
string s;
cout<<dfs(0,n,k,val,s,memo);
return ret;
}