The goal is to find the different patterns available in a pattern lock of your average android home screen lock. For simplicity, I considered limiting my search space to a 3x3 grid. Some key points I inferred are mentioned below:
Based on the linearity of the hop, it can be classified as either a straight hop(with equal displacements along both axes) or a crooked hop (displacement along X axes + displacement along Y axes = 3).
- We can also take jumps of 2 units at a time, if the inter-mediate vertex has already been visited.
Note : One thing I'd like to point out is that the purpose of using patternsHelperPrime
to call patternsHelper
is only to retain the original vector consisting of 1s for the next call in patterns
function.
Along the lines of DFS, I came up with the following code:
#include <bits/stdc++.h>
using namespace std;
void patternsHelper(vector<vector<int>> &matrix, int totalRows, int totalColumns, int row, int column, int &count, int depth)
{
if (row < 0 || row >= totalRows)
return;
if (column < 0 || column >= totalColumns)
return;
if (matrix[row][column] == 1)
{
// we have to connect atleast 4 points
if (depth == 3)
{
cout << "incrementing count to " << count + 1 << '\n';
count++;
}
matrix[row][column] = 0;
// check for immediate links
// straight-up gangsta
patternsHelper(matrix, totalRows, totalColumns, row, column + 1, count, depth + 1);
patternsHelper(matrix, totalRows, totalColumns, row, column - 1, count, depth + 1);
patternsHelper(matrix, totalRows, totalColumns, row + 1, column, count, depth + 1);
patternsHelper(matrix, totalRows, totalColumns, row - 1, column, count, depth + 1);
// dragon valley
patternsHelper(matrix, totalRows, totalColumns, row + 1, column + 1, count, depth + 1);
patternsHelper(matrix, totalRows, totalColumns, row - 1, column - 1, count, depth + 1);
patternsHelper(matrix, totalRows, totalColumns, row + 1, column - 1, count, depth + 1);
patternsHelper(matrix, totalRows, totalColumns, row - 1, column + 1, count, depth + 1);
// crooked one-two links
patternsHelper(matrix, totalRows, totalColumns, row - 2, column + 1, count, depth + 1);
patternsHelper(matrix, totalRows, totalColumns, row - 2, column - 1, count, depth + 1);
patternsHelper(matrix, totalRows, totalColumns, row - 1, column + 2, count, depth + 1);
patternsHelper(matrix, totalRows, totalColumns, row - 1, column - 2, count, depth + 1);
patternsHelper(matrix, totalRows, totalColumns, row + 1, column + 2, count, depth + 1);
patternsHelper(matrix, totalRows, totalColumns, row + 1, column - 2, count, depth + 1);
patternsHelper(matrix, totalRows, totalColumns, row + 2, column + 1, count, depth + 1);
patternsHelper(matrix, totalRows, totalColumns, row + 2, column - 1, count, depth + 1);
// straight-up bhopping links
if (matrix[row][column + 1] == 0)
patternsHelper(matrix, totalRows, totalColumns, row, column + 2, count, depth + 1);
if (matrix[row][column - 1] == 0)
patternsHelper(matrix, totalRows, totalColumns, row, column - 2, count, depth + 1);
if (matrix[row + 1][column] == 0)
patternsHelper(matrix, totalRows, totalColumns, row + 2, column, count, depth + 1);
if (matrix[row - 1][column] == 0)
patternsHelper(matrix, totalRows, totalColumns, row - 2, column, count, depth + 1);
if (matrix[row + 1][column + 1] == 0)
patternsHelper(matrix, totalRows, totalColumns, row + 2, column + 2, count, depth + 1);
if (matrix[row - 1][column - 1] == 0)
patternsHelper(matrix, totalRows, totalColumns, row - 2, column - 2, count, depth + 1);
if (matrix[row + 1][column - 1] == 0)
patternsHelper(matrix, totalRows, totalColumns, row + 2, column - 2, count, depth + 1);
if (matrix[row - 1][column + 1] == 0)
patternsHelper(matrix, totalRows, totalColumns, row - 2, column + 2, count, depth + 1);
}
}
void patternsHelperPrime(vector<vector<int>> matrix, int totalRows, int totalColumns, int row, int column, int &count, int depth)
{
patternsHelper(matrix, totalRows, totalColumns, row, column, count, 0);
}
int patterns(vector<vector<int>> matrix)
{
int result = 0;
int totalRows = matrix.size();
int totalColumns = matrix[0].size();
for (int i = 0; i < totalRows; i++)
for (int j = 0; j < totalColumns; j++)
{
int count = 0;
patternsHelperPrime(matrix, totalRows, totalColumns, i, j, count, 0);
result += count;
}
return result;
}
int main()
{
vector<vector<int>> matrix = {
{1, 1, 1},
{1, 1, 1},
{1, 1, 1}};
cout << patterns(matrix) << '\n';
return 0;
}