Untitled

 avatar
unknown
plain_text
a year ago
3.2 kB
4
Indexable
#include<bits/stdc++.h>

using namespace std;

int matrix[2000][2000];

bool check_condition(int row1, int col1, int row2, int col2) {
    // Neu khac hang khac cot
    if (row1 != row2 && col1 != col2) {
        int count = 0;
        // Kiem tra 4 o tren duoi trai phai
        if (row2 > 0 && matrix[row2 - 1][col2] == 1) count++;
        if (row2 < 1999 && matrix[row2 + 1][col2] == 1) count++;
        if (col2 > 0 && matrix[row2][col2 - 1] == 1) count++;
        if (col2 < 1999 && matrix[row2][col2 + 1] == 1) count++;

        if (count >= 3) return true;

        // Kiem tra tiep cac o co gia tri 1
        if (row2 > 0 && matrix[row2 - 1][col2] == 1 && (row2 - 1 != row1 && col2 != col1)) return true;
        if (row2 < 1999 && matrix[row2 + 1][col2] == 1 && (row2 + 1 != row1 && col2 != col1)) return true;
        if (col2 > 0 && matrix[row2][col2 - 1] == 1 && (row2 != row1 && col2 - 1 != col1)) return true;
        if (col2 < 1999 && matrix[row2][col2 + 1] == 1 && (row2 != row1 && col2 + 1 != col1)) return true;

        return false;
    }

    // Neu cùng hàng
    if (row1 == row2) {
    	// Kiem tra 4 ô trên duoi trái phai cua (row2, col2)
        bool hasOne = false;
        if (row2 > 0 && matrix[row2 - 1][col2] == 1) hasOne = true;
        if (row2 < 1999 && matrix[row2 + 1][col2] == 1) hasOne = true;
        if (col2 > 0 && matrix[row2][col2 - 1] == 1) hasOne = true;
        if (col2 < 1999 && matrix[row2][col2 + 1] == 1) hasOne = true;

        // Neu không có ô nào có giá tri 1, tra ve false
        if (!hasOne) return false;
        
        int start = std::min(col1, col2) + 1;
        int end = std::max(col1, col2);
        for (int col = start; col < end; ++col) {
            if (matrix[row1][col] == 0) {
                return true;
            }
        }
    }

    // Neu cùng cot
    if (col1 == col2) {
    	// Kiem tra 4 ô trên duoi trái phai cua (row2, col2)
        bool hasOne = false;
        if (row2 > 0 && matrix[row2 - 1][col2] == 1) hasOne = true;
        if (row2 < 1999 && matrix[row2 + 1][col2] == 1) hasOne = true;
        if (col2 > 0 && matrix[row2][col2 - 1] == 1) hasOne = true;
        if (col2 < 1999 && matrix[row2][col2 + 1] == 1) hasOne = true;

        // Neu không có ô nào có giá tri 1, tra ve false
        if (!hasOne) return false;
        
        int start = std::min(row1, row2) + 1;
        int end = std::max(row1, row2);
        for (int row = start; row < end; ++row) {
            if (matrix[row][col1] == 0) {
                return true;
            }
        }
    }

    return false;
}


int main () {
	int n, m;
	cin >> n >> m;
	vector<pair<int, int>> ls;//list square
	for (int i = 0; i < n; i++) {
		for	(int e = 0; e < m; e++) {
			cin >> matrix[i][e];
			if (matrix[i][e] == 1) ls.push_back(make_pair(i, e));
		}
	}
    
    int count = 0;
    for (int fk = 0; fk < ls.size(); fk++) { //first king
        for (int sk = fk + 1; sk < ls.size(); sk++) {  //second king
	        if (check_condition(ls[fk].first, ls[fk].second, ls[sk].first, ls[sk].second)) count++;
	    }
    }
	
	cout << count;
}
Editor is loading...
Leave a Comment