3x3网格中的图案

时间:2019-12-22 08:01:59

标签: c++ depth-first-search

目标是在平均android主屏幕锁的模式锁中找到可用的不同模式。为简单起见,我考虑将搜索空间限制为3x3网格。我提到的一些关键点在下面提到:

  1. 基于跳跃的线性度,可以将其分类为直线跳跃(沿两个轴的位移相等)或弯曲跳跃(沿X轴的位移+沿Y轴的位移= 3)。 / p>

    1. 如果已经访问了中间顶点,我们也可以一次跳2个单位。

注意:我想指出的一件事是,使用patternsHelperPrime来调用patternsHelper的目的仅仅是保留由1组成的原始向量用于下一次调用patterns功能。

沿着DFS,我想到了以下代码:

#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;
}

0 个答案:

没有答案