AtCoder ABC390 C問題 Paint to make a rectangle solve with C++ code

반응형
반응형

#Info

 https://atcoder.jp/contests/abc390/tasks/abc390_c

 

C - Paint to make a rectangle

AtCoder is a programming contest site for anyone from beginners to experts. We hold weekly programming contests online.

atcoder.jp

답안https://github.com/novvvv/PS/blob/main/atCoder/ABC390_C%E5%95%8F%E9%A1%8C_paint%20to%20make%20a%20rectangle.cpp

 

PS/atCoder/ABC390_C問題_paint to make a rectangle.cpp at main · novvvv/PS

알고리즘 문제 풀이 코드 모음. Contribute to novvvv/PS development by creating an account on GitHub.

github.com


#Solve

검은색으로 이루어진 직사각형을 만들 수 있는지를 확인하는 문제이다. 

배열의 모든 칸을 탐색하여 검은색 칸 (#)이 나올 때마다 높이, 넓이의 최대 최소를 업데이트한다.

흰색 칸은 검은색 칸으로 칠할 수 없기에, 해당 영역을 루프를돌아 ?가 아닌 흰색 칸(.)이 나오면 직사각형을 만들 수 없다는 뜻이기에 No를 출력하면 된다.  


#Code

#include <iostream>
using namespace std;

int main() {

    int H, W; // 1 <= H,W <= 1000
    cin >> H >> W;
    char board[1001][1001]; // # : black, . : white, ? : none
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> board[i][j];
        }
    }

    int min_height = H;
    int max_height = -1;
    int min_width = W;
    int max_width = -1;

    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (board[i][j] == '#') {
                min_height = min(min_height, i);
                max_height = max(max_height, i);
                min_width = min(min_width, j);
                max_width = max(max_width, j);
            }
        }
    }

    bool isRectangle = true;
    for (int i = min_height; i <= max_height; i++) {
        for (int j = min_width; j <= max_width; j++) {
            if (board[i][j] == '.') isRectangle = false;
        }
    }

    if (isRectangle) cout << "Yes" << endl;
    else cout << "No" << endl;


    return 0;
}

// ?
반응형

댓글

Designed by JB FACTORY