Number of possible mobile lock patterns with min m keys and Max n keys


  • 0

    Re: Android Unlock Patterns

    #include<bits/stdc++.h>
    using namespace std;
     bool visit[10]={false};
     int jump[10][10];
    
    int DFS(int start,int len,int count,int m,int n)
    {
    	if(len >= m)
    		count++;
    	len++;
    	if(len > n)
    		return count;
    	visit[start]=true;
    
    	for(int next=1; next<=9;next++)
    	{
    		int jump_through=jump[start][next];
    		if(visit[next]==false && (jump_through==0 || visit[jump_through]==true))
    		{
    			count =DFS(next,len ,count,m,n);
    		}
    	}
    	visit[start]=false;
    	return count;
    }
    
    int main()
    {
    	int m=4,n=6;
    	
    	memset(jump,0,sizeof(jump));
    	 jump[1][3] = jump[3][1] = 2;
     
       
        jump[7][9] = jump[9][7] = 8;
     
        //  4 lies between 1 and 7
        jump[1][7] = jump[7][1] = 4;
     
        //  6 lies between 3 and 9
        jump[3][9] = jump[9][3] = 6;
     
        //  5 lies between 1, 9  2, 8  3, 7 and 4, 6
        jump[1][9] = jump[9][1] = jump[2][8] = jump[8][2]
         = jump[3][7] = jump[7][3] = jump[4][6] = jump[6][4] = 5;
    
    
        
         int count=0;
         count+=DFS(1,1,0,m,n)*4;
         count+=DFS(2,1,0,m,n)*4;
         count+=DFS(5,1,0,m,n);
    
         cout<<count;
    
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.