[프로그래머스] 양과 늑대

Eden·2023년 7월 20일
0

문제 출처

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

using System;

public class Solution {

    public int solution(int[] info, int[,] edges) {
        passed = new bool[info.Length];
        infos = info;
        newedges = edges;
        passed[0] = true; // 노드 0에서 시작
        BFS(1,0); // 노드 0 이 양이라는 점
        return answer;
    }
    public int[,] newedges; // 문제 전역변수 선언
    public int[] infos ; // 문제 전역변수 선언
    public bool[] passed ; // 같은 길을 지날때 중복계산하지 않도록
    public int sheap = 0; // 양 수
    public int wolf = 0; // 늑대 수
    public int answer = 0; //리턴값
    public void BFS(int sheap, int wolf)
    {
        if(sheap > wolf)
            answer = Math.Max(sheap,answer); // 양 최댓값 갱신
        else
            return; // 늑대가 같거나 많아지면 중단
        for (int i = 0; i < newedges.GetLength(0); i++)
        {
            int parent = newedges[i,0];
            int child = newedges[i,1];
        
            if (passed[parent] && !passed[child])
            {
                passed[child] = true; //* 다른 경우의 수로 넘어가기위한 부분
                if(infos[child] == 0)
                {
                    BFS(sheap+1,wolf);
                }
                else{
                    BFS(sheap,wolf+1);
                }
                passed[child] = false; //* 다른 경우의 수로 넘어가기위한 부분
            }
        }
        
        
        
    }
}
profile
주섬주섬..

1개의 댓글

comment-user-thumbnail
2023년 7월 20일

글이 많은 도움이 되었습니다, 감사합니다.

답글 달기