1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include <set>

using namespace std;

const int ModBy = 1000000000 + 7;

const char FREE = '.';
const char TILE = '#';
const char VISITED = 'V';

const int MaxBoardSize = 3000;

char Board[MaxBoardSize][MaxBoardSize];

int BoardSize;
int NumPlayers;
int NumFields;

set< set<int> > Solutions;

unsigned long long SolutionsCount = 0;

int getFieldIdx(int row, int col) {
    return BoardSize * row + col;
}

void setFieldValue(int idx, char value) {
    int row = idx / BoardSize;
    int col = idx % BoardSize;

    Board[row][col] = value;
}

void AlgorithmStep(int LeadFieldNumber, set<int> AvailableFields, int Steps, set<int> Solution) {
    int row = LeadFieldNumber / BoardSize;
    int col = LeadFieldNumber % BoardSize;

    setFieldValue(LeadFieldNumber, TILE); // We placed a tile - we made a step
    ++Steps;
    Solution.insert(LeadFieldNumber);

    if (Steps == NumPlayers) {
        // All players made their steps

        // Save solution
        Solutions.insert(Solution);

        setFieldValue(LeadFieldNumber, FREE);

        return;
    }

    if (row - 1 >= 0 && Board[row - 1][col] == FREE) {
        AvailableFields.insert(getFieldIdx(row - 1, col));
    }
    if (col - 1 >= 0 && Board[row][col - 1] == FREE) {
        AvailableFields.insert(getFieldIdx(row, col - 1));
    }
    if (row + 1 < BoardSize && Board[row + 1][col] == FREE) {
        AvailableFields.insert(getFieldIdx(row + 1, col));
    }
    if (col + 1 < BoardSize && Board[row][col + 1] == FREE) {
        AvailableFields.insert(getFieldIdx(row, col + 1));
    }

    while (!AvailableFields.empty()) {
        auto it = AvailableFields.begin();
        int NextFieldNumber = *it;
        AvailableFields.erase(it);

        AlgorithmStep(NextFieldNumber, AvailableFields, Steps, Solution);
    }


    setFieldValue(LeadFieldNumber, FREE);
}

int main() {
    scanf("%d", &BoardSize);
    scanf("%d", &NumPlayers);

    NumFields = BoardSize * BoardSize;

    char GridItem;

    set<int> InitialAvailableFields;

    int previousCol;
    int previousRow;

    for (int row = 0; row < BoardSize; ++row) {
        for (int col = 0; col < BoardSize; ++col) {
            scanf(" %c", &GridItem);

            previousCol = col - 1;
            previousRow = row - 1;

            if (GridItem == '#') {
                Board[row][col] = TILE;

                if (previousCol >= 0 && Board[row][previousCol] == FREE) {
                    InitialAvailableFields.insert(getFieldIdx(row, previousCol));
                } else if (previousRow >= 0 && Board[previousRow][col] == FREE) {
                    InitialAvailableFields.insert(getFieldIdx(previousRow, col));
                }
            } else {
                Board[row][col] = FREE;

                if ((previousCol >= 0 && Board[row][previousCol] == TILE) ||
                        (previousRow >= 0 && Board[previousRow][col] == TILE)) {
                    InitialAvailableFields.insert(getFieldIdx(row, col));
                }
            }
        }
    }

    set<int> emptySolution;

    while (!InitialAvailableFields.empty()) {
        auto it = InitialAvailableFields.begin();
        int LeadFieldNumber = *it;
        InitialAvailableFields.erase(it);

        // DO work

        AlgorithmStep(LeadFieldNumber, InitialAvailableFields, 0, emptySolution);

        setFieldValue(LeadFieldNumber, VISITED);

        unsigned long long SolutionsInStep = Solutions.size();

        SolutionsCount = (SolutionsCount + SolutionsInStep % ModBy) % ModBy;

        Solutions.clear();
    }

    cout << SolutionsCount << endl;

    return 0;
}