알고리즘 공부/프로그래머스

(Java)프로그래머스 코딩테스트 연습 - 2017 카카오코드 본선 - 단체사진 찍기

HRuler 2020. 11. 18. 20:00

1. 문제

https://programmers.co.kr/learn/courses/30/lessons/1835?language=java

 

코딩테스트 연습 - 단체사진 찍기

단체사진 찍기 가을을 맞아 카카오프렌즈는 단체로 소풍을 떠났다. 즐거운 시간을 보내고 마지막에 단체사진을 찍기 위해 카메라 앞에 일렬로 나란히 섰다. 그런데 각자가 원하는 배치가 모두

programmers.co.kr

2. 나의 풀이

import java.util.*;

class Solution {
    public int solution(int n, String[] data) {
        int answer = 0;
        String [] element = {"A", "C", "F", "J", "M", "N", "R", "T"};
        Permutation perm = new Permutation(element.length, element.length);
        perm.permutation(element, 0);
        ArrayList<ArrayList<Object>> result = perm.getResult();
        
        System.out.println("모든 조합의 수 : " + result.size());
        
        for(int i = 0; i < result.size(); i = i + 1) {
        	boolean check = true;
        	for(int j = 0; j < data.length; j = j + 1) {
        		int imsi = Math.abs(result.get(i).indexOf(data[j].substring(0, 1)) - result.get(i).indexOf(data[j].substring(2, 3))) - 1;
        		int su = Integer.parseInt(data[j].substring(4, 5));
        		//System.out.println(result.get(i));
        		//System.out.println("절대 값 : " + imsi);
        		if(data[j].substring(3, 4).equals("=")) {
        			if(imsi != su) {
        				check = false;
        				break;
        			}
        		}else if(data[j].substring(3, 4).equals("<")){
        			if(imsi >= su) {
        				check = false;
        				break;
        			}
        		}else if(data[j].substring(3, 4).equals(">")){
        			if(imsi <= su) {
        				check = false;
        				break;
        			}
        		}
        	}
        	if(check == true) {
    			answer += 1;
    		}
        	/*
        	for(int j = 0; j < result.get(i).size(); j = j + 1) {
        		System.out.print(result.get(i).get(j));
        	}
        	System.out.println();
        	*/
        }
        return answer;
    }
}

class Permutation { 
	private int n; 
	private int r; 
	private String [] now; 
	// 현재 순열 
	private ArrayList<ArrayList<Object>> result; 
	// 모든 순열 
	public ArrayList<ArrayList<Object>> getResult() { 
		return result; 
	} 
	public Permutation(int n, int r) { 
		this.n = n; 
		this.r = r; 
		this.now = new String [r]; 
		this.result = new ArrayList<ArrayList<Object>>(); 
	} 
	public void swap(String [] arr, int i, int j) { 
		String temp = arr[i];
		arr[i] = arr[j]; 
		arr[j] = temp; 
	} 
	public void permutation(String [] arr, int depth) { 
		// 현재 순열의 길이가 r일 때 결과 저장 
		if (depth == r) { 
			ArrayList<Object> temp = new ArrayList<>(); 
			for (int i = 0; i < now.length; i++) { 
				temp.add(now[i]); 
			} 
			result.add(temp); 
			return; 
		} 
		for (int i = depth; i < n; i++) { 
			swap(arr, i, depth); 
			now[depth] = arr[depth]; 
			permutation(arr, depth + 1); 
			swap(arr, i, depth); 
		} 
	}
}

3. 다른 사람 풀이

class Solution {
  public int solution(int n, String[] data) {
        char[] s = new char[]{'A', 'C', 'F', 'J', 'M', 'N', 'R', 'T'};

        int count = 0;

        for (int a = 0; a < s.length; a++) {
            for (int b = 0; b < s.length; b++) {
                if (b == a) {
                    continue;
                }
                for (int c = 0; c < s.length; c++) {
                    if (c == a || c == b) {
                        continue;
                    }
                    for (int d = 0; d < s.length; d++) {
                        if (d == c || d == a || d == b) {
                            continue;
                        }
                        for (int e = 0; e < s.length; e++) {
                            if (e == d || e == a || e == b || e == c) {
                                continue;
                            }
                            for (int f = 0; f < s.length; f++) {
                                if (f == e || f == d || f == c || f == b || f == a) {
                                    continue;
                                }
                                for (int g = 0; g < s.length; g++) {
                                    if (g == a || g == b || g == c || g == d || g == e || g == f) {
                                        continue;
                                    }
                                    for (int h = 0; h < s.length; h++) {
                                        if (h == a || h == b || h == c || h == d || h == e || h == f || h == g) {
                                            continue;
                                        }
                                        String makeS = "" + s[a] + s[b] + s[c] + s[d] + s[e] + s[f] + s[g] + s[h];

                                        boolean isCheck = false;
                                        for (int i = 0; i < data.length; i++) {
                                            char first = data[i].charAt(0);
                                            char second = data[i].charAt(2);
                                            char inequality = data[i].charAt(3);
                                            int distance = data[i].charAt(4) - 48;

                                            int firstIndex = makeS.indexOf(first);
                                            int secondIndex = makeS.indexOf(second);
                                            switch (inequality) {
                                                case '=': {
                                                    if (Math.abs(firstIndex - secondIndex) == distance + 1) {
                                                        isCheck = true;
                                                    } else
                                                        isCheck = false;
                                                    break;
                                                }
                                                case '>': {
                                                    if (Math.abs(firstIndex - secondIndex) > distance + 1) {
                                                        isCheck = true;
                                                    } else
                                                        isCheck = false;
                                                    break;
                                                }
                                                case '<': {
                                                    if (Math.abs(firstIndex - secondIndex) < distance + 1) {
                                                        isCheck = true;
                                                    } else
                                                        isCheck = false;
                                                    break;
                                                }
                                            }
                                            if (isCheck == false) {
                                                break;
                                            }
                                        }
                                        if (isCheck) {
                                            count++;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            }
        }
        return count;
  }
}