Advertisement
HjHimansh

Android Unlock Pattern - HJ

Jul 26th, 2020
1,380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | None | 0 0
  1. class Solution {
  2.    
  3.     int minL, maxL;
  4.     unordered_map<int, unordered_map<int, int>> passingThrough;
  5.  public :
  6.  
  7.      
  8.     bool isPermissible(int prevNum, int currNum, vector<bool> &visited){
  9.    
  10.         int a = min(prevNum, currNum);
  11.         int b = max(prevNum, currNum);
  12.        
  13.         if(passingThrough[a][b]==-1){
  14.             //i.e no number is on the line crossing and b
  15.             return true;
  16.         }
  17.        
  18.         return visited[passingThrough[a][b]];
  19.     }
  20.  
  21.     //returns number of combinations, we're currently on cth number
  22.     int h_numberOfPatterns(int c, int prevNum, vector<bool> &visited){
  23.         if(c == maxL + 1){
  24.             return 0;
  25.         }
  26.         int answer = 0;
  27.        
  28.         for(int i=1; i<=9; i++){
  29.             if(!visited[i] && isPermissible(prevNum, i, visited)){
  30.                 if(minL <= c){ 
  31.                   answer += 1;
  32.                 }
  33.                 visited[i] = true;
  34.                 answer += h_numberOfPatterns(c+1, i, visited);
  35.                 visited[i] = false;
  36.             }
  37.         }
  38.        
  39.         return answer;
  40.     }
  41.  
  42.      int numberOfPatterns( int m, int n) {
  43.         minL = m;
  44.         maxL = n;
  45.        
  46.         passingThrough[1][2] = -1;
  47.         passingThrough[1][3] = 2;
  48.         passingThrough[1][4] = -1;
  49.         passingThrough[1][5] = -1;
  50.         passingThrough[1][6] = -1;
  51.         passingThrough[1][7] = 4;
  52.         passingThrough[1][8] = -1;
  53.         passingThrough[1][9] = 5;
  54.        
  55.         passingThrough[2][3] = -1;
  56.         passingThrough[2][4] = -1;
  57.         passingThrough[2][5] = -1;
  58.         passingThrough[2][6] = -1;
  59.         passingThrough[2][7] = -1;
  60.         passingThrough[2][8] = 5;
  61.         passingThrough[2][9] = -1;
  62.        
  63.         passingThrough[3][4] = -1;
  64.         passingThrough[3][5] = -1;
  65.         passingThrough[3][6] = -1;
  66.         passingThrough[3][7] = 5;
  67.         passingThrough[3][8] = -1;
  68.         passingThrough[3][9] = 6;
  69.        
  70.         passingThrough[4][5] = -1;
  71.         passingThrough[4][6] = 5;
  72.         passingThrough[4][7] = -1;
  73.         passingThrough[4][8] = -1;
  74.         passingThrough[4][9] = -1;
  75.        
  76.         passingThrough[5][6] = -1;
  77.         passingThrough[5][7] = -1;
  78.         passingThrough[5][8] = -1;
  79.         passingThrough[5][9] = -1;
  80.        
  81.         passingThrough[6][7] = -1;
  82.         passingThrough[6][8] = -1;
  83.         passingThrough[6][9] = -1;
  84.        
  85.         passingThrough[7][8] = -1;
  86.         passingThrough[7][9] = 8;
  87.        
  88.         passingThrough[8][9] = -1;
  89.        
  90.         vector<bool> visited(10, false);
  91.         int answer = 0;
  92.         for(int num=1; num<=9; num++){  
  93.             if(minL <= 1){
  94.                 answer += 1;
  95.             }
  96.             visited[num] = true;
  97.             answer += h_numberOfPatterns(2, num, visited);
  98.             visited[num] = false;
  99.         }
  100.        
  101.         return answer;
  102.  
  103.     }
  104. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement