Skip to content
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
125 changes: 125 additions & 0 deletions baekjoon/11967_불켜기/src/Hyekyoung.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.StringTokenizer;

public class Hyekyoung {

static int N, M, cnt = 1;
static boolean[][] map, visited;
static List<Point>[] switchMap;
static List<Point> room = new ArrayList<>();
static int[] dr = { -1, 1, 0, 0 }, dc = { 0, 0, -1, 1 };
static Queue<Point> queue = new ArrayDeque<>();

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());
switchMap = new ArrayList[N * N + 1];
map = new boolean[N + 1][N + 1];
visited = new boolean[N + 1][N + 1];
int x, y, a, b;

for (int i = 1; i <= N * N; i++) switchMap[i] = new ArrayList<>();

for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
x = Integer.parseInt(st.nextToken());
y = Integer.parseInt(st.nextToken());
a = Integer.parseInt(st.nextToken());
b = Integer.parseInt(st.nextToken());

switchMap[(x - 1) * N + y].add(new Point(a, b));
}
lightOn();
System.out.println(cnt);
}

static void lightOn() {
map[1][1] = true;
Point curPoint;
Point p;
int nr, nc, r, c, pr, pc;
queue.offer(new Point(1, 1));
visited[1][1] = true;

while (!queue.isEmpty()) {
curPoint = queue.poll();
r = curPoint.r;
c = curPoint.c;

for (Point point : switchMap[(r - 1) * N + c]) {
pr = point.r;
pc = point.c;
if (!visited[pr][pc]) {
map[pr][pc] = true;
cnt++;
room.add(new Point(pr, pc));
}
// 현재 위치에서 불을 켤 수 있는곳은 켜고 리스트에 추가
}

for (int i=0; i<room.size(); i++) {
p = room.get(i);
pr = p.r;
pc = p.c;
if (isConnected(r, c, pr, pc) && !visited[pr][pc]) {
visited[pr][pc] = true;
queue.offer(new Point(pr, pc));
room.remove(i);
i--;
}
// 리스트에 있는 목록 중 갈 수 있는 곳은 큐에 넣고 리스트에서 빼기
}

}
}

static boolean isConnected(int sr, int sc, int er, int ec) {
// 현재 위치에서 갈 수 있는 곳인지 판단
queue.offer(new Point(sr, sc));
int r, c, nr, nc;
Point curPoint;
boolean[][] check = new boolean[N + 1][N + 1];
check[sr][sc] = true;

while (!queue.isEmpty()) {
curPoint = queue.poll();
r = curPoint.r;
c = curPoint.c;

if (r == er && c == ec) {
return true;
}

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

if (nr < 1 || nc < 1 || nr > N || nc > N) continue;
if (!map[nr][nc] || check[nr][nc]) continue;

check[nr][nc] = true;
queue.offer(new Point(nr, nc));
}
}
return false;
}

static class Point {
int r, c;

public Point(int r, int c) {
super();
this.r = r;
this.c = c;
}
}
}
45 changes: 45 additions & 0 deletions baekjoon/14908_구두수선공/src/Hyekyoung.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Hyekyoung {

public static void main(String[] args) throws IOException {

StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
Work[] works = new Work[N];
double a, b;
StringTokenizer st;
for(int i=0; i<N; i++) {
st = new StringTokenizer(br.readLine());
a = Double.parseDouble(st.nextToken());
b = Double.parseDouble(st.nextToken());
works[i] = new Work(i+1, a/b);
}
Arrays.sort(works);
for (Work work : works) {
sb.append(work.index+"\n");
}
System.out.println(sb.toString());
}

static class Work implements Comparable<Work>{
int index;
double per;

public Work(int index, double compensation) {
super();
this.index = index;
this.per = compensation;
}

@Override
public int compareTo(Work o) {
return Double.compare(this.per, o.per);
}
}
}
81 changes: 81 additions & 0 deletions baekjoon/14927_전구끄기/src/Hyekyoung.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Hyekyoung {

static int N, minCnt = 325, push;
static int light[], selected, copy[], l, pos;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
light = new int[N];
int input;
StringTokenizer st;
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
input = Integer.parseInt(st.nextToken());
if (input == 1) light[i] |= (1 << j);
// 불 켜진 곳 표시
}
}
selectTop(0, 0);
System.out.println(minCnt < 325 ? minCnt : -1);
}

