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

[17주차] 고다혜 #233

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 9 commits into main from dahye
Jan 12, 2025
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
83 changes: 83 additions & 0 deletions BOJ/1000-5000번/DH_1765.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import java.io.*;
import java.util.*;

/*
* 닭싸움 팀 정하기
*/

public class DH_1765 {
static class Node {
int e;
boolean isFriend;

public Node(int e, boolean isFriend) {
this.e = e;
this.isFriend = isFriend;
}
}
static boolean[] check;
static ArrayList<Node> adj[];
static int groupCnt;

public static void main(String[] args) throws Exception {
initInput();
solution();
}

static void solution() {
for(int i = 1; i < check.length; i++) {
if(check[i]) continue;
groupCnt += 1;
dfs(i);
}

System.out.println(groupCnt);
}

static void dfs(int node) {
check[node] = true;

for(Node next: adj[node]) {
// 현재 사람의 바로 옆 사람이 친구라면 같은 팀을 할 수 있음
if(next.isFriend) {

if(check[next.e]) continue;
dfs(next.e);

}
// 현재 사람의 바로 옆 사람이 원수라면
else {
// 옆사람과 옆옆사람이 원수일 때, 현재 사람과 같은 팀을 할 수 있음
for(Node nnext: adj[next.e]) {
if(check[nnext.e] || nnext.isFriend) continue;
dfs(nnext.e);
}
}
}
}

static void initInput() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;

int n = Integer.parseInt(br.readLine());
check = new boolean[n + 1];
adj = new ArrayList[n + 1];

for(int i = 0; i < adj.length; i++) adj[i] = new ArrayList<Node>();

int m = Integer.parseInt(br.readLine());

for(int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine());

boolean isFriend = st.nextToken().charAt(0) == 'F';

int p = Integer.parseInt(st.nextToken());
int q = Integer.parseInt(st.nextToken());

adj[p].add(new Node(q, isFriend));
adj[q].add(new Node(p, isFriend));
}
}
}
47 changes: 21 additions & 26 deletions BOJ/1000-5000번/DH_2258.java
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -6,66 +6,61 @@
*/

public class DH_2258 {

static class Node implements Comparable<Node> {
int w, c;

public Node(int w, int c) {
this.w = w;
this.c = c;
}

public Node(int w, int c, int sum) {
this.w = w;
this.c = c;
}

@Override
public int compareTo(Node o) {
if (this.c != o.c)
return Integer.compare(this.c, o.c); // 가격 오름차순
if(this.c != o.c) return Integer.compare(this.c, o.c); // 가격 오름차순
return Integer.compare(o.w, this.w); // 무게 내림차순
}
}

static final int INF = Integer.MAX_VALUE;

static final Long INF = Long.MAX_VALUE;

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

PriorityQueue<Node> pq = new PriorityQueue<Node>();

int N = Integer.parseInt(st.nextToken()); // 덩어리의 개수
long M = Integer.parseInt(st.nextToken()); // 필요한 고기의 양

for(int r = 0; r < N; r++) {
for(int r = 0; r < N; r++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());

pq.add(new Node(a, b));
}

int result = INF;
long result = INF;

long totalSum = 0;
int totalCost = 0, same = 0, markCost = -1;

while (!pq.isEmpty()) {
long totalCost = 0;
int same = 0, markCost = -1;

while(!pq.isEmpty()) {
Node current = pq.poll();

if(markCost != current.c) {
if(markCost != current.c) {
markCost = current.c;
same = 0;
} else same += current.c;

totalSum += current.w;
totalCost = current.c;
if(totalSum >= M)
if(totalSum >= M) {
result = Math.min(result, totalCost + same);
}
}

System.out.println(result == INF ? -1: result);
System.out.println(result == INF ? -1: result);
}
}
71 changes: 71 additions & 0 deletions BOJ/1000-5000번/DH_3109.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import java.io.*;
import java.util.*;

/*
* 빵집
*/

public class DH_3109 {
static int[] dr = {-1, 0, 1}, dc = {1, 1, 1};
static int R, C, cnt;
static char[][] map;
static boolean flag;

public static void main(String[] args) throws Exception {
initInput();
solution();

System.out.println(cnt);
}

static void solution() {
for(int r = 0; r < R; r++) {
flag = false;
dfs(r, 0); // (r, 0)에서 dfs 시작
}
}

static void dfs(int r, int c) {
if(c == C - 1) {
cnt += 1;
flag = true;
return;
}

for(int d = 0; d < 3; d++) {
int nr = r + dr[d];
int nc = c + dc[d];

if(!check(nr, nc) || map[nr][nc] == 'x' || flag) continue;
map[nr][nc] = 'x';
dfs(nr, nc);
}
}

static boolean check(int r, int c) {
return r >= 0 && r < R && c >= 0 && c < C;
}

static void initInput() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

R= Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());

map = new char[R][C];

for(int r = 0; r < R; r++) {
String str = br.readLine();
map[r] = str.toCharArray();
}
}

static void printMap(char[][] map) {
for(int r = 0; r < map.length; r++) {
System.out.println(Arrays.toString(map[r]));
}

System.out.println();
}
}
Loading

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