[프로그래머스] 92343 양과 늑대 - JAVA - Lv3

2025. 4. 27. 22:41·알고리즘/문제풀이

[프로그래머스]  92343 양과 늑대 - JAVA - Lv3

문제

https://school.programmers.co.kr/learn/courses/30/lessons/92343

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

문제 분석

조건

  • 2진트리 각 노드의 양이나 늑대가 1마리씩 존재한다.
  • 양의 수가 늑대의 수보다 작거나 같으면 양은 잡아먹힌다.
  • 루트노드는 양이다.
  • 트리를 탐색하며 최대한 많은 양을 모을 때 양의 수를 출력.

풀이방법

  • BFS인데 일반적인 BFS가 아니다.
  • 방문처리하는 방법이 핵심이라고 생각하는데, 다른 BFS처럼 지금 노드의 번호를 저장하는 것이 아니라 양의 수, 늑대의 수, 방문 가능한 노드들을 이용해서 방문처리를 해야한다.
  • 큐에 넣기위해 클래스를 만들어 위 정보들을 넣어주었고 방문처리는 문자열로 처리하고 `Set`을 통해 관리하였다.

코드

import java.util.*;

class State {
    private int sheep;
    private int wolf;
    private List<Integer> nextNodes;

    public State(int sheep, int wolf, List<Integer> nextNodes) {
        this.sheep = sheep;
        this.wolf = wolf;
        this.nextNodes = nextNodes;
    }

    public int getSheep() {
        return sheep;
    }

    public int getWolf() {
        return wolf;
    }

    public List<Integer> getNextNodes() {
        return nextNodes;
    }
}

class Solution {
    public int solution(int[] info, int[][] edges) {
        int answer = 1;

        List<Integer>[] graph = new List[info.length];
        for (int i = 0; i < info.length; i++) {
            graph[i] = new ArrayList<>();
        }
        for (int[] edge : edges) {
            graph[edge[0]].add(edge[1]);
        }

        Set<String> visited = new HashSet<>();
        Deque<State> que = new ArrayDeque<>();
        que.add(new State(1, 0, new ArrayList<>(graph[0])));
        visited.add(generateKey(1, 0, new ArrayList<>(graph[0])));
        while (!que.isEmpty()) {
            State currentState = que.poll();
            for (int i = 0; i < currentState.getNextNodes().size(); i++) {
                int now = currentState.getNextNodes().get(i);

                int nextSheep = currentState.getSheep();
                int nextWolf = currentState.getWolf();

                if (info[now] == 0) {
                    nextSheep++;
                } else {
                    nextWolf++;
                }
                if (nextSheep <= nextWolf) {
                    continue;
                }
                answer = Math.max(answer, nextSheep);
                List<Integer> newNextNodes = new ArrayList<>(currentState.getNextNodes());
                newNextNodes.remove(Integer.valueOf(now));
                newNextNodes.addAll(graph[now]);
                String visitKey = generateKey(nextSheep, nextWolf, newNextNodes);
                if (!visited.contains(visitKey)) {
                    que.add(new State(nextSheep, nextWolf, newNextNodes));
                    visited.add(visitKey);
                }
            }
        }
        return answer;
    }

    private String generateKey(int nextSheep, int nextWolf, List<Integer> newNextNodes) {
        Collections.sort(newNextNodes);
        return nextSheep + ", " + nextWolf + ", " + newNextNodes.toString();
    }
}

결과

+

  • 3차원 배열을 통해 방문처리를 했던 경험은 있는데 문자열을 통해 방문처리를 한 경험은 처음이다. 한 지점에 여러번 방문하며 체크해야될 때 좋은 방법같다.
728x90
저작자표시 비영리 변경금지 (새창열림)
'알고리즘/문제풀이' 카테고리의 다른 글
  • [백준/BOJ] 1149 RGB거리 - JAVA - 실버1
  • [백준/BOJ] 12946 하노이의 탑 - JAVA - Lv2
  • [프로그래머스] 43162 네트워크 - JAVA - Lv3
  • [백준/BOJ] 14889 스타트와 링크 - JAVA - 실버1
LIRI
LIRI
  • LIRI
    기록
    LIRI
  • 전체
    오늘
    어제
    • 분류 전체보기 (74)
      • 블로그 꾸미기 (0)
      • Spring (6)
      • 바이브코딩 (1)
      • React (3)
      • CS (0)
      • 알고리즘 (57)
        • 개념 (2)
        • 문제풀이 (54)
      • Java (1)
      • DB (1)
      • log (4)
        • SSAFY (3)
        • 궁금 (1)
  • 블로그 메뉴

    • 홈
    • 방명록
  • 공지사항

  • 인기 글

  • 태그

    백준
    Springsecurity
    프로그래머스
    JWT
    dp
    리액트
    Security
    SSAFY 9기
    바이브코딩
    LIS
    싸피
    너비우선탐색
    느좋코딩
    그리디
    SSAFY
    BOJ
    알고리즘
    커서ai
    LV2
    springboot
    최장증가부분수열
    ssafy 합격 후기
    비트마스킹
    알고리즘 문제풀이
    lv3
    BFS
    dfs
    pccp모의고사
    Spring
    Java
  • 최근 댓글

  • 최근 글

  • 250x250
  • hELLO· Designed By정상우.v4.10.3
LIRI
[프로그래머스] 92343 양과 늑대 - JAVA - Lv3
상단으로

티스토리툴바