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
55 changes: 55 additions & 0 deletions src/programmers/algorithm/dfsbfs/Q132266.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package programmers.algorithm.dfsbfs;

import java.util.*;

class Q132266 {
static Queue<Integer> q;
static int[] distance;
static int des;
static List<Integer>[] graph;
static int ans;
public int[] solution(int n, int[][] roads, int[] sources, int destination) {
int[] answer = new int[sources.length];

graph = new ArrayList[n+1];
for(int i=1;i<n+1;i++){
graph[i] = new ArrayList<>();
}

for(int[] road: roads){
int from = road[0];
int to = road[1];

graph[from].add(to);
graph[to].add(from);
}

q = new LinkedList<>();
distance = new int[n+1];
Arrays.fill(distance,-1);

q.offer(destination);
distance[destination] = 0;

bfs();

for(int i=0;i<sources.length;i++){
answer[i] = distance[sources[i]];
}

return answer;
}

static void bfs(){
while(!q.isEmpty()){
int now = q.poll();

for(int next:graph[now]){
if(distance[next]==-1){
distance[next] = distance[now]+1;
q.offer(next);
}
}
}
}
}
72 changes: 72 additions & 0 deletions src/programmers/algorithm/dijkstra/Q72413.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package programmers.algorithm.dijkstra;

import java.util.*;

class Q72413 {
static PriorityQueue<int[]> pq;
static List<int[]>[] graph;
static int[] distance;
public int solution(int n, int s, int a, int b, int[][] fares) {
graph = new ArrayList[n+1];
for(int i=0;i<n+1;i++){
graph[i] = new ArrayList<>();
}
for(int[] fare:fares){
int from = fare[0];
int to = fare[1];
int cost = fare[2];

graph[from].add(new int[]{to,cost});
graph[to].add(new int[]{from,cost});
}

int answer = Integer.MAX_VALUE;

// S -> K + K -> A + K -> B
// S -> K + A -> K + B -> K

int[] disS = dijkstra(n,s); // S 시작
int[] disA = dijkstra(n,a); // A 시작
int[] disB = dijkstra(n,b); // B 시작

for(int k=1;k<n+1;k++){
answer = Math.min(answer,disS[k]+disA[k]+disB[k]);
}

return answer;
}

static int[] dijkstra(int n, int start){
int[] distance = new int[n + 1];
Arrays.fill(distance, Integer.MAX_VALUE);

PriorityQueue<int[]> pq =
new PriorityQueue<>((x, y) -> Integer.compare(x[1], y[1]));

distance[start] = 0;
pq.offer(new int[]{start, 0});

while(!pq.isEmpty()){
int[] cur = pq.poll();
int now = cur[0];
int cost = cur[1];

if(cost > distance[now]){
continue;
}

for(int[] nexts : graph[now]){
int next = nexts[0];
int nextCost = nexts[1];

if(distance[next] > distance[now] + nextCost){
distance[next] = distance[now] + nextCost;

pq.offer(new int[]{next,distance[next]});
}
}
}
return distance;
}

}
41 changes: 41 additions & 0 deletions src/programmers/algorithm/floydwarshall/Q72413.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package programmers.algorithm.floydwarshall;

import java.util.*;

class Q72413 {
public int solution(int n, int s, int a, int b, int[][] fares) {
int INF = 1000000000;
int[][] dp = new int[n+1][n+1];
for(int i=1;i<n+1;i++){
Arrays.fill(dp[i],INF);
dp[i][i] = 0;
}

for(int[] fare:fares){
int from = fare[0];
int to = fare[1];
int cost = fare[2];

dp[from][to] = cost;
dp[to][from] = cost;
}

for(int k=1;k<n+1;k++){
for(int i=1;i<n+1;i++){
for(int j=1;j<n+1;j++){
dp[i][j] = Math.min(dp[i][j],dp[i][k]+dp[k][j]);
}
}
}

int answer = INF;
for(int k=1;k<n+1;k++){
if (dp[s][k] == INF || dp[k][a] == INF || dp[k][b] == INF) {
continue;
}
answer = Math.min(answer,dp[s][k] + dp[k][a] + dp[k][b]);
}

return answer;
}
}
97 changes: 97 additions & 0 deletions src/programmers/algorithm/implementation/Q60061.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package programmers.algorithm.implementation;

import java.util.*;

class Q60061 {
static int N;
static boolean[][] pillar; // x,y에 기둥 있는지
static boolean[][] bar; // x,y에 보 있는지
public int[][] solution(int n, int[][] build_frames) {
N = n;
pillar = new boolean[n+1][n+1];
bar = new boolean[n+1][n+1];

// 적용했을 때 조건 만족하면 실행
for(int[] build_frame:build_frames){
int x = build_frame[0];
int y = build_frame[1];
int a = build_frame[2];
int b = build_frame[3];

// 기둥 삭제
if(b == 0 && a == 0){
pillar[x][y] = false;
if(!isValid()){
pillar[x][y] = true;
}
}
// 기둥 설치
else if(b == 1 && a == 0){
pillar[x][y] = true;
if(!isValid()){
pillar[x][y] = false;
}
}
// 보 삭제
else if(b == 0 && a == 1){
bar[x][y] = false;
if(!isValid()){
bar[x][y] = true;
}
}
// 보 설치
else if(b == 1 && a == 1){
bar[x][y] = true;
if(!isValid()){
bar[x][y] = false;
}
}

}

List<int[]> result = new ArrayList<>();
for(int i=0;i<n+1;i++){
for(int j=0;j<n+1;j++){
if(pillar[i][j]){
result.add(new int[]{i,j,0});
}
if(bar[i][j]){
result.add(new int[]{i,j,1});
}
}
}

int[][] answer = new int[result.size()][];
for(int i=0;i<result.size();i++){
answer[i] = result.get(i);
}
return answer;
}

// 조건 만족하는지
static boolean isValid(){
for(int x=0;x<N+1;x++){
for(int y=0;y<N+1;y++){
// 기둥
// 바닥 위 or 보의 한쪽 끝 위 or 다른 기둥 위
if(pillar[x][y]){
if(!(y==0 || bar[x][y] || (x>0 && bar[x-1][y])
|| (y>0 && pillar[x][y-1]))){
return false;
}
}

// 보
if(bar[x][y]){
// 한쪽 끝이 기둥 위 or 양쪽 끝이 다른 보와 연결
if (!((y > 0 && pillar[x][y - 1])
|| (x<N && y > 0 && pillar[x + 1][y - 1])
|| (x > 0 && x<N && bar[x - 1][y] && bar[x + 1][y]))) {
return false;
}
}
}
}
return true;
}
}