THPTTD_96 - Tô màu tranh

Xem dạng PDF

Gửi bài giải


Điểm: 5,00 (OI)
Giới hạn thời gian: 1.0s
Giới hạn bộ nhớ: 256M
Input: picture.inp
Output: picture.out

Tác giả:
Dạng bài
Ngôn ngữ cho phép
C, C#, C++, Go, Java, JavaScript, Kotlin, Pascal, Perl, PHP, PyPy, Python, Ruby, Rust, Scratch, Swift

Trong trường hợp đề bài hiển thị không chính xác, bạn có thể tải đề bài tại đây: Đề bài


Bình luận

Please read the guidelines before commenting.



  • 0
    ToiNhoDuongThanhThao  đã bình luận lúc 11, Tháng 6, 2026, 3:11

    include <iostream>

    include <vector>

    include <queue>

    include <fstream>

    using namespace std;

    int main() { iosbase::syncwith_stdio(false); cin.tie(NULL);

    ifstream cin_f("PICTURE.INP");
    ofstream cout_f("PICTURE.OUT");
    
    int m, n;
    cin_f >> m >> n;
    
    vector<vector<int>> g(m, vector<int>(n));
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            cin_f >> g[i][j];
        }
    }
    
    vector<vector<bool>> v(m, vector<bool>(n, false));
    int c = 0;
    
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            if (!v[i][j]) {
                c++;
                int cl = g[i][j];
                queue&lt;pair&lt;int, int>> q;
                q.push({i, j});
                v[i][j] = true;
    
                while (!q.empty()) {
                    pair&lt;int, int> cur = q.front();
                    q.pop();
    
                    int dx[] = {0, 0, 1, -1};
                    int dy[] = {1, -1, 0, 0};
    
                    for (int k = 0; k < 4; ++k) {
                        int ni = cur.first + dx[k];
                        int nj = cur.second + dy[k];
    
                        if (ni >= 0 && ni < m && nj >= 0 && nj < n && !v[ni][nj] && g[ni][nj] == cl) {
                            v[ni][nj] = true;
                            q.push({ni, nj});
                        }
                    }
                }
            }
        }
    }
    
    cout_f << c << endl;
    
    return 0;
    

    }