Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[9주차] 이혜원 #120

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

Merged
icegosimperson merged 10 commits into GreatAlgorithm-Study:main from icegosimperson:main
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit Hold shift + click to select a range
4c8c7de
이혜원: [PG] 64062 징검다리 건너기_241104
icegosimperson Nov 4, 2024
3c6041d
docs: HW_64062 클래스명 변경
icegosimperson Nov 4, 2024
7f4a247
이혜원: [CT] 전투 로봇_241104
icegosimperson Nov 4, 2024
77e5f80
이혜원: [BOJ] 2660 회장 뽑기_241105
icegosimperson Nov 5, 2024
19c621b
이혜원: [SQL] 분기별 분화된 대장균의 개체 수 구하기_241105
icegosimperson Nov 5, 2024
b706e75
이혜원: [BOJ] 2098 외판원 순회_241106
icegosimperson Nov 6, 2024
89ce22c
이혜원: [SQL] 물고기 종류 별 대어 찾기_241107
icegosimperson Nov 7, 2024
1c57be0
이혜원: [BOJ] 13164 행복 유치원_241108
icegosimperson Nov 8, 2024
ac3f04e
이혜원: [BOJ] 14620 꽃길_241108
icegosimperson Nov 8, 2024
bd31870
이혜원: [PG] 118668 코딩 테스트 공부_241107
icegosimperson Nov 9, 2024
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
44 changes: 44 additions & 0 deletions BOJ/1000-5000번/HW_2098.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
// 시간 복잡도 : 2<=N<=16 -> 16! 완전 탐색 불가 -> DP + 비트마스크 -> 16* 2^16 => O(N * 2 ^N) 가능
// 가장 적은 비용을 들이는 외판원의 순회 여행 경로를 구하는 프로그램
// 리스트 데이터를 변수 1개에 저장 -> bit(이진수 표현)
public class HW_2098 {
static int N; // 도시의 개수
static int[][] W; // 도시 간의 이동 비용
static int[][] d; // 최소 비용 저장
static final int INF = Integer.MAX_VALUE >> 2;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
N = Integer.parseInt(br.readLine());
W = new int[16][16]; // 비용 저장 배열
d = new int[16][1<<16]; // 최소 비용
for(int i=0; i<N; i++){
st = new StringTokenizer(br.readLine());
for(int j=0; j<N; j++){
W[i][j] = Integer.parseInt(st.nextToken());
}
}
System.out.println(tsp(0, 1));
}

public static int tsp(int c, int v) {
if(v==(1<<N)-1){ // 모든 노드를 방문할 경우
return W[c][0] == 0 ? INF : W[c][0]; // 시작 도시(0)로 돌아감 (돌아갈 수 없으면 INF 반환)
}
if(d[c][v] !=0){ // 이미 방문한 노드라면
return d[c][v]; // 바로 반환
}
int min_val = INF;
for(int i=0; i<N; i++){
if((v&(1<<i))==0 && W[c][i] !=0){ // 방문한 적 없고 갈 수 있는 노드라면
min_val = Math.min(min_val, tsp(i, (v | (1 << i))) + W[c][i]); // 현재도시(c) -> 다음 도시(i) 최소 비용 계산
}
}
d[c][v] = min_val;
return d[c][v];
}
}
Copy link
Contributor

@baexxbin baexxbin Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전반적으로 코드를 깔끔하게 짜셔서 이해하기 쉬운거같아요!!😁
저는 코드를 짜면서 int min_val = INF 부분을 놓쳤었는데, min_val를 선언하고 계산이 다 끝나고 d[c][v] = min_val로 넣는게 뭔가 깔끔하고 이해하기 좋은것같습니다👍

icegosimperson reacted with heart emoji
75 changes: 75 additions & 0 deletions BOJ/1000-5000번/HW_2660.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import java.io.*;
import java.util.*;

// 회장의 점수와 회장이 될 수 있는 모든 사람을 찾는 프로그램 작성

// 점수 : 특정 회원이 다른 모든 회원과 연결되기 위해 필요한 최단 거리 중에서 가장 먼 거리
// 회장 : 점수가 가장 낮은 사람(가장 가까운 거리에 있는 사람 = 최단거리) 구해야 함
// 조건 : 몇 사람을 통하면 모두가 서로알 수 있다 = 모든 회원(노드)가 연결되어 있음 -> 플로이드

// 시간복잡도 : O(V^3) (50^3 : 가능)
public class HW_2660 {
static final int INF = Integer.MAX_VALUE >> 2;
static int N;
static int[][] graph;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
N = Integer.parseInt(br.readLine());
graph = new int[N+1][N+1];

// 배열 초기화
for(int i=1; i<=N; i++) {
Arrays.fill(graph[i], INF);
graph[i][i] = 0; // 자기 자신과의 거리 0
}

while(true) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
if(a==-1 && b==-1) break; // 종료 조건
graph[a][b] = graph[b][a] = 1; // 양방향 그래프
}

// 플로이드 와샬
for(int k=1; k<=N; k++) {
for(int i=1; i<=N; i++) {
for(int j=1; j<=N; j++) {
if(graph[i][j] > graph[i][k] + graph[k][j]) {
graph[i][j] = graph[i][k] + graph[k][j];
}
}
}
}

int min = INF;
int[] scores = new int[N+1];

// 각 회원의 점수를 계산
for(int i=1; i<=N; i++) {
int score = 0;
for(int j=1; j<=N; j++) {
if(graph[i][j] != INF) {
score = Math.max(score, graph[i][j]);
}
}
scores[i] = score;
min = Math.min(min, score); // 최소 점수 갱신
}

