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

Junsu #84

Open
wants to merge 8 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
95 changes: 95 additions & 0 deletions junsu/week14/BOJ1238 파티/BOJ1238.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//BOJ1238 파티, 골드3
//다익스트라 알고리즘
//목적지로 간 후 다시 되돌아오는 거라서 다익스트라 목적지에서 시작점으로 한번 더 써주면 된다.
//visited 안쓴 버전
//실수한부분 - pq.add 위치 잘못씀;
//pq.add(new Node(next.to, dist[next.to]));
import java.io.*;
import java.util.*;

public class BOJ1238 {
static class Node implements Comparable<Node>{
int to;
int cost;

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

@Override
public int compareTo(Node o) {
return this.cost - o.cost;
}
}
static int N, M, X, MaxAns;
static int[] dist;
static boolean[] visited;
static int INF = 987654321;
static ArrayList<Node>[] nodeList;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());

N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
X = Integer.parseInt(st.nextToken());
nodeList = new ArrayList[N+1];
for(int i = 1; i <= N; i++) {
nodeList[i] = new ArrayList<>();
}

for(int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int from = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());
int cost = Integer.parseInt(st.nextToken());
nodeList[from].add(new Node(to, cost));
}

int temp;
for(int i = 1; i <= N; i++) {
dijstra(i);
temp = dist[X];
//파티후 귀가
dijstra(X);
temp += dist[i];

MaxAns = Math.max(MaxAns, temp);
// bw.write(MaxAns+"\n");
}

bw.write(MaxAns+"\n");
bw.flush();
bw.close();
br.close();
}

// 파티하러 X 마을로 출발
static void dijstra(int start) {
dist = new int[N+1];
Arrays.fill(dist, INF);

// //다익스트라 알고리즘
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.add(new Node(start, 0));
dist[start] = 0;

while(!pq.isEmpty()) {
Node curNode = pq.poll();
int cur = curNode.to;
int cost = curNode.cost;

for(Node next : nodeList[cur]) {
if(dist[next.to] > dist[cur] + next.cost) {
dist[next.to] = dist[cur] + next.cost;
pq.add(new Node(next.to, dist[next.to]));
}
// 실수한부분 - pq.add 위치 잘못씀;
// pq.add(new Node(next.to, dist[next.to]));
}
}

}
}
92 changes: 92 additions & 0 deletions junsu/week14/BOJ1261 알고스팟/BOJ1261.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//BOJ1261 알고스팟, 골드4
//다익스트라 알고리즘, 벽 부수고 이동하기랑 비슷
//최단경로가 아닌 벽 부순 갯수 기준이라서 우선순위 큐 써야함
import java.io.*;
import java.util.*;

public class BOJ1261 {
static class Node implements Comparable<Node>{
int x;
int y;
int cost;

public Node(int x, int y, int cost) {
this.x = x;
this.y = y;
this.cost = cost;
}
@Override
public int compareTo(Node o) {
return this.cost - o.cost;
}
}
static int N, M, ans;
static int[][] map;
static int INF = 987654321;
static int[] dx = {0, 1, 0, -1}; //우 하 좌 상
static int[] dy = {1, 0, -1, 0};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
M = Integer.parseInt(st.nextToken());
N = Integer.parseInt(st.nextToken());

map = new int[N+1][M+1];

for(int i = 1; i <= N; i++) {
// st = new StringTokenizer(br.readLine());
// String temp = st.nextToken();
String temp = br.readLine();
for(int j = 1; j <= M; j++) {
map[i][j] = temp.charAt(j-1) - '0';
}
}

// for(int i = 1; i <= N; i++) {
// for(int j = 1; j <= M; j++) {
// System.out.print(map[i][j] + " ");
// }
// System.out.println();
// }
ans = dijkstra();

bw.write(ans + "\n");
bw.flush();
bw.close();
br.close();
}
static int dijkstra() {
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.add(new Node(1, 1, 0)); // x, y, 벽을 부순 횟수
boolean[][] visit = new boolean[N+1][M+1];
visit[1][1] =true;

while (!pq.isEmpty()) {
Node cur = pq.poll();
int curX = cur.x;
int curY = cur.y;
int curCost = cur.cost;

if (curX == N && curY == M) {
return curCost;
}

for (int dic = 0; dic < 4; dic++) {
int nextX = curX + dx[dic];
int nextY = curY + dy[dic];
if (nextX <= 0 || nextY <= 0 || nextX > N || nextY > M) continue;

if(!visit[nextX][nextY] && map[nextX][nextY] == 0) {
visit[nextX][nextY] = true;
pq.add(new Node(nextX, nextY, curCost));
}
if(!visit[nextX][nextY] && map[nextX][nextY] == 1) {
visit[nextX][nextY] = true;
pq.add(new Node(nextX, nextY, curCost+1));
}
}
}
return 0;
}
}
76 changes: 76 additions & 0 deletions junsu/week14/BOJ14284 간선 이어가기2/BOJ14284.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//BOJ14284 간선 이어가기2, 골드5
//데이크스트라 알고리즘
import java.io.*;
import java.util.*;

public class BOJ14284 {
static class Node implements Comparable<Node>{
int to;
int cost;
public Node(int to, int cost) {
this.to = to;
this.cost = cost;
}
@Override
public int compareTo(Node o) {
return this.cost - o.cost;
}
}
static int N, M, ans;
static ArrayList<Node>[] nodeList;
static int[] dist;
static int INF = 987654321;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());

N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
dist = new int[N+1];
nodeList = new ArrayList[N+1];
for(int i = 1; i <= N; i++) {
dist[i] = INF;
nodeList[i] = new ArrayList<>();
}

for(int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
nodeList[a].add(new Node(b, c));
nodeList[b].add(new Node(a, c));
}

st = new StringTokenizer(br.readLine());
int s = Integer.parseInt(st.nextToken());
int t = Integer.parseInt(st.nextToken());

dijkstra(s, t);

bw.write(ans + "\n");
bw.flush();
bw.close();
br.close();
}
static void dijkstra(int start, int end) {
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.add(new Node(start, 0));
dist[start] = 0;

while(!pq.isEmpty()) {
Node curNode = pq.poll();
int cur = curNode.to;
int cost = curNode.cost;

for(Node next : nodeList[cur]) {
if(dist[next.to] > dist[cur] + next.cost) {
dist[next.to] = dist[cur] + next.cost;
pq.add(new Node(next.to, dist[next.to]));
}
}
}
ans = dist[end];
}
}
85 changes: 85 additions & 0 deletions junsu/week14/BOJ14719 빗물/BOJ14719.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//BOJ14719 빗물, 골드5
//구현, 시뮬레이션 문제
//구현 문제라서 크게 피드백이 없다.
// 처음에 빈 공간과 이미 방문한 곳을 동시에 처리해서 에러가 발생했다.
// 빈 공간과 이미 방문한 곳을 따로 처리해줘야함
import java.io.*;
import java.util.*;

public class BOJ14719 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());

int H = Integer.parseInt(st.nextToken());
int W = Integer.parseInt(st.nextToken());
int[][] map = new int[H+1][W+1];
boolean[][] visited = new boolean[H+1][W+1];

st = new StringTokenizer(br.readLine());
for(int i = 1; i <= W; i++) {
int num = Integer.parseInt(st.nextToken());
int cnt = 0;
for(int j = H; j >= 1; j--) {
if(cnt++ == num) break;
map[j][i] = 1;
}
}

// for(int i = 1; i <= H; i++) {
// for(int j = 1; j <= W; j++) {
// System.out.print(map[i][j] + " ");
// }
// System.out.println();
// }

int ans = 0;
int N, M, endN, endM;
Loop1 :
for(int i = 1; i <= H; i++) {
N = M = endN = endM = 0;
for(int j = 1; j <= W; j++) {
if(H == 1 && W == 1) {
break Loop1;
}
// 빈 공간
if(map[i][j] == 0) continue;

// 이미 방문한 곳은 N과 M 리셋해줘야함
if(visited[i][j]) {
N = 0;
M = 0;
continue;
}

//블록 처음 발견했을때
if(N == 0 && M == 0) {
N = i;
M = j;
}else { //끝 블록 발견했을때
endN = i;
endM = j;
// System.out.println("시작"+N+ " " + endN+ " "+M+" "+endM);

//실수한 부분 : x, y 범위 설정을 잘못 생각함
for(int x = i; x <= i; x++) {
for(int y = M + 1; y < endM; y++) {
visited[x][y] = true;
if(map[x][y] == 0) {
// System.out.println(x + " " + y);
ans++;
}
}
}
N = M = endN = endM = 0;
j--;
}
}
}
bw.write(ans+"\n");
bw.flush();
bw.close();
br.close();
}
}
Loading