-
Notifications
You must be signed in to change notification settings - Fork 4
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
[22주차] 배수빈 #303
Merged
[22주차] 배수빈 #303
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ed03470
배수빈: [BOJ] 병원 거리 최소화하기_250210
baexxbin d584ca9
배수빈: [BOJ] 16724 피리 부는 사나이_240212
baexxbin 7ca48fc
배수빈: [PG] 67254 보석 쇼핑_240212
baexxbin a075b3c
배수빈: [BOJ] 18430 무기공학_250214
baexxbin af6e74a
배수빈: [BOJ] 1707 이분 그래프
baexxbin 95e760d
배수빈: [SQL] 년, 월, 성별 별 상품 구매 회원 수 구하기_250216
baexxbin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.StringTokenizer; | ||
|
||
/** | ||
* 어느 구역에 있더라도 SAFE ZONE 들거가게 하는 SAFE ZONE의 최소 개수 구하기 | ||
* => 순환 그래프의 개수 구하기 | ||
*/ | ||
|
||
public class SB_16724 { | ||
static int N, M; | ||
static int[][] board; | ||
static int[] dx = {-1, 1, 0, 0}; // 행 이동 | ||
static int[] dy = {0, 0, -1, 1}; // 열 이동 | ||
static boolean[][] visited; | ||
static boolean[][] finished; | ||
static int cycle = 0; | ||
|
||
private static void dfs(int x, int y) { | ||
visited[x][y] = true; | ||
|
||
// 다음 좌표 | ||
int d = board[x][y]; | ||
int nx = x+dx[d]; | ||
int ny = y+dy[d]; | ||
|
||
if (visited[nx][ny] && !finished[nx][ny]) { // 사이클 찾은 경우 | ||
cycle++; | ||
} | ||
|
||
if (!visited[nx][ny]) dfs(nx, ny); // 방문안했을 경우 다음 좌표로 이동 | ||
|
||
finished[x][y] = true; // 현재 노드의 dfs를 다 돌았으면 끝났음 표시 | ||
} | ||
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()); | ||
M = Integer.parseInt(st.nextToken()); | ||
|
||
board = new int[N][M]; | ||
visited = new boolean[N][M]; | ||
finished = new boolean[N][M]; | ||
for (int i = 0; i < N; i++) { | ||
String line = br.readLine(); | ||
for (int j = 0; j < M; j++) { | ||
char d = line.charAt(j); | ||
switch (d){ | ||
case 'U': | ||
board[i][j] = 0; | ||
break; | ||
case 'D': | ||
board[i][j] = 1; | ||
break; | ||
case 'L': | ||
board[i][j] = 2; | ||
break; | ||
case 'R': | ||
board[i][j] = 3; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
for (int i = 0; i < N; i++) { | ||
for (int j = 0; j < M; j++) { | ||
if (!finished[i][j]) dfs(i, j); | ||
} | ||
} | ||
|
||
System.out.println(cycle); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.StringTokenizer; | ||
|
||
public class SB_병원_거리_최소화하기 { | ||
static int N, M; | ||
static int[][] board; | ||
static List<Node> people = new ArrayList<>(); | ||
static List<Node> hospital = new ArrayList<>(); | ||
static List<List<Integer>> combi = new ArrayList<>(); | ||
static int ans = Integer.MAX_VALUE; | ||
|
||
private static void dfs(int start, List<Integer> tmp) { | ||
if (tmp.size() == M) { | ||
combi.add(new ArrayList<>(tmp)); | ||
return; | ||
} | ||
|
||
for (int i = start; i < hospital.size(); i++) { | ||
tmp.add(i); | ||
dfs(i + 1, tmp); | ||
tmp.remove(tmp.size() - 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()); | ||
M = Integer.parseInt(st.nextToken()); | ||
|
||
board = new int[N][N]; | ||
for (int i = 0; i < N; i++) { | ||
st = new StringTokenizer(br.readLine()); | ||
for (int j = 0; j < N; j++) { | ||
board[i][j] = Integer.parseInt(st.nextToken()); | ||
if (board[i][j]==1) people.add(new Node(i, j)); | ||
else if (board[i][j]==2) hospital.add(new Node(i, j)); | ||
} | ||
} | ||
|
||
// 병원 선택 조합 생성 | ||
dfs(0, new ArrayList<Integer>()); | ||
|
||
// 각 사람별 최소 병원 구간 구하기 | ||
for (List<Integer> lst: combi) { | ||
int tmp = 0; | ||
for (Node p : people) { | ||
int mn = Integer.MAX_VALUE; | ||
for (Integer i : lst) { // 각 사람이 병원을 돌며, 최소 병원길이 찾기 | ||
Node h = hospital.get(i); | ||
int dist = Math.abs(h.x - p.x) + Math.abs(h.y - p.y); | ||
mn = Math.min(mn, dist); | ||
} | ||
tmp += mn; // 최소 병원길이 더해주기 | ||
if (tmp >= ans) break; | ||
} | ||
ans = Math.min(ans, tmp); | ||
} | ||
|
||
System.out.println(ans); | ||
} | ||
static class Node{ | ||
int x, y; | ||
|
||
public Node(int x, int y) { | ||
this.x = x; | ||
this.y = y; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import java.util.*; | ||
|
||
class SB_67258 { | ||
public int[] solution(String[] gems) { | ||
Set<String> set = new HashSet<>(Arrays.asList(gems)); | ||
HashMap<String, Integer> map = new HashMap<>(); | ||
int N = set.size(); | ||
int mn = Integer.MAX_VALUE; | ||
int[] ans = new int[2]; | ||
|
||
int start = 0; | ||
for (int end = 0; end < gems.length; end++) { | ||
map.put(gems[end], map.getOrDefault(gems[end], 0) + 1); // 하나씩 보석 먹어나가기 | ||
while (map.size()==N) { // 먹은 보석의 개수가 전체 보석일때 동안 | ||
if (end-start < mn) { // 현재 길이가 짧다면 답 업데이트 | ||
mn = end-start; | ||
ans[0] = start; | ||
ans[1] = end; | ||
} | ||
map.put(gems[start], map.getOrDefault(gems[start], 0) - 1); | ||
if (map.get(gems[start])==0) map.remove(gems[start]); // 보석뺐을때 0 되면 그 보석 못먹은거니까 빼기 | ||
start++; | ||
} | ||
} | ||
ans[0]++; | ||
ans[1]++; | ||
return ans; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DFS로 깔끔하게 구현해주신 것 같은데요,,!!
행과 열이 1,000이면서
이런 테스트 케이스에서는 스택 오버플로우가 발생할 것 같습니다!
질문 게시판에 반례 보고 댓글 남깁니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
헉!!! 그렇군요!!
순환그래프는 dfs방법으로만 생각하고 시간복잡도도 괜찮아서 된다고 생각했었는데, 재귀일땐 스택 오버플로우도 잘 고려하는 습관을 길러야겠어요! N<=10^4 정도만 안전하다는 점... 잊지않도록 하겠습니당!! 알려주셔서 감사합니다!!😊