본문 바로가기

알고리즘

상자색칠

문제


면이 6개인 상자가 있다. 이를 여러 가지 색으로 칠하려 하는데, 단 조건이 있다. 인접한 면에 같은 색깔을 칠하면 안 된다는 것이다. 여러 가지 색깔이 주어졌을 때, 그 색깔들로 조건을 만족하여 상자의 모든 면을 칠할 수 있는지 판별하는 프로그램을 작성하시오.

입력


첫째 줄에 색깔의 개수 N ( 1 <= N <= 1,000 ) 이 주어진다. 둘째 줄에 색깔을 나타내는 N개의 숫자가 주어진다. 색깔은 양의 정수로 이루어져 있고, 1부터 N까지의 범위의 수이다.

출력


조건을 만족하면서 상자를 칠할 수 있으면 “YES”, 아니면 “NO”를 출력한다.

 

예제 입력/출력

6 1 2 1 2 1 3

NO

예제 입력/출력

6 1 2 3 4 5 6

YES

예제 입력/출력

6 1 2 3 1 2

YES

예제 입력/출력

8 1 2 2 2 1 1 1 3

NO

 

import java.util.Scanner;

public class Main
{

    public static int MAX=1001;
    public static int color[] = new int[MAX];
    public static int num;
    public static int kind=0;
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);

        num = scan.nextInt();//숫자의 개수

        inputColor(scan);

        calculateKind();
//        System.out.println("kind:"+kind);
        decideYesOrNo();


        scan.close();
    }

    private static void decideYesOrNo()
    {

        if(num<=5)
        {
            System.out.println("NO");
            return;
        }
        if(kind>=5)
        {
            System.out.println("YES");
            return;
        }
        if(kind<=2)
        {
            System.out.println("NO");
            return;
        }
        if(kind==3)
        {
           if(count())
           {
               System.out.println("YES");
               return;
           }
           else
           {
               System.out.println("NO");
               return;
           }
        }
        if(kind==4)
        {
            if(count2())
            {
                System.out.println("YES");
                return;
            }
            else
            {
                System.out.println("NO");
                return;
            }
        }

    }
    private static boolean count2()
    {
        boolean result = true;
        int temp = 0;
        for(int i=0;i<MAX;i++)
        {
            if(color[i]==1)
            {
                temp=temp+1;
            }
            
            if(temp==3)
            {
                result=false;
                break;
            }
        }
        return result;
        
    }
    
    
    private static boolean count()
    {
        boolean result = true;
        for(int i=0;i<MAX;i++)
        {
            if(color[i]==1)
            {
                result=false;
                break;
            }
        }
        return result;
    }
    
    private static void calculateMax()
    {
        boolean result = true;
        for(int i=0;i<MAX;i++)
        {
            if(color[i]>=3)
            {
                result=false;

            }
        }
    }

    private static void calculateKind()
    {
        for(int i=0;i<MAX;i++)
        {
            if(color[i]!=0)
            {
                kind++;
            }
        }
    }

    private static void inputColor(Scanner scan)
    {
        for(int i=0;i<num;i++)
        {
            int x = scan.nextInt();
            color[x] = color[x]+1;
        }
    }
}

 

'알고리즘' 카테고리의 다른 글

inequal  (0) 2019.08.01
Class President  (0) 2019.08.01
Seat  (0) 2019.08.01
Offset  (0) 2019.08.01
소수판별2  (0) 2019.04.16