You will be fine

10. 단체사진찍기 JAVA (프로그래머스)

by BFine
반응형

가. 문제파악

 1. 유형 : 브루트포스

    -   이 문제는 제한이 크지 않기때문에 모든 경우의 수를 확인해주면 된다.

 

나. 코드 

 1. 풀이 : 모든 경우의 수를 구하자 

    -   이문제의 채점의 경우 하나로 여러번 실행하기 때문에 반드시 모든변수의 초기화를 메서드내에서 해야한다.

          => 테케는 맞는데 틀리는 경우 '왜틀렸지?' 하는 늪에 빠질수있다..    

    -   여기서 거리는 사이에 낀 캐릭터의 수이다 그러므로 -1 보정이 필요하다 ( 문제를 꼼꼼히 읽자....)  

import java.util.ArrayList;
import java.util.List;

public class Solution {

    public int solution(int n, String[] data) {
        String[] in = {"A", "C", "F", "J", "M", "N", "R", "T"};
        visited = new boolean[in.length];
        count = 0;
        list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(new Con(data[i].charAt(0)+"",data[i].charAt(2)+""
            ,data[i].charAt(3),Integer.parseInt(data[i].charAt(4)+"")));
        }
        dfs("",0,in.length,in);

        return count;
    }
    private static boolean[] visited;
    static List<Con> list;
    private static int count;
    public void dfs(String sum,int depth,int length,String[] in){
        if(depth == length){
              for(Con c : list) {
                  int a = sum.indexOf(c.name1);
                  int b = sum.indexOf(c.name2);
                  int cal = Math.abs(a - b) - 1;
                  if (c.bo == '>') {
                      if (cal <= c.su) {
                          return;
                      }
                  }
                  if (c.bo == '<') {
                      if (cal >= c.su) {
                          return;
                      }
                  }

                  if (c.bo == '=') {
                      if (cal != c.su) {
                          return;
                      }
                  }
              }
              count++;
        }else {
            for (int i = 0; i <length; i++) {
                if(!visited[i]){
                    visited[i] = true;
                    dfs(sum+in[i],depth+1,length,in);
                    visited[i] = false;
                }
            }
        }

    }
    
    
    static class Con{
       String name1;
       String name2;
       char bo;
       int su;
        public Con(String name1, String name2, char bo, int su) {
            this.name1 = name1;
            this.name2 = name2;
            this.bo = bo;
            this.su = su;
        }
    }
    public static void main(String[] args) {
//        System.out.println(new Solution().solution(new int[][]{{1,1,0,0},{1,0,0,0},{1,0,0,1},{1,1,1,1}}));
//        System.out.println(Arrays.toString(new Solution().solution(new int[][]{{1, 1, 0, 0}, {1, 0, 0, 0}, {1, 0, 0, 1}, {1, 1, 1, 1}})));
        System.out.println(new Solution().solution(2,new String[]{"N~F=0", "R~T>2"}));
       // System.out.println(new Solution().solution(new int[][]{{0,0,1,1},{1,1,1,1}}));
//        System.out.println(new Solution().solution(2,4,2,1));
    }
}
반응형

블로그의 정보

57개월 BackEnd

BFine

활동하기