2024ccpc網路賽補題

Iter-moon發表於2024-09-09

L.網路預選賽

題意:

查詢多少個2 * 2 的子矩陣滿足
[c, c]
[p, c]  
輸出個數

  

Code:

#include <bits/stdc++.h>
    
using namespace std; 
string s = "ccpc";
int dirs[4][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}}; 
char a[505][505];

void solve() {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j];
        }
    }
    int ans = 0;
    for (int i = 1; i < n; i++) {
        for (int j = 1; j < m; j++) { 
            int cnt = 0;
            for (int k = 0; k < 4; k++) {
                int dx = dirs[k][0] + i, dy = dirs[k][1] + j;
                if (a[dx][dy] == s[k]) {
                    cnt++;
                }
            }
            ans += (cnt == 4);
        }
    }
    cout << ans << '\n';
}

int main() {
    cin.tie(0) -> sync_with_stdio(false);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

  

相關文章