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주차] 이혜원 #194

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 icegosimperson: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
36 changes: 36 additions & 0 deletions BOJ/20001-25000번/HW_20181.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class HW_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()); // 최소 만족도
int[] arr = new int[N];
long[] dp = new long[N+1]; // dp[i] : i번쨰 먹이까지의 최대 탈피 에너지

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

long sum = 0; // 만족도 합
int left = 0;

for(int right=1; right <=N; right++){
sum += arr[right-1];

while(K <= sum){
dp[right] = Math.max(dp[right], dp[left] + sum - K); // 탈피 에너지 계산
sum -= arr[left];
left++; // 구간 축소
}
dp[right] = Math.max(dp[right], dp[right - 1]); // 최대 탈피 에너지값 출력
}
System.out.println(dp[N]);
}
}
23 changes: 23 additions & 0 deletions BOJ/20001-25000번/HW_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 HW_20291 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());

TreeMap<String, Integer> words = new TreeMap<>();

for(int i=0; i<N; i++) {
String input = br.readLine();
String extension = input.substring(input.lastIndexOf('.')+1);

words.put(extension, words.getOrDefault(extension, 0)+1);
}

for(Map.Entry<String, Integer> entry : words.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}

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

public class HW_22944 {
static int N, H, D;
static char[][] board;
static int[][] check;
static int[] dx = {-1, 1, 0, 0};
static int[] dy = {0, 0, -1, 1};

static class Node {
int x, y, h, d, m;

public Node(int x, int y, int h, int d, int m) {
this.x = x;
this.y = y;
this.h = h; // 현재 체력
this.d = d; // 우산 내구도
this.m = m; // 이동 횟수
}
}

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());

board = new char[N][N];
check = new int[N][N];

int startX = 0, startY = 0;
for (int i = 0; i < N; i++) {
String line = br.readLine();
for (int j = 0; j < N; j++) {
board[i][j] = line.charAt(j);
if (board[i][j] == 'S') {
startX = i;
startY = j;
}
}
}

for (int i = 0; i < N; i++) {
Arrays.fill(check[i], -1);
}

int result = bfs(startX, startY);
System.out.println(result);
}

public static int bfs(int sx, int sy) {
Queue<Node> queue = new LinkedList<>();
queue.add(new Node(sx, sy, H, 0, 0)); // 시작점 추가
check[sx][sy] = H; // 시작점 방문 처리

while (!queue.isEmpty()) {
Node cur = queue.poll();

for (int i = 0; i < 4; i++) {
int newH = cur.h, newD = cur.d, newM = cur.m;
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];


if (!isValid(nx, ny))
continue;

if (board[nx][ny] == 'E') { // 안전지대에 도달하면 이동 횟수 반환
return newM + 1;
}

if (board[nx][ny] == 'U')
newD = D;

if (newD > 0) {
newD--;
} else {
newH--;
}

if (newH == 0)
continue;

if (check[nx][ny] < newH + newD) {
check[nx][ny] = newH + newD;
queue.add(new Node(nx, ny, newH, newD, newM + 1));
}
}
}
return -1; // 안전지대에 도달하지 못한 경우
}

public static boolean isValid(int nx, int ny) {
return 0<= nx && nx < N && 0 <=ny && ny < N;
}
}
179 changes: 179 additions & 0 deletions CodeTree/2021-2022년/HW_색깔_폭탄.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import java.io.*;
import java.util.*;

public class HW_색깔_폭탄 {
static int n, m;
static int[][] board;
static int score = 0;
static int[] dx = {-1,1,0,0};
static int[] dy = {0,0,-1,1};

static class Bomb {
int size;
int redCount;
int stdX;
int stdY;
List<int[]> blocks; // 폭탄 묶음 내 좌표

Bomb(int size, int redCount, int stdX, int stdY, List<int[]> blocks) {
this.size = size;
this.redCount = redCount;
this.stdX = stdX;
this.stdY = stdY;
this.blocks = blocks;
}
}

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());
board = new int[n][n];

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

while (true) {
Bomb group = find();
if (group == null || group.size < 2) {
break;
}
score += group.size * group.size;
remove(group);
gravity();
board = rotate(board);
gravity();
}

System.out.println(score);
}

static Bomb find() {
List<Bomb> candidates = new ArrayList<>();

for (int i=0; i<n; i++){
for (int j=0; j<n; j++){
int color = board[i][j];
if (0 < color && color <= m) {
Bomb bg = bfs(i, j, color);
if (bg != null && 2 <=bg.size) {
candidates.add(bg);
}
}
}
}
if (candidates.isEmpty()){
return null;
}
// 우선순위 정렬
candidates.sort((a,b)-> {
if (a.size != b.size) // 1. size 내림차순
return b.size - a.size;
if (a.redCount != b.redCount) // 2. redCount 오름차순
return a.redCount - b.redCount;
if (a.stdX != b.stdX) // 3. 기준 행 내림차순
return b.stdX - a.stdX;
return a.stdY - b.stdY; // 4. 기준 열 오름차순
});

return candidates.get(0);
}

static Bomb bfs(int sx, int sy, int color) {
boolean[][] visited = new boolean[n][n];
Queue<int[]> q = new LinkedList<>();
q.offer(new int[]{sx, sy});
visited[sx][sy] = true;

List<int[]> group = new ArrayList<>();
group.add(new int[]{sx, sy});
int redCount = (board[sx][sy] == 0) ? 1 : 0;

while(!q.isEmpty()){
int[] cur = q.poll();
int x=cur[0], y=cur[1];

for (int k=0; k<4; k++){
int nx = x + dx[k];
int ny = y + dy[k];
if (!isValid(nx, ny)) {
continue;
}
if (board[nx][ny] == -1) { // 검은 돌 제외
continue;
}
if (!visited[nx][ny]) {
if (board[nx][ny] == color || board[nx][ny] == 0) {
visited[nx][ny] = true;
q.offer(new int[]{nx,ny});
group.add(new int[]{nx,ny});
if (board[nx][ny] == 0) {
redCount++;
}
}
}
}
}

if (group.size()<2) return null;


int targetX=-1;
int targetY=-1;
for (int i=0; i<group.size(); i++) {
int gx = group.get(i)[0];
int gy = group.get(i)[1];
if (board[gx][gy]!=0) { // 기준점 찾기: 빨간 아닌 폭탄 중 x 최대, x 같다면 y 최소
if (gx > targetX || (gx==targetX && (targetY==-1 || gy < targetY))) {
targetX = gx;
targetY = gy;
}
}
}
return new Bomb(group.size(), redCount, targetX, targetY, group);
}

static void remove(Bomb b) {
for (int i=0; i<b.blocks.size(); i++) {
int x=b.blocks.get(i)[0];
int y=b.blocks.get(i)[1];
board[x][y] = -2; // 빈칸 처리
}
}

static void gravity() {
for (int y=0; y<n; y++){
for (int x=n-1; x>=0; x--){
if (board[x][y] > -1) {
int nx = x;
while(true) {
int down = nx+1;
if (down>=n) break;
if (board[down][y]!=-2) break;
board[down][y]=board[nx][y];
board[nx][y]=-2;
nx=down;
}
}
}
}
}
static int[][] rotate(int[][] arr) {
int[][] newBoard = new int[n][n];
for (int x=0; x<n; x++){
for (int y=0; y<n; y++){
newBoard[n-1-y][x]=arr[x][y];
}
}
return newBoard;
}
static boolean isValid(int nx, int ny){
return 0 <= nx && nx < n && 0 <= ny && ny < n;
}
}
Loading

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