package com.fishercoder.solutions;

/**
 * 351. Android Unlock Patterns
 *
 * Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total number of unlock patterns of the Android lock screen, which consist of minimum of m keys and maximum n keys.

 Rules for a valid pattern:
 Each pattern must connect at least m keys and at most n keys.
 All the keys must be distinct.
 If the line connecting two consecutive keys in the pattern passes through any other keys, the other keys must have previously selected in the pattern. No jumps through non selected key is allowed.
 The order of keys used matters.

 Explanation:
 | 1 | 2 | 3 |
 | 4 | 5 | 6 |
 | 7 | 8 | 9 |
 Invalid move: 4 - 1 - 3 - 6
 Line 1 - 3 passes through key 2 which had not been selected in the pattern.

 Invalid move: 4 - 1 - 9 - 2
 Line 1 - 9 passes through key 5 which had not been selected in the pattern.

 Valid move: 2 - 4 - 1 - 3 - 6
 Line 1 - 3 is valid because it passes through key 2, which had been selected in the pattern

 Valid move: 6 - 5 - 4 - 1 - 9 - 2
 Line 1 - 9 is valid because it passes through key 5, which had been selected in the pattern.

 Example:
 Given m = 1, n = 1, return 9.


 */
public class _351 {

    public static class Solution1 {
        private int[][] jumps;
        private boolean[] visited;

        public int numberOfPatterns(int m, int n) {
            jumps = new int[10][10];
            jumps[1][3] = jumps[3][1] = 2;
            jumps[4][6] = jumps[6][4] = 5;
            jumps[7][9] = jumps[9][7] = 8;
            jumps[1][7] = jumps[7][1] = 4;
            jumps[2][8] = jumps[8][2] = jumps[1][9] = jumps[9][1] = 5;
            jumps[9][3] = jumps[3][9] = 6;
            jumps[3][7] = jumps[7][3] = 5;
            visited = new boolean[10];
            int count = 0;
            count += dfs(1, 1, 0, m, n)
                * 4;//1,3,7,9 are symmetric, so we only need to use 1 to do it once and then multiply the result by 4
            count += dfs(2, 1, 0, m, n)
                * 4;//2,4,6,8 are symmetric, so we only need to use 1 to do it once and then multiply the result by 4
            count += dfs(5, 1, 0, m, n);
            return count;
        }

        private int dfs(int num, int len, int count, int m, int n) {
            if (len >= m) {
                count++;
            }
            len++;
            if (len > n) {
                return count;
            }
            visited[num] = true;
            for (int next = 1; next <= 9; next++) {
                int jump = jumps[num][next];
                if (!visited[next] && (jump == 0 || visited[jump])) {
                    count = dfs(next, len, count, m, n);
                }
            }
            visited[num] = false;//backtracking
            return count;
        }
    }
}

@@ -33,1190 +33,8 @@
 s;%0A%0A
-/**%0A * 351. Android Unlock Patterns%0A *%0A * Given an Android 3x3 key lock screen and two integers m and n, where 1 %E2%89%A4 m %E2%89%A4 n %E2%89%A4 9, count the total number of unlock patterns of the Android lock screen, which consist of minimum of m keys and maximum n keys.%0A%0A Rules for a valid pattern:%0A Each pattern must connect at least m keys and at most n keys.%0A All the keys must be distinct.%0A If the line connecting two consecutive keys in the pattern passes through any other keys, the other keys must have previously selected in the pattern. No jumps through non selected key is allowed.%0A The order of keys used matters.%0A%0A Explanation:%0A %7C 1 %7C 2 %7C 3 %7C%0A %7C 4 %7C 5 %7C 6 %7C%0A %7C 7 %7C 8 %7C 9 %7C%0A Invalid move: 4 - 1 - 3 - 6%0A Line 1 - 3 passes through key 2 which had not been selected in the pattern.%0A%0A Invalid move: 4 - 1 - 9 - 2%0A Line 1 - 9 passes through key 5 which had not been selected in the pattern.%0A%0A Valid move: 2 - 4 - 1 - 3 - 6%0A Line 1 - 3 is valid because it passes through key 2, which had been selected in the pattern%0A%0A Valid move: 6 - 5 - 4 - 1 - 9 - 2%0A Line 1 - 9 is valid because it passes through key 5, which had been selected in the pattern.%0A%0A Example:%0A Given m = 1, n = 1, return 9.%0A%0A%0A */%0A
 publ
@@ -669,32 +669,36 @@
 (1, 1, 0, m, n)%0A
+    
                 
@@ -791,32 +791,32 @@
 the result by 4%0A
-
             coun
@@ -831,32 +831,36 @@
 (2, 1, 0, m, n)%0A
+    
                 

refactor 351