// 가장 윗줄 모든 경우 선택
static void selectTop(int cnt, int selected) {
if (cnt == N) {
lightOff(selected);
return;
}
selectTop(cnt + 1, selected);
selected |= 1 << cnt;
selectTop(cnt + 1, selected);
}

static void lightOff(int selected) {
copy = light.clone();
push = 0;
boolean flag = true;
l = selected & (-selected);
// 현재 열 바로 윗줄에서 가장 오른쪽에 위치한 1 비트 위치 찾기
// 내 윗 전구가 1이면 꺼야함(윗 줄을 끌 수 있는 것은 아랫줄 뿐이니까)
while (l > 0) {
pos = log2(l);
changeLight(0, pos);
selected &= ~(1 << pos);
l = selected & (-selected);
}
for (int i = 1; i < N; i++) {
l = copy[i - 1] & (-copy[i - 1]);
while (l > 0) {
changeLight(i, log2(l));
l = copy[i - 1] & (-copy[i - 1]);
}
}
for (int i = 0; i < N; i++) {
if (copy[i] > 0) {
flag = false;
break;
}
}
if (flag) minCnt = Math.min(minCnt, push);
}

static void changeLight(int r, int c) {
push++;
copy[r] ^= 1 << c;
if (r > 0) copy[r - 1] ^= 1 << c;
if (r < N - 1) copy[r + 1] ^= 1 << c;
if (c > 0) copy[r] ^= 1 << c - 1;
if (c < N - 1) copy[r] ^= 1 << c + 1;
}

static int log2(int x) {
return (int) (Math.log(x) / Math.log(2));
}
}
85 changes: 85 additions & 0 deletions programmers/섬연결하기/src/Hyekyoung.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import java.util.Arrays;

//간많프 간선이 적으면 크루스칼!
public class Hyekyoung {
public static void main(String[] args) {

int n = 4;
int[][] costs = new int[][] { { 0, 1, 1 }, { 0, 2, 2 }, { 1, 2, 5 }, { 1, 3, 1 }, { 2, 3, 8 } };
Solution s = new Solution();
if (s.solution(n, costs) == 4) {
System.out.println("정답");
} else {
System.out.println("땡");
}

}
}

class Edge implements Comparable<Edge> {
int from, to, weight;

public Edge(int from, int to, int weight) {
super();
this.from = from;
this.to = to;
this.weight = weight;
}

@Override
public int compareTo(Edge o) {
return Integer.compare(this.weight, o.weight);
}
}

class Solution {
int V, E;
Edge[] edgeList;
int[] parents;

void makeSet() {
parents = new int[V];
for (int i = 0; i < V; i++) {
parents[i] = i;
}
}

int findSet(int a) {
if (a == parents[a]) return a;
return parents[a] = findSet(parents[a]);
}

boolean union(int a, int b) {
int aRoot = findSet(a);
int bRoot = findSet(b);

if (aRoot == bRoot) return false;

parents[bRoot] = aRoot;
return true;
}

public int solution(int n, int[][] costs) {
V = n;
E = costs.length;

edgeList = new Edge[E];

for (int i = 0; i < E; i++) {
edgeList[i] = new Edge(costs[i][0], costs[i][1], costs[i][2]);
}

Arrays.sort(edgeList);
makeSet();
int result = 0, count = 0;

for (Edge edge : edgeList) {
if (union(edge.from, edge.to)) {
result += edge.weight;
if (++count == V - 1) break;
}
}

return result;
}
}