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

남희수 / 11월 1주차 / 목 #320

Open
wants to merge 23 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
54 changes: 54 additions & 0 deletions HeesuNam/boj/boj10159.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import java.io.*;
import java.util.*;

// 단방향 그래프
// 간선잇기(인접행렬) from(small) -> to(big)
// 플로이드 워셜로 연결된 간선들 잇기
// 한 정점에서 알수없는 정점세기
// 자신을 제외한 정점의 개수 = 나보다 큰애([자신][정점]), 나보다 작은애([정점][자신])가 이어지지 않은 정점의 개수
public class Main {
private static final int INF = 4950; // N(N-1)/2

public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int M = Integer.parseInt(br.readLine());
int[][] graph = new int[N + 1][N + 1];

for (int i = 1; i <= N; i++) {
Arrays.fill(graph[i], INF);
}

for (int i = 0; i < M; i++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int bigger = Integer.parseInt(st.nextToken());
int smaller = Integer.parseInt(st.nextToken());
graph[smaller][bigger] = 1;
}

for (int k = 1; k <= N; k++) {
for (int i = 1; i <= N; i++) {
if (i == k || graph[i][k] == INF)
continue;
for (int j = 1; j <= N; j++) {
if (j == i || graph[k][j] == INF)
continue;
if (graph[i][j] > graph[i][k] + graph[k][j]) {
graph[i][j] = graph[i][k] + graph[k][j];
}
}
}
}

StringBuilder sb = new StringBuilder();
for (int thing = 1; thing <= N; thing++) {
int cnt = 0;
for (int j = 1; j <= N; j++) {
if (graph[thing][j] == INF && graph[j][thing] == INF)
cnt++;
}
sb.append(cnt-1).append("\n");
}
System.out.println(sb.toString());
} // end of main
} // end of class
60 changes: 60 additions & 0 deletions HeesuNam/boj/boj1197.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import java.io.*;
import java.util.*;

public class Main {
private static int V, E;
private static ArrayList<Node>[] graph;
private static PriorityQueue<Node> pq = new PriorityQueue<>((n1, n2) -> Integer.compare(n1.cost, n2.cost));

static class Node {
int num;
int cost;

public Node(int num, int cost) {
super();
this.num = num;
this.cost = cost;
}
}

public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
V = Integer.parseInt(st.nextToken());
E = Integer.parseInt(st.nextToken());
graph = new ArrayList[V + 1];
for (int i = 1; i <= V; i++) {
graph[i] = new ArrayList<>();
}
for (int i = 0; i < E; i++) {
st = new StringTokenizer(br.readLine(), " ");
int A = Integer.parseInt(st.nextToken());
int B = Integer.parseInt(st.nextToken());
int C = Integer.parseInt(st.nextToken());
graph[A].add(new Node(B, C));
graph[B].add(new Node(A, C));
}
prim();

} // end of main

private static void prim() {
boolean[] visited = new boolean[V + 1];
pq.add(new Node(1, 0));
int total = 0;
while (!pq.isEmpty() && V > 0) {
Node cur = pq.poll();
if (visited[cur.num])
continue;
visited[cur.num] = true;
V--;
total += cur.cost;
for (Node nxt : graph[cur.num]) {
if (!visited[nxt.num]) {
pq.offer(nxt);
}
}
}
System.out.println(total);
}
} // end of class
77 changes: 77 additions & 0 deletions HeesuNam/boj/boj12919.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import java.io.*;
import java.util.*;

// 만드는 문자열이 == len(T)멈춤
// S랑 비교
// 이미 만든 문자열인지 검사(HashSet)
// A추가
// B추가 and 뒤집기
public class Main {
private static StringBuilder S, T;
private static HashSet<StringBuilder> chk = new HashSet<>();
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
S = new StringBuilder(br.readLine());
T = new StringBuilder(br.readLine());
makeStr(T,T.length());
System.out.println(0);
} // end of main

private static void makeStr(StringBuilder sb, int len) {
if(len==S.length()) {
if(S.compareTo(sb)==0) {
System.out.println(1);
System.exit(0);
}
return;
}
if(chk.contains(sb)) return;
chk.add(sb);
if(sb.charAt(len-1)=='A')makeStr(new StringBuilder(sb).deleteCharAt(len-1),len-1);
if(sb.charAt(0)=='B')makeStr(new StringBuilder(sb).reverse().deleteCharAt(len-1),len-1);
}
} // end of class

