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

[14주차] 이지영 #197

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
baexxbin merged 8 commits into GreatAlgorithm-Study:main from yeongleej:main
Dec 15, 2024
Merged
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
41 changes: 41 additions & 0 deletions BOJ/20001-25000번/JY_20181.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.io.*;
import java.util.*;

public class JY_20181 {

public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

int N = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());

long[] arr = new long[N+1];
st = new StringTokenizer(br.readLine());
for(int i=1; i<N+1; i++) {
arr[i] = Long.parseLong(st.nextToken());
}

long[] dp = new long[N+1];
int s = 1;
int e = 1;
int sum = 0;

while(e <= N) {
sum += arr[e];
dp[e] = dp[e-1]; // 이전 값을 최대값으로 가져옴

// K보다 작아질 떄까지 s증가
while(sum >= K) {
// dp[s-1]+sum-K : 시작 위치
dp[e] = Math.max(dp[e], dp[s-1]+sum-K);
sum -= arr[s];
s++;
}
e++;
}
System.out.println(dp[N]);

}

}
23 changes: 23 additions & 0 deletions BOJ/20001-25000번/JY_20291.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.io.*;
import java.util.*;

public class JY_20291 {

public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

Map<String, Integer> fMap = new TreeMap<>();

int N = Integer.parseInt(st.nextToken());
for(int i=0; i<N; i++) {
String[] fName = br.readLine().split("\\.");
fMap.put(fName[1], fMap.getOrDefault(fName[1], 0)+1);
}

for(String k : fMap.keySet()) {
System.out.println(k+" "+fMap.get(k));
}
}

}
93 changes: 93 additions & 0 deletions BOJ/20001-25000번/JY_22944.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import java.io.*;
import java.util.*;

public class JY_22944 {

static final int INF = Integer.MAX_VALUE;
static int N, H, D;
static char[][] g;
static int[] dx = {-1, 1, 0, 0};
static int[] dy = {0, 0, -1, 1};
static boolean[][] visited;
static List<int[]> uList;
static int K;
static int ans;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

N = Integer.parseInt(st.nextToken());
H = Integer.parseInt(st.nextToken());
D = Integer.parseInt(st.nextToken());

g = new char[N][N];
int sx = -1;
int sy = -1;
int ex = -1;
int ey = -1;
K = 0;
uList = new ArrayList<>();
for(int i=0; i<N; i++) {
String line = br.readLine();
for(int j=0; j<N; j++) {
g[i][j] = line.charAt(j);
if(g[i][j] == 'S') {
sx = i;
sy = j;
}
if(g[i][j] == 'U') {
uList.add(new int[] {i, j});
K++;
}
if(g[i][j] == 'E') {
ex = i;
ey = j;
}
}
}
uList.add(new int[] {ex, ey});

ans = INF;

// DFS 탐색 진행
visited = new boolean[N][N];
visited[sx][sy] = true;
dfs(sx, sy, 0, H, 0);

if(ans == INF) System.out.println(-1);
else System.out.println(ans);


}
public static int calDist(int sx, int sy, int ex, int ey) {
return Math.abs(sx-ex) + Math.abs(sy-ey);
}
public static void dfs(int x, int y, int depth, int hp, int up) {
if(g[x][y] == 'E') {
ans = Math.min(ans, depth);
return;
}
if(depth >= ans) return;

for(int i=0; i<K+1; i++) {
int[] u = uList.get(i);
if(visited[u[0]][u[1]]) continue;

// 현재 위치 -> 다음 우산 위치의 거리
int nDist = calDist(x, y, u[0], u[1]);
if(nDist > hp+up) continue;

int nhp = hp;
// 체력이 소모 되는 경우 : 남은 우산 내구성 - 이동할 거리 + 1(다음위치도 우산이므로)
int tmp = up - nDist + 1;
if(tmp < 0) {
nhp += tmp;
}
visited[u[0]][u[1]] = true;
dfs(u[0], u[1], depth+nDist, nhp, D-1);
visited[u[0]][u[1]] = false;
}
}

}
224 changes: 224 additions & 0 deletions CodeTree/2021-2022년/JY_색깔폭탄.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
import java.io.*;
import java.util.*;

public class Main {

static int N, M;
static int[][] g;
static int[] dx = {-1, 1, 0, 0};
static int[] dy = {0, 0, -1, 1};
static boolean[][] visited;
static int rCnt;
static class Bomb implements Comparable<Bomb> {
int x, y, color;

public Bomb(int x, int y, int color) {
super();
this.x = x;
this.y = y;
this.color = color;
}

@Override
public int compareTo(Bomb other) {
if(this.x == other.x) {
// 2) 열이 작은 순
return this.y - other.y;
}
// 1) 행이 큰 순
return other.x - this.x;
}

@Override
public String toString() {
return "Bomb [x=" + x + ", y=" + y + ", color=" + color + "]";
}

}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());

