Skip to content

Week29 Study #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions 2022/[Week29 - Mix]/김주현/Q1987.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// 1987번 알파벳 (G4) [백트래킹]
/*
<문제 정보>
1.

<변수 범위>
1.

<프로그램 진행>
1.

<필요 함수>
1.

*/


import java.io.*;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

public class Q1987 {
static int N;
static int M;
static int answer = -1;
static char[][] map;
static boolean[] visitedAlphabet = new boolean[26];

static int[][] DIR = {{1,0}, {0,1}, {-1,0}, {0,-1}};

public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
StringTokenizer st;

// ******************** 입력 & 초기화 ********************
st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
map = new char[N][M];
for (int i=0; i<N; i++) {
map[i] = br.readLine().toCharArray();
}

// ******************** 메인 로직 ********************

visitedAlphabet[alphabetToIndex(map[0][0])] = true;
dfs(1,0,0);

// ******************** 정답 출력 ********************
sb.append(answer);
bw.write(sb.toString());
bw.flush();
bw.close();
br.close();
}

public static void dfs(int depth, int x, int y) {
// System.out.printf("[IN] depth : %d, x : %d, y : %d Alphabet : %c \n", depth, x, y, map[x][y]);
int nextX, nextY;
boolean inRange;
boolean hasPossibleRoad = false;
for (int[] d : DIR) {
nextX = x + d[0];
nextY = y + d[1];
inRange = nextX >= 0 && nextY >=0 && nextX < N && nextY < M;
if (inRange && !visitedAlphabet[alphabetToIndex(map[nextX][nextY])]) {
int alphabetIndex = alphabetToIndex(map[nextX][nextY]);
hasPossibleRoad = true;
visitedAlphabet[alphabetIndex] = true;
dfs(depth+1, nextX, nextY);
visitedAlphabet[alphabetIndex] = false;
}
}

if (!hasPossibleRoad) {
answer = Math.max(answer, depth);
// System.out.printf("[OUT - No Path] depth : %d, x : %d, y : %d Alphabet : %c \n", depth, x, y, map[x][y]);
return;
}
// System.out.printf("[OUT - All Searched] depth : %d, x : %d, y : %d Alphabet : %c \n", depth, x, y, map[x][y]);
}

public static int alphabetToIndex (char alphabet) {
return alphabet - 'A';
}
}
60 changes: 60 additions & 0 deletions 2022/[Week29 - Mix]/김주현/Q2212.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 2212번 센서 (G5) [정렬]
/*
<문제 정보>
1.

<변수 범위>
1.

<프로그램 진행>
1.

<필요 함수>
1.

*/


import java.io.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;

public class Q2212 {

public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
StringTokenizer st;

// ******************** 입력 & 초기화 ********************
int N = Integer.parseInt(br.readLine());
int K = Integer.parseInt(br.readLine());
int[] sensors = new int[N];
st = new StringTokenizer(br.readLine());
for (int i=0; i<N; i++) {
sensors[i] = Integer.parseInt(st.nextToken());
}

// ******************** 메인 로직 ********************
Arrays.sort(sensors);
Integer[] diffs = new Integer[N-1];
for (int i=0; i<N-1; i++) {
diffs[i] = sensors[i+1] - sensors[i];
}

Arrays.sort(diffs, Collections.reverseOrder());
int coverage = sensors[N-1] - sensors[0];
for (int i=0; i<Math.min(K-1, N-1); i++) {
coverage -= diffs[i];
}

// ******************** 정답 출력 ********************
sb.append(coverage);
bw.write(sb.toString());
bw.flush();
bw.close();
br.close();
}
}
86 changes: 86 additions & 0 deletions 2022/[Week29 - Mix]/김주현/Q2668.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// 2668번 숫자 고르기 (G5) [그리디]
/*
<문제 정보>
1.

<변수 범위>
1.

<프로그램 진행>
1.

<필요 함수>
1.

*/


import java.io.*;
import java.util.*;

public class Q2668 {
static int N;
static int[] arr;
static boolean[] visited;
static int answer = 0;

public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
StringTokenizer st;

// ******************** 입력 & 초기화 ********************
N = Integer.parseInt(br.readLine());
arr = new int[N+1];
for (int i=1; i<=N; i++) {
arr[i] = Integer.parseInt(br.readLine());
}

// ******************** 메인 로직 ********************
visited = new boolean[N+1];
for (int i=1; i<=N; i++) {
if (!visited[i]) {
search(i);
}
}

// ******************** 정답 출력 ********************
sb.append(answer).append("\n");
for (int i=1; i<=N; i++) {
if (visited[i]) sb.append(i).append("\n");
}

bw.write(sb.toString());
bw.flush();
bw.close();
br.close();
}

public static void search (int n) {
int start = n;
int end = 0;
Set<Integer> visitList = new HashSet<>();
boolean isOriginalPoint = false;

while ( !isOriginalPoint ) {
end = arr[start];
if ( visited[end] || visitList.contains(end) ) break;
visitList.add(end);
if (end == n) {
isOriginalPoint = true;
} else if ( start == end) {
break;
} else {
start = end;
}
}

if (isOriginalPoint) {
answer += visitList.size();
for (int point : visitList) {
visited[point] = true;
}
}
}
}
Empty file removed 2022/[Week29 - Mix]/김주현/temp
Empty file.