Guest User

Android OS Pattern Screen Lock Simulator

a guest
Jun 1st, 2011
794
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1. // 2011 Aret Carlsen
  2.  
  3. // Android pattern screen lock simulator for a certain subset of users.
  4. // Implements four constraints:
  5. // 1.) No dot can be visited twice.
  6. // 2.) Path must be at least four dots long. (OS enforces.)
  7. // 3.) Only adjacent dots may be next in path.
  8. // 3.) User never crosses their own line. (Implied by constraint (3).)
  9.  
  10. #include <stdio.h>
  11. #include <stdint.h>
  12. #include <stdlib.h>
  13.  
  14. #include <list>
  15.  
  16. struct Dot {
  17.   int8_t x;
  18.   int8_t y;
  19.  
  20.   Dot(int8_t new_x, int8_t new_y)
  21.   : x(new_x), y(new_y)
  22.   { }
  23.  
  24.   bool is_adjacent_to(Dot* otherDot){
  25.     return ( (abs(otherDot->x - x) < 2) && (abs(otherDot->y - y) < 2) );
  26.   }
  27. };
  28.  
  29. typedef std::list<Dot*> DotList_t;
  30.  
  31. unsigned int search_path(DotList_t unvisited_dots, DotList_t visited_dots = DotList_t(), Dot* newly_arrived_dot = NULL){
  32.   // Consraint (1).
  33.   if(newly_arrived_dot){
  34.     visited_dots.push_back(newly_arrived_dot);
  35.     unvisited_dots.remove(newly_arrived_dot);
  36.   }
  37.  
  38.   unsigned int path_count = 0;
  39.   for(DotList_t::iterator it = unvisited_dots.begin();
  40.       it != unvisited_dots.end();
  41.       it++){
  42.   // Constraints (3) and (4): Only adjacent dots can be next in the path.
  43.     if(newly_arrived_dot && !newly_arrived_dot->is_adjacent_to(*it))
  44.       continue;
  45.   // Constraint (2): This is only a complete path if at least 3 dots were already visited.
  46.     if(visited_dots.size() >= 3)
  47.       path_count++;
  48.     path_count += search_path(unvisited_dots, visited_dots, *it);
  49.   }
  50.  
  51.   return path_count;
  52. }
  53.  
  54. int main(){
  55.   DotList_t dots;
  56.  
  57.   Dot* testDot = NULL;
  58.   // Generate dot graph.
  59.   for(uint8_t x = 0; x < 3; x++){
  60.     for(uint8_t y = 0; y < 3; y++){
  61.       // MEMORY LEAK
  62.       dots.push_back(new Dot(x, y));
  63.       //if((x == 1) && (y == 1))
  64.       //  testDot = dots.back();
  65.     }
  66.   }
  67.  
  68.   unsigned int path_count = 0;
  69.   if(testDot){
  70.     // Test for a specific initial dot:
  71.     fprintf(stderr, "Given initial dot: x=%d, y=%d\n", testDot->x, testDot->y);
  72.     path_count = search_path(dots, DotList_t(), testDot);
  73.   }else{
  74.   // Simulate users who do not cross their own lines or move to non-adjacent
  75.   // dots. The former constraint is implied by the latter, as it is impossible
  76.   // to cross one's own lines without moving to a non-adjacent dot.
  77.     path_count = search_path(dots);
  78.   }
  79.   fprintf(stdout, "Total path count: %u\n", path_count);
  80.  
  81.   return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment