Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

14-janghw0126 #172

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

14-janghw0126 #172

wants to merge 8 commits into from

Conversation

janghw0126
Copy link
Member

@janghw0126 janghw0126 commented Oct 5, 2024

πŸ”— 문제 링크

λ°±μ€€ | 적둝색약

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1μ‹œκ°„

✨ μˆ˜λ„ μ½”λ“œ

문제λ₯Ό 봀을 λ•Œ, 적둝색약인 μ‚¬λžŒμ„ 봀을 λ•Œμ™€ μ•„λ‹Œ μ‚¬λžŒμ΄ 봀을 λ•Œ κ΅¬μ—­μ˜ 수λ₯Ό ꡬ해야 ν•˜λ―€λ‘œ λΉ¨κ°•, 초둝, νŒŒλž‘ 각각의 색을 λͺ¨λ‘ 탐색해야 λœλ‹€λŠ” 것을 μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€. λ”°λΌμ„œ, 각 μƒ‰μƒλ³„λ‘œ μž¬κ·€λ₯Ό λŒλ €μ•Όν•˜κΈ° λ•Œλ¬Έμ— μž¬κ·€ κΉŠμ΄κ°€ κΉŠμ„ 수 μžˆμœΌλ―€λ‘œ μ λ‘μƒ‰μ•½μ˜ νŠΉμ„±μ— 따라 λΉ¨κ°•, μ΄ˆλ‘μƒ‰μ„ ꡬ뢄할 수 μ—†λŠ” μΌ€μ΄μŠ€λ₯Ό λ§Œλ“€μ–΄μ€˜μ•Ό ν•©λ‹ˆλ‹€.

Note

핡심 둜직

  1. 정상인과 적둝색맹을 κ΅¬λΆ„ν•˜μ—¬ 각자의 그룹을 μ…‰λ‹ˆλ‹€.
  2. BFSλ₯Ό μ‚¬μš©ν•΄ 그룹을 νƒμƒ‰ν•©λ‹ˆλ‹€.
  3. 정상인인 경우, ν•œλ²ˆμ— ν•˜λ‚˜μ˜ 그룹을 νƒμƒ‰ν•˜κ³  κ·Έ 그룹을 λͺ¨λ‘ λ°©λ¬Έν•œ ν›„ 그룹의 개수λ₯Ό ν•˜λ‚˜ μ¦κ°€μ‹œν‚΅λ‹ˆλ‹€.
  4. 적둝색맹인 경우, 빨간색과 μ΄ˆλ‘μƒ‰μ„ κ΅¬λΆ„ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ—, μ λ‘μƒ‰λ§Ήμ—μ„œλŠ” μ΄ˆλ‘μƒ‰μ„ λΉ¨κ°„μƒ‰μœΌλ‘œ λ°”κΏ”μ„œ ν•˜λ‚˜μ˜ μƒ‰κΉ”λ‘œ μ·¨κΈ‰ν•˜κ³  λ‹€μ‹œ 그룹을 μ„Ό ν›„ μ—°κ²°λœ 점듀을 계속 νƒμƒ‰ν•©λ‹ˆλ‹€.

βœ¨μ΅œμ’… μ½”λ“œ

import sys
from collections import deque

input = sys.stdin.readline

def bfs(x, y, graph, visited):
    q = deque()
    q.append((x, y))
    visited[x][y] = True
    
    # μƒν•˜μ’Œμš° λ°©ν–₯ μ„ μ–Έν•˜κΈ°
    dx, dy = [1, -1, 0, 0], [0, 0, 1, -1]
    
    while q:
        x, y = q.popleft()
        
        for i in range(4):
            nx, ny = x + dx[i], y + dy[i]
            
            if 0 <= nx < n and 0 <= ny < n and not visited[nx][ny]:
                # 색상이 κ°™κ³  λ°©λ¬Έν•˜μ§€ μ•Šμ€ κ²½μš°μ—λ§Œ 큐에 λ„£μŒ
                if graph[x][y] == graph[nx][ny]:
                    visited[nx][ny] = True
                    q.append((nx, ny))

n = int(input())
graph = [list(input().strip()) for _ in range(n)]

# 첫 번째 λ°©λ¬Έ 배열은 정상인 (R, G, B ꡬ뢄함)
visited = [[False] * n for _ in range(n)]
normal_count = 0

# 두 번째 λ°©λ¬Έ 배열은 적둝색맹인 (R, G ꡬ뢄 μ•ˆ 함)
visited_rg = [[False] * n for _ in range(n)]
rg_count = 0

# 정상인 κ·Έλ£Ή 개수 κ΅¬ν•˜κΈ°
for i in range(n):
    for j in range(n):
        if not visited[i][j]:
            bfs(i, j, graph, visited)
            normal_count += 1

# 적둝색맹인 경우 'G'λ₯Ό 'R'둜 λ°”κΎΈκΈ°
for i in range(n):
    for j in range(n):
        if graph[i][j] == 'G':
            graph[i][j] = 'R'

# 적둝색맹인 κ·Έλ£Ή 개수 κ΅¬ν•˜κΈ°
for i in range(n):
    for j in range(n):
        if not visited_rg[i][j]:
            bfs(i, j, graph, visited_rg)
            rg_count += 1

print(normal_count, rg_count)

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

.

Copy link
Member

@jung0115 jung0115 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ½”λ“œλŠ” μ’€ 지저뢄 ν•˜μ§€λ§Œ... 저도 λΉ„μŠ·ν•˜κ²Œ ν’€μ—ˆμŠ΅λ‹ˆλ‹€ πŸ‘
핡심 λ‘œμ§μ„ κ°„κ²°ν•˜κ²Œ μ„€λͺ…ν•΄μ£Όμ…”μ„œ 더 이해가 μ‰¬μ› μŠ΅λ‹ˆλ‹€! 이번 μ°¨μ‹œλ„ μˆ˜κ³ ν•˜μ…¨μ–΄μš” ☺️

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;

public class Main {
  static int[] dx = {-1, 1, 0, 0};
  static int[] dy = {0, 0, -1, 1};
  
  static int N;
  static Character[][] weakMap;
  static Character[][] strongMap;
  static boolean[][] weakVisited;
  static boolean[][] strongVisited;
  static int weakCount = 0, strongCount = 0;
    
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    N = Integer.parseInt(br.readLine());

    strongMap = new Character[N][N];
    weakMap = new Character[N][N];
    
    // 맡 정보 μž…λ ₯
    for (int i = 0; i < N; i++) {
      String str = br.readLine();
      for (int j = 0; j < N; j++) {
        strongMap[i][j] = str.charAt(j);
        weakMap[i][j] = (str.charAt(j) == 'G') ? 'R' : str.charAt(j); // μ•½ν•œ 색맹 처리
      }
    }

    weakVisited = new boolean[N][N];
    strongVisited = new boolean[N][N];

    // 두 맡 λͺ¨λ‘μ— λŒ€ν•΄ BFS μ‹€ν–‰
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (!weakVisited[i][j]) {
          bfs(i, j, weakMap, weakVisited);
          weakCount++;
        }
        if (!strongVisited[i][j]) {
          bfs(i, j, strongMap, strongVisited);
          strongCount++;
        }
      }
    }

    // κ²°κ³Ό 좜λ ₯
    System.out.print(strongCount + " " + weakCount);
  }

  // BFS 탐색 ν•¨μˆ˜
  static void bfs(int x, int y, Character[][] map, boolean[][] visited) {
    Queue<int[]> queue = new LinkedList<>();
    queue.add(new int[]{x, y});
    visited[x][y] = true;
    Character color = map[x][y];

    while (!queue.isEmpty()) {
      int[] pos = queue.poll();
      int curX = pos[0];
      int curY = pos[1];

      // λ„€ λ°©ν–₯ 탐색
      for (int i = 0; i < 4; i++) {
        int moveX = curX + dx[i];
        int moveY = curY + dy[i];

        if (moveX >= 0 && moveX < N && moveY >= 0 && moveY < N && !visited[moveX][moveY] && map[moveX][moveY] == color) {
          visited[moveX][moveY] = true;
          queue.add(new int[]{moveX, moveY});
        }
      }
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants