Untitled

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

int n, map[20][20];
vector<int> v, re_v;

void init(int N, int mMap[20][20]) {
    n = N;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            map[i][j] = mMap[i][j];
        }
    }
    v.clear();  // Reset lại vector v
    re_v.clear();  // Reset lại vector re_v
}

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

    // Tạo lại vector v và re_v dựa trên mStructure
    v.assign(mStructure, mStructure + M);
    re_v.assign(mStructure + M - 1, mStructure - 1);

    int cnt = 0;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            bool check_hozion = true, check_vertical = true;
            
            for (int k = 0; k < M - 1 && (check_hozion || check_vertical); k++) {
                if (j + M - 1 < n) { 
                    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_hozion = false;
                    }
                } else {
                    check_hozion = false;
                }

                if (i + M - 1 < n) { 
                    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;
                    }
                } else {
                    check_vertical = false;
                }
            }

            if (check_hozion) cnt++;
            if (check_vertical) cnt++;
        }
    }

    return cnt;
}

int main() {
    freopen("sample_input.txt", "r", stdin);
    cin >> 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);

    return 0;
}
Leave a Comment