[Programmers] Lv2. ์นด์นด์˜คํ”„๋ Œ์ฆˆ ์ปฌ๋Ÿฌ๋ง๋ถ | C++

2023. 7. 13. 20:07ใ†Coding Test/Programmers

๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

๐Ÿ‘จ‍๐Ÿ’ปํ’€์ด ๊ณผ์ •

 

BFS๋กœ ํ’€ ์ˆ˜ ์žˆ๋Š” ์‰ฌ์šด ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

  1. ์ƒ‰์น ๋˜์ง€ ์•Š์€ ์˜์—ญ์€ ์ง€๋‚˜๊ฐ„๋‹ค.
  2. ์ƒ‰์น ๋˜์–ด ์žˆ๊ณ , ๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†๋Š” ํ”ฝ์…€์ด๋ผ๋ฉด, ํ•ด๋‹น ํ”ฝ์…€์„ ๊ธฐ์ค€์œผ๋กœ ์ƒํ•˜์ขŒ์šฐ BFS ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•œ๋‹ค.
  3. BFS ํƒ์ƒ‰์ด ๋๋‚ฌ๋‹ค๋ฉด ์˜์—ญ ๊ฐœ์ˆ˜๋ฅผ 1๊ฐœ ๋Š˜๋ ค์ฃผ๊ณ , ์ด์ „ ์˜์—ญ ํฌ๊ธฐ์™€ ๋น„๊ตํ•˜์—ฌ ๋” ํฐ ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ํ•œ๋‹ค.

 

โœ๏ธ์†Œ์Šค ์ฝ”๋“œ ๋ฐ ๊ฒฐ๊ณผ

#include <vector>
#include <queue>
using namespace std;
const int DIRECTION_COUNT = 4;
const int rowDirections[DIRECTION_COUNT] = { -1, 1, 0, 0 };
const int colDirections[DIRECTION_COUNT] = { 0, 0, -1, 1 };

struct Pixel {
    Pixel() { }
    Pixel(int color, int row, int col) : _color(color), _row(row), _col(col) { }

    int     _color;
    int     _row;
    int     _col;
};

int FindArea(vector<vector<Pixel>>& board, vector<vector<bool>>& visited, int m, int n, Pixel selection) {
    int area = 1;
    visited[selection._row][selection._col] = true;
    
    queue<Pixel> coloredAreas;
    coloredAreas.emplace(selection);

    while (!coloredAreas.empty()) {
        Pixel pixel = coloredAreas.front();
        coloredAreas.pop();

        for (int i = 0; i < DIRECTION_COUNT; i++) {
            int nextRow = pixel._row + rowDirections[i];
            int nextCol = pixel._col + colDirections[i];

            if (nextRow < 0 or nextRow >= m or nextCol < 0 or nextCol >= n)
                continue;
            if (board[nextRow][nextCol]._color != pixel._color or board[nextRow][nextCol]._color == 0 or visited[nextRow][nextCol])
                continue;

            coloredAreas.emplace(Pixel(pixel._color, nextRow, nextCol));
            visited[nextRow][nextCol] = true;
            area++;
        }
    }

    return area;
}

// ์ „์—ญ ๋ณ€์ˆ˜๋ฅผ ์ •์˜ํ•  ๊ฒฝ์šฐ ํ•จ์ˆ˜ ๋‚ด์— ์ดˆ๊ธฐํ™” ์ฝ”๋“œ๋ฅผ ๊ผญ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.
vector<int> solution(int m, int n, vector<vector<int>> picture) {
    int number_of_area = 0;
    int max_size_of_one_area = 0;
    vector<vector<Pixel>> board(m, vector<Pixel>(n));
    vector<vector<bool>> visited(m, vector<bool>(n, false));

    for (int row = 0; row < m; row++)
        for (int col = 0; col < n; col++)
            board[row][col] = Pixel(picture[row][col], row, col);

    for (int row = 0; row < m; row++) {
        for (int col = 0; col < n; col++) {
            if (visited[row][col] or board[row][col]._color == 0)
                continue;

            number_of_area++;
            max_size_of_one_area = max(max_size_of_one_area, FindArea(board, visited, m, n, board[row][col]));
        }
    }

    return { number_of_area, max_size_of_one_area };
}

 

์ฑ„์  ์ผ€์ด์Šค๊ฐ€ 1๊ฐœ ๋ฐ–์— ์—†๋„ค์š”?

 

728x90
๋ฐ˜์‘ํ˜•