목록분류 전체보기 (273)
Dev.baelanche

public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int card[] = new int[n]; for(int i=0; i

메달 개수 정보를 담은 클래스 생성 > 금, 은, 동메달 수에 따른 정렬 > 등수 구함 순으로 풀었다. 코드가 쓸데없이 길지만 구현만 하면 되는 문제라 설계만 잘하면 술술 풀리는 문제이다. public class Main { static class Country { int rank; int num; int gold; int silver; int bronze; Country(int num, int gold, int silver, int bronze) { this.num = num; this.gold = gold; this.silver = silver; this.bronze = bronze; } } public static void main(String[] args) { Scanner sc = new Sca..

인접노드 탐색 문제이다. 높이 정보를 조건으로 주어야 한다는 것만 추가해서 풀면된다. 이처럼 높이 조건을 돌려보면서 컴포넌트 개수 중 최고 값을 찾으면 된다. visited 배열을 반복적으로 초기화하면서 DFS(높이 조건) 와 컴포넌트 세는 DFS 를 따로 수행했다. (참 지저분하다) public class Main { static int n; static int a[][]; static int temp[][]; static boolean visited[][]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); a = new int[n][n]; visited = new boolean[..

이차원 배열을 짜서 DFS 를 수행하면 된다. 노드에 간선이 있을 경우 노드를 계속 타고 들어가며 방문한 노드 배열 정보를 1로 표시한다. public class Main { static int n; static int a[][]; static int path[][]; static boolean visited[]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); a = new int[n+1][n+1]; path = new int[n+1][n+1]; visited = new boolean[n+1]; for(int i=1; i

탐색을 여러번 할뿐 컴포넌트 개수를 세는건 여타 기본 DFS 문제와 똑같다. 필자는 R, G, B 탐색 후 R, G 는 같은 값으로 배열을 채워 넣어 풀었다. public class Main { static int n; static String a[][]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); a = new String[n][n]; for(int i=0; i

영역이 아닌 곳을 탐색하여 컴포넌트의 크기를 구하면 된다. 좌표가 배열의 인덱스와 반전되니 그 부분을 신경써서 풀자. public class Main { static int asc[] = new int[5001]; static int a[][]; static int m; static int n; public static void main(String[] args) { Scanner sc = new Scanner(System.in); m = sc.nextInt(); n = sc.nextInt(); a = new int[n][m]; int k = sc.nextInt(); while(k-->0) { int x1 = sc.nextInt(); int y1 = sc.nextInt(); int x2 = sc.next..

그래프에서 컴포넌트의 개수와 각 컴포넌트의 크기를 구하는 문제이다. public class Main { static int n; static int a[][]; static int asc[] = new int[625]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); a = new int[n][n]; for(int i=0; i

그래프 컴포넌트 중 가장 큰 컴포넌트의 크기를 구하는 간단한 문제이다. public class Main { static int n; static int m; static int a[][]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); m = sc.nextInt(); a = new int[n+1][m+1]; int k = sc.nextInt(); int max = 0; while(k-->0) a[sc.nextInt()][sc.nextInt()] = 1; for(int i=1; i