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
104 changes: 104 additions & 0 deletions WEEK14/BOJ_14466/cheolsoon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import java.io.*;
import java.util.*;

import org.omg.CORBA.INTERNAL;

public class Main {

/*
* 소가 길을 건나간 이유
* N x N
* 인접 목초지 자유롭게 이동한다.
* 일부는 길을 건너간다.
*
* */
static int N, K, R;
// 상하좌우
static int dx[] = {-1,1,0,0};
static int dy[] = {0,0,-1,1};
static int graph[][];
static boolean visited[][];
static List<int[]> bridge[][];
static List<int[]> cow;

public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = null;
st = new StringTokenizer(in.readLine());
N = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
R = Integer.parseInt(st.nextToken());
graph = new int[N][N];
bridge = new ArrayList[N][N];
for(int i=0;i<N;i++) {
for(int j=0;j<N;j++) {
bridge[i][j] = new ArrayList<>();
}
}
cow = new ArrayList<>();
int r1,c1,r2,c2;
for(int i=0;i<R;i++) {
st = new StringTokenizer(in.readLine());
r1 = Integer.parseInt(st.nextToken())-1;
c1 = Integer.parseInt(st.nextToken())-1;
r2 = Integer.parseInt(st.nextToken())-1;
c2 = Integer.parseInt(st.nextToken())-1;
bridge[r1][c1].add(new int[] {r2, c2});
bridge[r2][c2].add(new int[] {r1, c1});
}

int r,c;
for(int i=0;i<K;i++) {
st = new StringTokenizer(in.readLine());
r = Integer.parseInt(st.nextToken())-1;
c = Integer.parseInt(st.nextToken())-1;

cow.add(new int[] {r, c});
}

int cnt=0;
// 소가 한마리씩 길을 건넌다
for(int i=0;i<K;i++) {
visited = new boolean[N][N];
moveCow(cow.get(i));

for(int j=i;j<K;j++) {
int temp[] = cow.get(j);
// 방문하지 않은 곳에 소가 있으면 만날 수 없음.
if(!visited[temp[0]][temp[1]]) cnt++;
}
}

System.out.println(cnt);

}

