Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7faf0e5

Browse files
authored
Merge pull request #125 from yeahdy/main
[9주차] 이예진
2 parents 7bcbd26 + 1f257c3 commit 7faf0e5

File tree

10 files changed

+661
-1
lines changed

10 files changed

+661
-1
lines changed

‎BOJ/1000-5000번/YJ_2098.java‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class YJ_2098 {
5+
static int N;
6+
static int[][] route;
7+
static int[][] dp;
8+
static final int INF = 16_000_001;
9+
10+
public static void main(String[] args) throws IOException {
11+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
N = Integer.parseInt(br.readLine());
13+
route = new int[N][N];
14+
dp = new int[1<<N][N]; //N개의 도시를 방문하는 모든 가능한 상태 (1<<N 은 2^N)
15+
16+
for(int i = 0; i < N; i++){
17+
StringTokenizer st = new StringTokenizer(br.readLine());
18+
for(int j = 0; j < N; j++){
19+
route[i][j] = Integer.parseInt(st.nextToken());
20+
}
21+
}
22+
System.out.println(travelling(1,0)); //비트마스킹 1 은 방문완료
23+
}
24+
25+
26+
static int travelling(int visited, int now){
27+
if(visited == (1<<N)-1){ //모든 도시를 다 방문했는지 체크. 만약 N=4 일 경우 1<<N)-1 은 01111 즉, visited 가 1111 이라면 모든 도시를 다 방문했다는 뜻
28+
if(route[now][0] > 0){
29+
return route[now][0];
30+
}
31+
return INF;
32+
}
33+
34+
if(dp[visited][now] > 0){ //값이 0 이상인 경우 메모리제이션 완료
35+
return dp[visited][now];
36+
}
37+
dp[visited][now] = INF; //해당 경로는 방문 전으로 최소값을 찾기위해 최대값으로 초기화
38+
39+
for(int next=0; next < N; next++){
40+
if(stop(visited, now, next)){
41+
continue;
42+
}
43+
dp[visited][now] = Math.min(
44+
dp[visited][now], travelling(visited |(1<<next),next) + route[now][next]
45+
);
46+
}
47+
48+
return dp[visited][now];
49+
}
50+
51+
// 도시 i에서 도시 j로 갈 수 없고, 다음에 이동할 도시를 이미 순회했을 경우
52+
private static boolean stop (int visited, int now, int next){
53+
return route[now][next] == 0 || (visited & (1<<next)) > 0;
54+
}
55+
}

‎BOJ/1000-5000번/YJ_2660.java‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import java.util.*;
2+
import java.io.*;
3+
4+
public class YJ_2660 {
5+
static class Node {
6+
int index;
7+
int distance;
8+
Node (int index, int distance) {
9+
this.index = index;
10+
this.distance = distance;
11+
}
12+
}
13+
14+
static List<List<Integer>> graph = new ArrayList<>();
15+
static int MAX = 51; //★무작정 MAX_VALUE 넣지않기
16+
static int n;
17+
18+
public static void main(String[] args) throws IOException{
19+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
20+
n = Integer.parseInt(br.readLine());
21+
22+
//다익스트라 초기화
23+
for(int i=0; i<n+1; i++){
24+
graph.add(new ArrayList<>());
25+
}
26+
27+
while(true){
28+
StringTokenizer st = new StringTokenizer(br.readLine());
29+
int to = Integer.parseInt(st.nextToken());
30+
int from = Integer.parseInt(st.nextToken());
31+
if(to == -1 && from == -1){
32+
break;
33+
}
34+
graph.get(to).add(from);
35+
graph.get(from).add(to);
36+
}
37+
38+
getCandidates();
39+
}
40+
41+
static void getCandidates(){
42+
int min = MAX;
43+
List<Integer> candidates = new ArrayList<>();
44+
45+
//한 사람에 대한 모든 간선의 연결수 구하기
46+
for(int i=1; i<n+1; i++){
47+
//회원 별 연결점수 중 최고점을 저장
48+
int connection = findConnection(i);
49+
50+
//회장 후보: 점수가 가장 낮은 사람
51+
if(connection < min){
52+
candidates = new ArrayList<>();
53+
candidates.add(i);
54+
min = connection;
55+
}else if(connection == min) {
56+
candidates.add(i);
57+
}
58+
}
59+
60+
System.out.printf("%d %d%n",min,candidates.size());
61+
candidates.forEach(candidate -> System.out.printf("%d ",candidate));
62+
}
63+
64+
static int findConnection (int index){
65+
PriorityQueue<Node> queue = new PriorityQueue<>((n1,n2) -> n2.distance - n1.distance);
66+
queue.offer(new Node(index,0));
67+
68+
int[] table = new int[n+1];
69+
Arrays.fill(table,MAX);
70+
table[index] = 0;
71+
72+
while(!queue.isEmpty()){
73+
Node person = queue.poll();
74+
75+
for(int friend : graph.get(person.index)){
76+
if(table[friend] > table[person.index]+1){ //★다음 간선까지의 거리 1
77+
table[friend] = table[person.index]+1;
78+
//★거리를 누적해서 다음 순회에서 친구의 친구 > 친구의 친구의 친구 > ... 연결을 만듬
79+
queue.offer(new Node(friend,table[friend])); //★Node(연결된 친구,거리)
80+
}
81+
}
82+
}
83+
84+
int max = 0;
85+
for(int i=1; i<n+1; i++){
86+
max = Math.max(max,table[i]);
87+
}
88+
return max;
89+
}
90+
}