// 회장 찾기(최소 점수)
ArrayList<Integer> candidates = new ArrayList<>();
for(int i=1; i<=N; i++) {
if(scores[i] == min) {
candidates.add(i);
}
}

System.out.println(min + " " + candidates.size());
for(int i=0; i<candidates.size(); i++) {
System.out.print(candidates.get(i) +" ");
}
}
}

94 changes: 94 additions & 0 deletions CodeTree/2017-2018년/HW_전투_로봇.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import java.io.*;
import java.util.*;

public class HW_전투_로봇 {
static int n, m;
static int[][] board;
static boolean[][] visited;
static int[] dx = {1, 0, -1, 0};
static int[] dy = {0, 1, 0, -1};
static int rbX, rbY;
static int rblevel = 2; // 초기 로봇 레벨 : 2
static int monsters = 0, time = 0;
public static class Node{
int x, y, d;
Node(int x, int y, int d){
this.x = x;
this.y = y;
this.d = d; // 최단거리 기준으로 몬스터를 잡아야하기에
}
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
board = new int[n][n];
for(int i=0; i<n; i++){
StringTokenizer st = new StringTokenizer(br.readLine());
for(int j=0; j<n; j++){
board[i][j] = Integer.parseInt(st.nextToken());
if(board[i][j]==9) {// 전투 로봇 lv9
rbX = i;
rbY = j;
board[i][j] = 0; // 로봇 위치->빈칸 처리
}
}
}
while(true){
Node target = bfs();
if(target==null){
break;
}
// 로봇 이동 및 시간 증가
rbX = target.x;
rbY = target.y;
time += target.d;

board[rbX][rbY] = 0;
monsters++;
if(monsters == rblevel){
rblevel++;
monsters = 0;
}
}
System.out.println(time);
}
public static Node bfs(){ // 잡을 수 있는 몬스터 찾기
Queue<Node> queue = new LinkedList<>();
visited = new boolean[n][n];
queue.add(new Node(rbX, rbY, 0)); // 초기값
visited[rbX][rbY] = true; // 방문 처리
List<Node> temp = new ArrayList<>(); // 잡을 수 있는 몬스터를 저장

while(!queue.isEmpty()){
Node cur = queue.poll();
for(int i=0; i<4; i++){
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];
if(check(nx, ny) && !visited[nx][ny] && board[nx][ny] <= rblevel){
visited[nx][ny] = true; // 방문 처리
if(board[nx][ny]>0 && board[nx][ny] <rblevel){
temp.add(new Node(nx, ny, cur.d+1)); // 후보로 추가
}
queue.add(new Node(nx, ny, cur.d+1)); // 다음 위치로
}
}
}
if(temp.isEmpty()){
return null;
}
// 가장 가까운 거리의 몬스터를 없애기 위해 정렬
temp.sort((a, b) ->{
if(a.d == b.d){ // 가장 가까운 거리의 없앨 수 있는 몬스터가 하나 이상이라면
if(a.x == b.x){
return Integer.compare(a.y, b.y); // 가장 위에 존재하는 몬스터
}
return Integer.compare(a.x, b.x); // 가장 왼쪽에 존재하는 몬스터부터
}
return Integer.compare(a.d, b.d); // 거리가 가장 가까운 몬스터
});
return temp.get(0); // 가장 우선순위가 높은 타겟 없앰
}
public static boolean check(int x, int y){
return x >=0 && y >=0 && x < n && y <n;
}
}
35 changes: 35 additions & 0 deletions Programmers/Level3/HW_64062.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 시간 복잡도 : stones.length<=200_000 -> O(N^2) 불가
// 완전 탐색 -> 시간 초과 -> 이분 탐색으로 탐색 범위 줄여줌 -> O(N logN)

// 징검다리를 건널 수 있는 최대 니니즈 친구들의 수 구하기
// 한명씩 징검다리를 건너는 경우하여 돌을 줄이는 과정 -> 시간초과
// 이분탐색으로 특정 인원이 건널 수 있는지 여부만 확인
class HW_64062 {
public int solution(int[] stones, int k) {
int answer = 0;
int start = 0;
int end = Integer.MAX_VALUE;

while(start <= end){
int mid = (start + end)/2; // 건널 수 있는 사람의 수
if(check(mid, k, stones)){ // true (mid명까지 건널 수 있는 경우)
answer = mid;
start = mid+1; // (mid+1)명까지 건널 수 있는지 확인
} else{ // false
end = mid-1;
}
}
return answer;
}
public boolean check(int mid, int k, int[] stones){ // mid명이 건널 수 있는지 판단
int cnt = 0; // 연속으로 건널 수 없는 돌의 개수
for(int i = 0; i < stones.length; i++) {
if(stones[i] < mid) { // 현재 돌에 mid명이 건너려고할 때
cnt++;
if(cnt>=k) // 연속으로 건널 수 없는 돌이 k개 이상일 경우
return false;
} else cnt=0; // 연속성 끊김
}
return true;
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- https://school.programmers.co.kr/learn/courses/30/lessons/299308
SELECT CONCAT(QUARTER(DIFFERENTIATION_DATE), 'Q') AS QUARTER, COUNT(ID) AS ECOLI_COUNT
FROM ECOLI_DATA
GROUP BY QUARTER
ORDER BY QUARTER;

AltStyle によって変換されたページ (->オリジナル) /