Docsity

Brute Force Solution to Push Button Lock - Lecture Notes | COMP 157, Study notes of Algorithms and Programming

Toggle sidebar
bg1
COMP157๎˜ƒFall๎˜ƒ2007๎˜ƒbrute๎˜ƒforce๎˜ƒexamples๎˜ƒSept.๎˜ƒ17๎˜ƒ
Brute๎˜ƒforce๎˜ƒsolution๎˜ƒto๎˜ƒPush๎˜ƒButton๎˜ƒLock:๎˜ƒ
A๎˜ƒTest๎˜ƒis๎˜ƒone๎˜ƒpossible๎˜ƒassignment๎˜ƒof๎˜ƒbuttons๎˜ƒร†๎˜ƒcombinations:๎˜ƒ
๎˜ƒ
๎˜ƒ
๎˜ƒ
๎˜ƒ
Recursively๎˜ƒbuild๎˜ƒall๎˜ƒpossible๎˜ƒbutton๎˜ƒร†๎˜ƒcombination๎˜ƒassignments๎˜ƒ(including๎˜ƒinvalid๎˜ƒones).๎˜ƒ
๎˜ƒ
๎˜ƒ
๎˜ƒ
๎˜ƒ
๎˜ƒ
๎˜ƒ
๎˜ƒ
๎˜ƒ
๎˜ƒ
๎˜ƒ
๎˜ƒ
Check๎˜ƒif๎˜ƒa๎˜ƒbutton๎˜ƒร†๎˜ƒcombination๎˜ƒassignment๎˜ƒis๎˜ƒvalid:๎˜ƒ
๎˜ƒ
๎˜ƒ
๎˜ƒ
๎˜ƒ
๎˜ƒ
๎˜ƒ
๎˜ƒ
struct Test
{
int x[11];
int n;
Test(int _n) { n = _n; }
Test(const Test& o) { n=o.n; for (int i=0; i<o.n; i++) x[i]=o.x[i]; }
};๎˜ƒ
void make_tests(int n, int max, vector<Test>& v)
{
if (n==1)
{
for (int j=0; j<=max; j++)
{
Test t(1); t.x[0] = j;
v.push_back(t);
}
return;
}
vector<Test> v2;
make_tests(n-1,max,v2);
for (unsigned int i=0; i<v2.size(); i++)
{
Test copy(v2[i]);
copy.n++;
for (int j=0; j<=max; j++)
{
copy.x[n-1]=j;
v.push_back(copy);
}
}
}๎˜ƒ
bool checkTest(Test& test)
{
int combos[11];
int n = 0;
int i;
for (i=0; i<test.n; i++)
if (test.x[i]<test.n) { combos[n] = test.x[i]; n++; }
sort(combos, n);
if (combos[0] != 0) return false;
for (i=0; i<n-1; i++) if ((combos[i+1]-combos[i])>1) return false;
return true;
}๎˜ƒ
pf3

Partial preview of the text

Download Brute Force Solution to Push Button Lock - Lecture Notes | COMP 157 and more Study notes Algorithms and Programming in PDF only on Docsity!

Brute force solution to Push Button Lock: A Test is one possible assignment of buttons ร† combinations: Recursively build all possible button ร† combination assignments (including invalid ones). Check if a button ร† combination assignment is valid: struct Test { int x[11]; int n; Test(int _n) { n = _n; } Test(const Test& o) { n=o.n; for (int i=0; i<o.n; i++) x[i]=o.x[i]; } }; void make_tests(int n, int max, vector& v) { if (n==1) { for (int j=0; j<=max; j++) { Test t(1); t.x[0] = j; v.push_back(t); } return; } vector v2; make_tests(n-1,max,v2); for (unsigned int i=0; i<v2.size(); i++) { Test copy(v2[i]); copy.n++; for (int j=0; j<=max; j++) { copy.x[n-1]=j; v.push_back(copy); } } } bool checkTest(Test& test) { int combos[11]; int n = 0; int i; for (i=0; i<test.n; i++) if (test.x[i]<test.n) { combos[n] = test.x[i]; n++; } sort(combos, n); if (combos[0] != 0) return false; for (i=0; i<n-1; i++) if ((combos[i+1]-combos[i])>1) return false; return true;

Solve the problem for a specific number of buttons: Official judgeโ€™s solution to Push Button Lock (not brute force?): int solve(int num_buttons) { // make vector containing all possible assignments vector v; make_tests(num_buttons, num_buttons, v); // count the number of valid assignments int count = 0; for (unsigned int i=0; i<v.size(); i++) if (checkTest(v[i])) count++; return count; } unsigned int max_move = 0; int savemove[16384]; unsigned long count(unsigned int used) { unsigned int move; unsigned int nMove = 0; if(savemove[used] > 0){ return(savemove[used]); } for (move = 1; move < max_move; move++) { if (move & used) { continue; } nMove++; nMove += count(used | move); } savemove[used] = nMove; return(nMove); } int solve(int num_buttons) { max_move = 1<<num_buttons; // 2^n ::memset(&(savemove[0]), '\0', sizeof(savemove)); return count(0);