/**
import java.io.*;

// T에서 S만들기
// 맨뒤가 A면 end줄이기
// 맨앞이 B면 reverse하기
public class Main {
private static char[] S, T;
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
S = br.readLine().toCharArray();
T = br.readLine().toCharArray();
makeStr(0,T.length-1,false);
System.out.println(0);
} // end of main

private static void makeStr(int start, int end, boolean isReverse) {
if(end-start+1==S.length) {
if(isEqual(start,end,isReverse)) {
System.out.println(1);
System.exit(0);
}
return;
}
if(T[isReverse?start:end]=='A')makeStr(start+(isReverse?1:0),end+(isReverse?0:-1),isReverse);
if(T[isReverse?end:start]=='B')makeStr(start+(isReverse?0:1),end+(isReverse?-1:0),!isReverse);
}

private static boolean isEqual(int start, int end, boolean isReverse) {
if(isReverse) {
for (int i = end, inx = 0; inx < S.length; i--, inx++) {
if(S[inx]!=T[i]) return false;
}
}else {
for (int i = start, inx = 0; inx < S.length; i++, inx++) {
if(S[inx]!=T[i]) return false;
}
}
return true;
}
} // end of class

**/
54 changes: 54 additions & 0 deletions HeesuNam/boj/boj14226.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import java.io.*;
import java.util.*;


// screen+=clipboard*2
// screen-=1

public class Main {
private static int S;
private static boolean[][] typed;
static class Emoji{
int clipboard;
int screen;
int time;
public Emoji(int clipboard, int screen, int time) {
super();
this.clipboard = clipboard;
this.screen = screen;
this.time = time;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
S = Integer.parseInt(br.readLine());
typingEmoji();
}
private static void typingEmoji() {
typed = new boolean[S+1][S+1]; // clipboard, screen
Queue<Emoji> queue = new ArrayDeque<>();
queue.offer(new Emoji(0,1,0));
while(!queue.isEmpty()) {
Emoji cur = queue.poll();
if(cur.screen==S) {
System.out.println(cur.time);
break;
}
if(typed[cur.clipboard][cur.screen])continue;
typed[cur.clipboard][cur.screen] = true;
if(cur.screen>0) {
// 화면에 있는 이모티콘을 모두 복사해서 클립보드에 저장
if(!typed[cur.screen][cur.screen])
queue.offer(new Emoji(cur.screen, cur.screen,cur.time+1));
//화면에 있는 이모티콘 중 하나를 삭제
if(!typed[cur.clipboard][cur.screen-1])
queue.offer(new Emoji(cur.clipboard, cur.screen-1,cur.time+1));
}
if(cur.clipboard>0 && cur.screen+cur.clipboard <= S&& !typed[cur.clipboard][cur.screen+cur.clipboard]) {
// 클립보드에 있는 모든 이모티콘을 화면에 붙여넣기
queue.offer(new Emoji(cur.clipboard, cur.screen+cur.clipboard, cur.time+1));
}
}

} // end of main
} // end of class
71 changes: 71 additions & 0 deletions HeesuNam/boj/boj14461.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import java.io.*;
import java.util.*;

public class Main {
static class Cow {
int y;
int x;
int move;
int time;

public Cow(int y, int x, int move, int time) {
super();
this.y = y;
this.x = x;
this.move = move;
this.time = time;
}
}

private static int N, T;
private static int[][] map;
private static final int[] dy = { -1, 0, 1, 0 };
private static final int[] dx = { 0, 1, 0, -1 };

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());
T = Integer.parseInt(st.nextToken());
map = new int[N][N];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine(), " ");
for (int j = 0; j < N; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
dijk();
} // end of main

private static void dijk() {
PriorityQueue<Cow> pq = new PriorityQueue<>((o1, o2) -> o1.time - o2.time);
boolean[][][] visited = new boolean[N][N][3];
pq.offer(new Cow(0, 0, 0, 0));
while (!pq.isEmpty()) {
Cow cur = pq.poll();
if (visited[cur.y][cur.x][cur.move])
continue;
visited[cur.y][cur.x][cur.move] = true;
if (cur.y == N - 1 && cur.x == N - 1) {
System.out.println(cur.time);
return;
}
int nxtMove = (cur.move + 1) % 3;
for (int i = 0; i < 4; i++) {
int ny = cur.y + dy[i];
int nx = cur.x + dx[i];
if (rangeChk(ny, nx) && !visited[ny][nx][nxtMove]) {
if (nxtMove == 0) {
pq.offer(new Cow(ny, nx, nxtMove, cur.time + T + map[ny][nx]));
} else {
pq.offer(new Cow(ny, nx, nxtMove, cur.time + T));
}
}
}
}
}

private static boolean rangeChk(int y, int x) {
return 0 <= y && y < N && 0 <= x && x < N;
}
} // end of class
Loading
Loading