private static void moveCow(int[] pos) {
int x = pos[0];
int y = pos[1];
visited[pos[0]][pos[1]] = true;

for(int dir=0;dir<4;dir++) {
int nx = x+dx[dir];
int ny = y+dy[dir];

if(nx<0 || nx>=N || ny<0 || ny>=N) continue;
if(visited[nx][ny]) continue;

// if(bridge[x][y].contains(new int[] {nx,ny})) continue; ;
boolean isBridge = false;
for(int k=0;k<bridge[x][y].size();k++) {
int temp [] = bridge[x][y].get(k);
if(temp[0] == nx && temp[1] == ny) {
isBridge = true;
break;
}
}

if(isBridge) continue;

moveCow(new int[] {nx, ny});
}

}
}
141 changes: 141 additions & 0 deletions WEEK14/BOJ_14466/cheolsoon.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
![header](https://capsule-render.vercel.app/api?type=waving&height=200&color=0:FF658D,100:FFCB32&text=BOJ%2014466&fontColor=FFFFFF&fontAlign=80&fontAlignY=35&fontSize=50)

# **🔒Problem**

> [BOJ 14466 소가 길을 건너간 이유 6](https://www.acmicpc.net/problem/14466)

<br>
<br>

# **💻Code**

```java
import java.io.*;
import java.util.*;

import org.omg.CORBA.INTERNAL;

public class Main {

/*
* 소가 길을 건나간 이유
* N x N
* 인접 목초지 자유롭게 이동한다.
* 일부는 길을 건너간다.
*
* */
static int N, K, R;
// 상하좌우
static int dx[] = {-1,1,0,0};
static int dy[] = {0,0,-1,1};
static int graph[][];
static boolean visited[][];
static List<int[]> bridge[][];
static List<int[]> cow;

public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = null;
st = new StringTokenizer(in.readLine());
N = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
R = Integer.parseInt(st.nextToken());
graph = new int[N][N];
bridge = new ArrayList[N][N];
for(int i=0;i<N;i++) {
for(int j=0;j<N;j++) {
bridge[i][j] = new ArrayList<>();
}
}
cow = new ArrayList<>();
int r1,c1,r2,c2;
for(int i=0;i<R;i++) {
st = new StringTokenizer(in.readLine());
r1 = Integer.parseInt(st.nextToken())-1;
c1 = Integer.parseInt(st.nextToken())-1;
r2 = Integer.parseInt(st.nextToken())-1;
c2 = Integer.parseInt(st.nextToken())-1;
bridge[r1][c1].add(new int[] {r2, c2});
bridge[r2][c2].add(new int[] {r1, c1});
}

int r,c;
for(int i=0;i<K;i++) {
st = new StringTokenizer(in.readLine());
r = Integer.parseInt(st.nextToken())-1;
c = Integer.parseInt(st.nextToken())-1;

cow.add(new int[] {r, c});
}

int cnt=0;
// 소가 한마리씩 길을 건넌다
for(int i=0;i<K;i++) {
visited = new boolean[N][N];
moveCow(cow.get(i));

for(int j=i;j<K;j++) {
int temp[] = cow.get(j);
// 방문하지 않은 곳에 소가 있으면 만날 수 없음.
if(!visited[temp[0]][temp[1]]) cnt++;
}
}

System.out.println(cnt);

}

private static void moveCow(int[] pos) {
int x = pos[0];
int y = pos[1];
visited[pos[0]][pos[1]] = true;

for(int dir=0;dir<4;dir++) {
int nx = x+dx[dir];
int ny = y+dy[dir];

if(nx<0 || nx>=N || ny<0 || ny>=N) continue;
if(visited[nx][ny]) continue;

// if(bridge[x][y].contains(new int[] {nx,ny})) continue; ;
boolean isBridge = false;
for(int k=0;k<bridge[x][y].size();k++) {
int temp [] = bridge[x][y].get(k);
if(temp[0] == nx && temp[1] == ny) {
isBridge = true;
break;
}
}

if(isBridge) continue;

moveCow(new int[] {nx, ny});
}

}
}

```

<br>
<br>

# **🔑Description**

>

<br>
<br>

# **📑Related Issues**

>

<br>
<br>

# **🕛Resource**

| Memory | Time |
| --------- | ------- |
| `28524`KB | `320`ms |
70 changes: 70 additions & 0 deletions WEEK14/BOJ_20366/cheolsoon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
/*
* 눈덩이 i의 지름 hi
* 하나의 눈사람 두개 눈덩이
* 아래 눈덩이 >= 위 눈덩이
* 눈덩이 N개 중에 서로 다른 4개 골라서
* 2개의 눈사람 만든다
* 눈사람의 키는 두 눈사람 지름 합과 같다
* 눈사람의 키 차이가 작을수록 사이가 좋다
* 만들 수 있는 눈사람의 키 차이의 최솟값을 구해라
*
* */
static int N, snow[];
static boolean used[];

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

N = Integer.parseInt(in.readLine());
snow = new int[N];
used = new boolean[N];

st = new StringTokenizer(in.readLine());
for(int i=0;i<N;i++) {
snow[i] = Integer.parseInt(st.nextToken());
}

Arrays.sort(snow);

int minDiff = Integer.MAX_VALUE;
for(int i=0;i<N-1;i++) {
used[i] = true;
int snowman1, snowman2;
for(int j=i+1;j<N;j++) {
used[j] = true;
snowman1 = snow[i] + snow[j];
// 두개 뽑고 나머지 두개 뽑기
int start = 0;
int end = N-1;
while(start<end) {
if(start == i || start == j) {
start++;
continue;
}
if(end == i || end == j) {
end--;
continue;
}

snowman2 = snow[start] + snow[end];
minDiff = Math.min(minDiff, Math.abs(snowman1 - snowman2));
if(snowman1 > snowman2) {
start++;
}else {
end--;
}
}
}
}

System.out.println(minDiff);
}

}
Loading