Untitled

mail@pastecode.io avatar
unknown
plain_text
23 days ago
2.0 kB
3
Indexable
Never
#include <iostream>
#include <vector>
using namespace std;

int n;
vector<vector<int>> map;
vector<int> v;
vector<int> re_v;

void init(int N, vector<vector<int>>& mMap) {
    n = N;
    map = mMap;
    v.clear();
    re_v.clear();
}

int numberOfCandidate(int M, int mStructure[5]) {
    if (M == 1) return n * n;

    int cnt = 0;
    bool check_vertical, check_horizontal;

    for (int i = 0; i < M; i++) {
        v.push_back(mStructure[i]);
        re_v.push_back(mStructure[M - i - 1]);
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            check_horizontal = true;
            check_vertical = true;

            // Check horizontal shape
            if (j + M - 1 < n) {
                for (int k = 0; k < M - 1; k++) {
                    if ((map[i][j + k] + v[k] != map[i][j + k + 1] + v[k + 1]) &&
                        (map[i][j + k] + re_v[k] != map[i][j + k + 1] + re_v[k + 1])) {
                        check_horizontal = false;
                        break;
                    }
                }
                if (check_horizontal) cnt++;
            }
            
            // Check vertical shape
            if (i + M - 1 < n) {
                for (int k = 0; k < M - 1; k++) {
                    if ((map[i + k][j] + v[k] != map[i + k + 1][j] + v[k + 1]) &&
                        (map[i + k][j] + re_v[k] != map[i + k + 1][j] + re_v[k + 1])) {
                        check_vertical = false;
                        break;
                    }
                }
                if (check_vertical) cnt++;
            }
        }
    }
    return cnt;
}

int main() {
    freopen("sample_input.txt", "r", stdin);

    cin >> n;
    map.resize(n, vector<int>(n));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> map[i][j];
        }
    }

    init(n, map);
    
    int mStructure[3] = {1, 1, 1};
    cout << numberOfCandidate(3, mStructure) << endl;

    return 0;
}
Leave a Comment