Skip to content
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

[9주차] 고다혜 #118

Open
wants to merge 5 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
78 changes: 78 additions & 0 deletions BOJ/1000-5000번/DH_2660.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import java.io.*;
import java.util.*;

/*
* 회장뽑기
*/

public class DH_2660 {
static int N, maxDepth;
static ArrayList<Integer> adj[];
static ArrayDeque<int []> q;
static boolean[] v;
static int[] depth; // 각 회원마다 끝까지 탐색했을 때 depth를 저장할 변수
static TreeSet<Integer> set; // 회장이 될 수 있는 사람의 번호를 담음

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

StringBuilder sb = new StringBuilder();

sb.append(maxDepth).append(" ").append(set.size()).append("\n");
for(int i: set) sb.append(i).append(" ");
System.out.println(sb);
}

static void solution() {
for(int i = 1; i < adj.length; i++) {
v = new boolean[N + 1];
q.add(new int[] {i, 0}); // [0]: 학생 번호, [1]: depth 깊이
v[i] = true;

while(!q.isEmpty()) {
int[] current = q.poll();
depth[i] = current[1];

for(int next: adj[current[0]]) {
if(v[next]) continue;
q.add(new int[] {next, current[1] + 1});
v[next] = true;
}
}

if(depth[i] < maxDepth) { // maxDepth 깊이가 작아야 점수가 작아지기 때문에
maxDepth = depth[i];
set.clear();
}

if(depth[i] == maxDepth) set.add(i);
}
}

static void initInput() throws Exception {
System.setIn(new FileInputStream("../AlgorithmStudy/input/BOJ2660.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;

q = new ArrayDeque<>();
set = new TreeSet<Integer>();

maxDepth = Integer.MAX_VALUE;
N = Integer.parseInt(br.readLine());
adj = new ArrayList[N + 1];
for(int i = 0; i < adj.length; i++) adj[i] = new ArrayList<Integer>();

depth = new int[N + 1];

String s;
while(!(s = br.readLine()).equals("-1 -1")) {
st = new StringTokenizer(s);
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());

adj[a].add(b);
adj[b].add(a);
}
}
}
152 changes: 152 additions & 0 deletions CodeTree/2017-2018년/DH_전투로봇.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import java.io.*;
import java.util.*;

public class DH_전투로봇 {
static class Point {
int r, c;
public Point(int r, int c) {
this.r = r;
this.c = c;
}
}

static class qPoint implements Comparable<qPoint> {
Point p;
int d;
public qPoint(Point p, int d) {
this.p = p;
this.d = d;
}

@Override
public int compareTo(qPoint o) {
if(this.d == o.d) {
if(this.p.r == o.p.r) return Integer.compare(this.p.c, o.p.c); // 열 오름차순
return Integer.compare(this.p.r, o.p.r); // 행 오름차순s
}
return Integer.compare(this.d, o.d); // 거리 오름차순
}
}

static int[] dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1};
static int N, dieMonsterCnt, totalTime;
static int[][] levelMap, monsterIdxMap;
static Point robot;

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

static void solution() {

int time;
while((time = bfs()) != 0) {
totalTime += time;
}
System.out.println(totalTime);
}

static int bfs() {

PriorityQueue<qPoint> q = new PriorityQueue<>();
boolean[][] v = new boolean[N][N];

q.add(new qPoint(robot, 0));
v[robot.r][robot.c] = true;
int robotLevel = levelMap[robot.r][robot.c];
levelMap[robot.r][robot.c] = 0;

// 없앨 수 있는 몬스터 정보 구하기
while(!q.isEmpty()) {
qPoint current = q.poll();

Copy link
Contributor

Choose a reason for hiding this comment

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

애초에 큐를 우선순위큐로 하면 가장 먼저 찾은 몬스터를 없애면 되는군요!! 👍 효율적인 코드 배워갑니다!

// 몬스터를 없앨 수 있다면
if(canKillMonster(current.p, robotLevel)) {
// 몬스터 없앤 개수 늘리기
dieMonsterCnt++;
// 없앤 몬스터의 개수와 로봇의 레벨이 같다면
if(dieMonsterCnt == robotLevel) {
// 로봇의 레벨 늘려주기
dieMonsterCnt = 0;
robotLevel++;
}

// map에 있는 몬스터의 정보 없애주기
levelMap[current.p.r][current.p.c] = 0;
monsterIdxMap[current.p.r][current.p.c] = 0;

// 로봇 위치, map에 로봇 정보 갱신하기
robot.r = current.p.r;
robot.c = current.p.c;
levelMap[current.p.r][current.p.c] = robotLevel;

// 로봇이 이동한 위치 반환
return current.d;
}

for(int d = 0; d < 4; d++) {
int nr = current.p.r + dr[d];
int nc = current.p.c + dc[d];

// 범위를 벗어나거나 || 이미 방문했던 곳이거나 || 자신의 레벨보다 큰 몬스터가 있거나
if(!check(nr, nc) || v[nr][nc] || levelMap[nr][nc] > robotLevel) continue;
q.add(new qPoint(new Point(nr, nc), current.d + 1));
v[nr][nc] = true;
}
}

// 몬스터를 해치우지 못한 경우
return 0;
}

static boolean canKillMonster(Point current, int robotLevel) {
int monsterLevel = levelMap[current.r][current.c];
return 0 < monsterLevel && monsterLevel < robotLevel;
}

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

static void initInput() throws Exception {
System.setIn(new FileInputStream("../AlgorithmStudy/input/전투로봇.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;

N = Integer.parseInt(br.readLine());
levelMap = new int[N][N];
monsterIdxMap = new int[N][N];

int idx = 1;
for(int r = 0; r < N; r++) {
st = new StringTokenizer(br.readLine());
for(int c = 0; c < N; c++) {
levelMap[r][c] = Integer.parseInt(st.nextToken());
if(levelMap[r][c] == 9) {
robot = new Point(r, c);
levelMap[r][c] = 2; // 전투로봇의 초기 레벨: 2
}
else if(levelMap[r][c] != 0) {
monsterIdxMap[r][c] = idx;
}
}
}

// printRobotInfo();
// printlevelMapInfo();
// printMostersInfo();
}

static void printRobotInfo() {
System.out.println("robotInfo: " + robot.toString() + ", level: " + levelMap[robot.r][robot.c]);
}

static void printlevelMapInfo() {
for(int r = 0; r < N; r++) {
System.out.println(Arrays.toString(levelMap[r]));
}

System.out.println();
}
}
39 changes: 39 additions & 0 deletions Programmers/Level3/DH_64062.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

/*
* 징검다리 건너기
*/

public class DH_64062 {
static int solution(int[] stones, int k) {
// 돌을 건넌 사람의 최대 수 구하기 - upper bound 구하기
int s = 0, e = 200_000_001;

while(s <= e) {
// 돌을 건넌 친구 수
int m = (s + e) / 2;

int cnt = 0;

// 친구들이 모두 길을 건넜을 때
// 값이 0이하가 되는 곳이 k개 이상이 된다면 못건너감
boolean canGo = true;
for(int stone: stones) {
if(stone - m < 0) cnt++;
else cnt = 0;

if(cnt >= k) canGo = false;
}

if(canGo) s = m + 1;
else e = m - 1;
}

return e;
}

public static void main(String[] args) {
int[] stones = {2, 4, 5, 3, 2, 1, 4, 2, 5, 1};
int k = 3;
System.out.println(solution(stones, k));
}
}