본문 바로가기
Study/Coding Test

[백준] 10026 - 적록색약 Python, C/C++

by 들숨날숨흡 2023. 10. 11.
728x90

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

 

10026번: 적록색약

적록색약은 빨간색과 초록색의 차이를 거의 느끼지 못한다. 따라서, 적록색약인 사람이 보는 그림은 아닌 사람이 보는 그림과는 좀 다를 수 있다. 크기가 N×N인 그리드의 각 칸에 R(빨강), G(초록)

www.acmicpc.net

(1) Python

import sys
from collections import deque

input = sys.stdin.readline

dx = [-1, 1, 0, 0]
dy = [0, 0, 1, -1]

def dfs(start_x, start_y, flag):
    queue = deque()
    queue.append((start_x, start_y))

    if flag == 0:
        visited_normal[start_x][start_y] = 1
    else:
        visited_not[start_x][start_y] = 1

    while queue:
        x, y = queue.popleft()
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if 0 <= nx < N and 0 <= ny < N:
                if flag == 0 and visited_normal[nx][ny] == 0: #일반인의 경우
                    if colors[nx][ny] == colors[x][y]:
                        visited_normal[nx][ny] = 1
                        queue.append((nx, ny))
                elif flag == 1 and visited_not[nx][ny] == 0: #색약의 경우
                    if colors[x][y] in ("R","G"):
                        if colors[nx][ny] in ("R","G"):
                            visited_not[nx][ny] = 1
                            queue.append((nx, ny))
                    else:
                        if colors[nx][ny] == colors[x][y]:
                            visited_not[nx][ny] = 1
                            queue.append((nx, ny))

N = int(input())
colors = list()
answer = [0, 0]
visited_normal = [[0] * N for _ in range(N)]
visited_not = [[0] * N for _ in range(N)]

for i in range(N):
    color = input().rstrip()
    li = list()
    for c in color:
        li.append(c)
    colors.append(li)

for i in range(N):
    for j in range(N):
        if visited_normal[i][j] == 0:
            dfs(i, j, 0)
            answer[0] += 1
            # print(answer)
        if visited_not[i][j] == 0:
            dfs(i, j, 1)
            answer[1] += 1
            # print(answer)

print(answer[0], answer[1], end = " ")

 

 

(2) C/C++

#include <iostream>
#include <algorithm>
#include <vector>
#define FIO cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#define MAX 101
using namespace std;
int N;
char MAP[MAX][MAX];
bool Visit[MAX][MAX];

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

void Input()
{
    cin >> N;
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            cin >> MAP[i][j];
        }
    }
}

void BFS(int a, int b)
{
    queue<pair<int, int>> Q;
    Q.push(make_pair(a, b));
    Visit[a][b] = true;

    while (Q.empty() == 0)
    {
        int x = Q.front().first;
        int y = Q.front().second;
        Q.pop();

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

            if (nx >= 0 && ny >= 0 && nx < N && ny < N)
            {
                if (Visit[nx][ny] == false)
                {
                    if (MAP[nx][ny] == MAP[x][y])
                    {
                        Visit[nx][ny] = true;
                        Q.push(make_pair(nx, ny));
                    }
                }
            }
        }
    }
}
void Solution()
{
    int Answer, Answer2;
    Answer = Answer2 = 0;
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (Visit[i][j] == false)
            {
                BFS(i, j);
                Answer++;
            }
        }
    }

    memset(Visit, false, sizeof(Visit));
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (MAP[i][j] == 'G') MAP[i][j] = 'R';
        }
    }

    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (Visit[i][j] == false)
            {
                BFS(i, j);
                Answer2++;
            }
        }
    }

    cout << Answer << " " << Answer2 << endl;
}

void Solve()
{
    Input();
    Solution();
}

int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    Solve();

    return 0;
}

 

 


https://github.com/sumini0516

 

sumini0516 - Overview

sumini0516 has 6 repositories available. Follow their code on GitHub.

github.com

 

728x90