백준 10026번 적록색약 [C++]

2024. 9. 7. 11:40· [백준]/C++
목차
  1. #문제 간단 정리
  2. #문제 해결 방법
  3. #전체 코드
반응형

https://www.acmicpc.net/problem/10026

 

 

 

 

 

#문제 간단 정리

 

기본적인 bfs 구현문제

 

 

#문제 해결 방법

 

bfs 를 조회할때 

색약인지 색약인지 아닌지를 구분받아서 조회할 수 있는지 

물어보는 문제이다

 

색약인경우의 조건을 잘 구현하자

 

#전체 코드

 

#include <iostream>
#include <vector>
#include <string>
#include <queue>
using namespace std;

int dy[4] = { 0,-1,0,1 };
int dx[4] = { -1,0,1,0 };

int n;
vector<vector<char>> rgb;
vector<vector<bool>> check;
int blindCount;
int nBlindCount;

void bfs(int y, int x, bool blind) {

    queue<pair<int, int>> q;
    q.push({ y,x });
    check[y][x] = true;

    char color = rgb[y][x];


    while (!q.empty() && !blind) {
        int y = q.front().first;
        int x = q.front().second;

        q.pop();

        for (int i = 0; i < 4; i++) {
            int ny = y + dy[i];
            int nx = x + dx[i];

            if (0<=nx && nx <n && 0<= ny && ny<n ) {

                if (check[ny][nx] == false) {
                    if (rgb[ny][nx] == color) {
                        q.push({ ny, nx });

                        check[ny][nx] = true;
                    }
                }


            }

        }

    }

    while (!q.empty() && blind) {
        int y = q.front().first;
        int x = q.front().second;

        q.pop();

        for (int i = 0; i < 4; i++) {
            int ny = y + dy[i];
            int nx = x + dx[i];

            if (0 <= nx && nx < n && 0 <= ny && ny < n) {

                if (check[ny][nx] == false) {
                    if (color == 'B' && rgb[ny][nx] == color) {
                        q.push({ ny, nx });

                        check[ny][nx] = true;
                    }
                    else if (color == 'R' || color == 'G') {
                        if (rgb[ny][nx] == 'R' || rgb[ny][nx] == 'G') {
                            q.push({ ny, nx });

                            check[ny][nx] = true;
                        }

                    }
                }



            }

        }

    }


}

int main() {
    cin >> n;

    rgb.resize(n, vector<char>(n));
    check.resize(n, vector<bool>(n, false));

    for (int i = 0; i < n; i++) {
        string s; cin >> s;

        for (int j = 0; j < n; j++) {
            rgb[i][j] = s[j];

        }

    }

    blindCount = 0;
    nBlindCount = 0;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (check[i][j] == false) {
                bfs(i, j, false);
                nBlindCount++;
            }

        }
    }

    check.assign(n, vector<bool>(n, false));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (check[i][j] == false) {
                bfs(i, j, true);
                blindCount++;
            }


            
        }
    }

    cout << nBlindCount << ' ' << blindCount << '\n';

    return 0;
}
반응형

'[백준] > C++' 카테고리의 다른 글

백준 4358번 생태학 [C++]  (0) 2024.09.13
백준 9764번 서로 다른 자연수의 합 [C++]  (0) 2024.09.08
백준 2589번 보물섬 [C++]  (0) 2024.09.05
백준 25601번 자바의 형변환 [C++]  (0) 2024.08.30
백준 16692번 Greedy Scheduler [C++]  (0) 2024.08.27
  1. #문제 간단 정리
  2. #문제 해결 방법
  3. #전체 코드
'[백준]/C++' 카테고리의 다른 글
  • 백준 4358번 생태학 [C++]
  • 백준 9764번 서로 다른 자연수의 합 [C++]
  • 백준 2589번 보물섬 [C++]
  • 백준 25601번 자바의 형변환 [C++]
경우42
경우42
개발 등 공부기록용 블로그입니다
경우42
경우없는 개발 블로그
경우42
전체
오늘
어제
  • 분류 전체보기 (225)
    • 후기 (1)
    • [Codeforces] (4)
    • [SW Expert Academy] (10)
    • [백준] (149)
      • C++ (144)
      • C# (4)
      • python (1)
    • [프로그래머스] (15)
      • lv.3 (8)
      • lv.2 (4)
      • lv.1 (3)
    • [CS(Computer Science)] (2)
      • 자료구조 (2)
    • 알고리즘 (32)
      • Tip (6)
      • 코드 (15)
      • SQL 문법 정리 (10)
    • 웹개발지식 (2)
    • 스프링 (2)
    • 딥러닝 (0)
    • [가톨릭대주변음식점] (2)
      • 런칭&모니터링 (0)
      • 개발 (0)
      • 트러블 슈팅 (2)
    • [만냠-밥약속매칭플랫폼] (1)
    • [일정정리 웹 개발] (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 14246번
  • 11365번
  • 5585번
  • 133300번
  • 10845번
  • 17352번
  • 플로이드-워셜
  • 10989번
  • 두 포인터
  • lv.2
  • 백준
  • 2751번
  • C++
  • 9012번
  • 4920번
  • 2003번
  • 코드 #다익스트라
  • 냠톨릭
  • 프로그래머스
  • c#

최근 댓글

최근 글

hELLO · Designed By 정상우.v4.2.0
경우42
백준 10026번 적록색약 [C++]
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.