g = new int[N][N];
for(int i=0; i<N; i++) {
st = new StringTokenizer(br.readLine());
for(int j=0; j<N; j++) {
g[i][j] = Integer.parseInt(st.nextToken());
}
}

int ans = 0;
while(true) {
// 1) 폭탄 묶음 찾기
List<Bomb> bList = findBundle();

// 더이상 폭탄 묶음이 없으면 종료
if(bList.size() == 0) break;

// 폭탄 묶음 점수 계산
int c = bList.size();
ans += (c*c);

// 2) 폭탄 제거 & 중력 작용
removeBomb(bList);

setGravity();

// 3) 반시계 90 회전
rotate();

// 4) 중력 작용
setGravity();

}
System.out.println(ans);

}
public static void print(int[][] a) {
for(int i=0; i<g.length; i++) {
System.out.println(Arrays.toString(g[i]));
}
System.out.println();
}
public static boolean inRange(int x, int y) {
return x>=0 && x<N && y>=0 && y<N;
}
public static List<Bomb> bfs(Bomb start) {
Queue<Bomb> q = new LinkedList<>();
List<Bomb> aList = new ArrayList<>();

visited[start.x][start.y] = true;
q.add(start);
aList.add(start);

// 빨간 폭탄 리스트
List<Bomb> rList = new ArrayList<>();

while(!q.isEmpty()) {
Bomb now = q.poll();

for(int i=0; i<4; i++) {
int nx = now.x + dx[i];
int ny = now.y + dy[i];

if(!inRange(nx, ny)) continue;
if(visited[nx][ny]) continue;
if(g[nx][ny] != 0 && g[nx][ny] != start.color) continue;

Bomb next = new Bomb(nx, ny, g[nx][ny]);
if(g[nx][ny] == 0) {
rList.add(next);
}

visited[nx][ny] = true;
q.add(next);
aList.add(next);
}
}

rCnt = rList.size();
// 빨간 폭탄들 visited 원상복귀
for(Bomb r: rList) {
visited[r.x][r.y] = false;
}

if(aList.size() <= 1) {
return new ArrayList<>();
}
// 우선순위에 맞게 정렬 후 반환
Collections.sort(aList);
return aList;

}
public static List<Bomb> findBundle() {
// 기준점 우선순위 큐 (크기->빨간폭탄->행->열)
PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if(o1[0] == o2[0]) {
if(o1[1] == o2[1]) {
if(o1[2] == o2[2]) {
// 4) 열이 작은 순
return o1[3] - o2[3];
}
// 3) 행이 큰 순
return o2[2] - o1[2];
}
// 2) 빨간 폭탄의 개수가 적은 순
return o1[1] - o2[1];
}
// 1) 묶음 크기가 큰 순
return o2[0] - o1[0];
}
});
// 기준점의 위치와 폭탄 묶음 저장 해쉬맵
Map<Integer, List<Bomb>> tMap = new HashMap<>();
visited = new boolean[N][N];
for(int i=0; i<N; i++) {
for(int j=0; j<N; j++) {
// 빨강(0), 돌(-1), 빈칸(-2) 제외 폭탄들이 대상
if(g[i][j] > 0 && !visited[i][j]) {
rCnt = 0;
List<Bomb> bList = bfs(new Bomb(i, j, g[i][j]));

if(bList.size() == 0) continue;

Bomb center = bList.get(0);
int cNum = center.x*N + center.y;
if(tMap.getOrDefault(cNum, new ArrayList<>()).size() > bList.size()) {
continue;
}
tMap.put(cNum, bList);
pq.add(new int[] {bList.size(), rCnt, center.x, center.y});
}
}
}
// pq가 비어있다면 만족하는 폭탄 묶음들 없음
if(pq.size() == 0) return new ArrayList<>();
// 가장 우선순위가 높은 기준점
int[] target = pq.poll();
return tMap.get(target[2]*N + target[3]);

}
public static void removeBomb(List<Bomb> bList) {
// -2 : 빈칸
for(Bomb b: bList) {
g[b.x][b.y] = -2;
}
}
public static void setGravity() {
for(int y=0; y<N; y++) {
for(int x=N-1; x>=0; x--) {
if(g[x][y] < 0) continue;

// 폭탄
int color = g[x][y];
g[x][y] = -2;
int t = x;
while(t < N) {
if(t+1 == N || g[t+1][y] != -2) break;
t++;
}
g[t][y] = color;
}
}
}
public static void rotate() {
int[][] t = new int[N][N];

for(int x=0; x<N; x++) {
for(int y=0; y<N; y++) {
int nx = N - y - 1;
int ny = x;
t[nx][ny] = g[x][y];
}
}

g = t;
}

}
Loading

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