‎BOJ/10001-15000번/YJ_13164.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class YJ_13164 {
5+
public static void main(String[] args) throws IOException {
6+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
String[] data = br.readLine().split("\\s");
8+
int N = Integer.parseInt(data[0]);
9+
int K = Integer.parseInt(data[1]);
10+
int[] children = new int[N];
11+
StringTokenizer st = new StringTokenizer(br.readLine());
12+
13+
for(int i=0; i<N; i++){
14+
children[i] = Integer.parseInt(st.nextToken());
15+
}
16+
17+
int[] costs = new int[N-1];
18+
for(int i=1; i < children.length; i++){
19+
costs[i-1] = children[i] - children[i-1];
20+
}
21+
Arrays.sort(costs);
22+
23+
//가장 대소차이가 심한 그룹의 경우 제외하기
24+
int total = 0;
25+
for(int i=0; i<N-K; i++){
26+
total += costs[i];
27+
}
28+
System.out.println(total);
29+
}
30+
}

‎BOJ/10001-15000번/YJ_14620.java‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class YJ_14620 {
5+
static int N;
6+
static int[][] garden;
7+
static boolean[][] visited;
8+
static int result = 3001;
9+
10+
public static void main(String[] args) throws IOException {
11+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
N = Integer.parseInt(br.readLine());
13+
garden = new int[N][N];
14+
visited = new boolean[N][N];
15+
16+
for(int i=0; i<N; i++) {
17+
StringTokenizer st = new StringTokenizer(br.readLine());
18+
for (int j = 0; j < N; j++) {
19+
garden[i][j] = Integer.parseInt(st.nextToken());
20+
}
21+
}
22+
23+
dfs(0,0);
24+
System.out.println(result);
25+
}
26+
27+
static final int SEED = 3;
28+
static int[] nx = {0,1,0,-1};
29+
static int[] ny = {1,0,-1,0};
30+
static void dfs(int depth, int price){
31+
if(depth == SEED){
32+
result = Math.min(result, price);
33+
return;
34+
}
35+
36+
for(int i=1; i<N-1; i++){
37+
for(int j=1; j<N-1; j++){
38+
if(stop(i,j)){
39+
break;
40+
}
41+
int flower = plant(i,j);
42+
dfs(depth+1,price+flower);
43+
remove(i,j);
44+
}
45+
}
46+
}
47+
48+
//꽃을 심을 수 있는 구간인지 확인
49+
static boolean stop(int i, int j){
50+
if(visited[i][j]){
51+
return true;
52+
}
53+
for(int d=0; d<4; d++) {
54+
int x = i + nx[d];
55+
int y = j + ny[d];
56+
//꽃을 심을 수 없는 장소
57+
if(x < 0 || y < 0 || x >= N || y >= N || visited[x][y]){
58+
return true;
59+
}
60+
}
61+
return false;
62+
}
63+
64+
//5평에 꽃 심기
65+
static int plant(int i , int j){
66+
int price = garden[i][j];
67+
68+
for(int d=0; d<4; d++) {
69+
int x = i + nx[d];
70+
int y = j + ny[d];
71+
price += garden[x][y];
72+
visited[x][y] = true;
73+
//System.out.printf("garden[%d][%d] = %d %n",x,y,garden[x][y]);
74+
}
75+
76+
return price;
77+
}
78+
79+
//꽃 하나 없애기
80+
static void remove(int i, int j){
81+
for(int d=0; d<4; d++) {
82+
int x = i + nx[d];
83+
int y = j + ny[d];
84+
visited[x][y] = false;
85+
}
86+
}
87+
88+
}

‎BOJ/20001-25000번/YJ_20007.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public static void main(String[] args) throws IOException {
5151
}
5252

5353
static void shareMochi (int index){
54-
int days = 0;
5554
PriorityQueue<Pos> road = new PriorityQueue<>();
5655

5756
table[index] = 0;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /