-
Notifications
You must be signed in to change notification settings - Fork 0
Min: 12월 4주차 문제 풀이 #145
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
Merged
Min: 12월 4주차 문제 풀이 #145
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,63 @@ | ||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
| import java.util.StringTokenizer; | ||
|
|
||
| class Min { | ||
| static int[] dx = {1, -1, 0, 0}; | ||
| static int[] dy = {0, 0, 1, -1}; | ||
| static boolean[][] visited; | ||
| static int[][] arr; | ||
| static int answer; | ||
| public static void main(String[] args) throws IOException { | ||
| BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
| arr = new int[5][5]; | ||
| StringTokenizer str; | ||
| for(int i = 0; i < 5; i++) { | ||
| str = new StringTokenizer(br.readLine()); | ||
| for(int j = 0; j < 5; j++) { | ||
| arr[i][j] = Integer.parseInt(str.nextToken()); | ||
| } | ||
| } | ||
| str = new StringTokenizer(br.readLine()); | ||
| int a = Integer.parseInt(str.nextToken()); | ||
| int b = Integer.parseInt(str.nextToken()); | ||
| visited = new boolean[5][5]; | ||
| int apple = arr[a][b] == 1 ? 1 : 0; | ||
| answer = 0; | ||
| visited[a][b] = true; | ||
| dfs(a, b, 0, apple); | ||
| System.out.println(answer); | ||
| } | ||
|
|
||
| private static void dfs(int col, int row, int move, int apple) { | ||
| if(apple >= 2) { | ||
| answer = 1; | ||
| return; | ||
| } | ||
| if(move == 3) return; | ||
|
|
||
|
|
||
| for(int i = 0; i < 4; i++) { | ||
| int nx = col + dx[i]; | ||
| int ny = row + dy[i]; | ||
|
|
||
| if(isValid(nx, ny) && !visited[nx][ny]) { | ||
| visited[col][row] = true; | ||
| if(arr[nx][ny] == 1) { | ||
| dfs(nx, ny, move + 1, apple + 1); | ||
| }else if(arr[nx][ny] == 0){ | ||
| dfs(nx, ny, move + 1, apple); | ||
| } | ||
| visited[nx][ny] = false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static boolean isValid(int x, int y) { | ||
| if(x >= 0 && x < 5 && y >= 0 && y < 5 && arr[x][y] != -1) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
This file contains hidden or 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,41 @@ | ||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
| import java.util.ArrayList; | ||
| import java.util.StringTokenizer; | ||
|
|
||
| public class Min { | ||
| public static void main(String[] args) throws IOException { | ||
| BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
| ArrayList<ArrayList<Integer>> list = new ArrayList<>(); | ||
| for(int i = 0; i <= 12; i++) { | ||
| list.add(new ArrayList<>()); | ||
| } | ||
|
|
||
| for(int i = 0; i < 12; i++) { | ||
| StringTokenizer str = new StringTokenizer(br.readLine()); | ||
| int a = Integer.parseInt(str.nextToken()); | ||
| int b = Integer.parseInt(str.nextToken()); | ||
| list.get(a).add(b); | ||
| list.get(b).add(a); | ||
| } | ||
|
|
||
| for(int i = 1; i <= 12; i++) { | ||
| int total = 0; | ||
| if(list.get(i).size() == 3) { | ||
| int a = list.get(i).get(0); | ||
| total += list.get(a).size(); | ||
| int b = list.get(i).get(1); | ||
| total += list.get(b).size(); | ||
| int c = list.get(i).get(2); | ||
| total += list.get(c).size(); | ||
|
|
||
| if(total == 6) { | ||
|
Comment on lines
+31
to
+33
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 규칙을 잘 알아내신 것 같아요!! 👍 |
||
| System.out.println(i); | ||
| return; | ||
| } | ||
|
|
||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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,67 @@ | ||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
| import java.util.PriorityQueue; | ||
| import java.util.StringTokenizer; | ||
|
|
||
| public class Min { | ||
| public static void main(String[] args) throws IOException { | ||
| BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
| StringTokenizer str = new StringTokenizer(br.readLine()); | ||
| int N = Integer.parseInt(str.nextToken()); | ||
| int M = Integer.parseInt(str.nextToken()); | ||
| String[][] arr = new String[N][M]; | ||
| str = new StringTokenizer(br.readLine()); | ||
| int x1 = Integer.parseInt(str.nextToken()) - 1; | ||
| int y1 = Integer.parseInt(str.nextToken()) - 1; | ||
| int x2 = Integer.parseInt(str.nextToken()) - 1; | ||
| int y2 = Integer.parseInt(str.nextToken()) - 1; | ||
|
|
||
| for(int i = 0; i < N; i++) { | ||
| arr[i] = br.readLine().split(""); | ||
| } | ||
|
|
||
| boolean[][] visited = new boolean[N][M]; | ||
| PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2) -> o1[2] - o2[2]); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 우선순위 큐를 사용해도 됐었네요! 배워갑니다! 👍 |
||
| pq.offer(new int[]{x1, y1, 0}); | ||
| int[] dx = {1, -1, 0, 0}; | ||
| int[] dy = {0, 0, 1, -1}; | ||
|
|
||
| while(!pq.isEmpty()) { | ||
| int[] tmp = pq.poll(); | ||
| int x = tmp[0]; | ||
| int y = tmp[1]; | ||
| int cnt = tmp[2]; | ||
|
|
||
| if(visited[x][y]) continue; | ||
| visited[x][y] = true; | ||
|
|
||
| if(x == x2 && y == y2) { | ||
| System.out.println(cnt); | ||
| return; | ||
| } | ||
|
|
||
| for(int i = 0; i < 4; i++) { | ||
| int nx = x + dx[i]; | ||
| int ny = y + dy[i]; | ||
| if(!isValid(nx, ny, N, M) || visited[nx][ny]) continue; | ||
|
|
||
| if(nx == x2 && ny == y2) { | ||
| System.out.println(cnt + 1); | ||
| return; | ||
| } | ||
|
|
||
| if (arr[nx][ny].equals("1")) { | ||
| pq.offer(new int[]{nx, ny, cnt + 1}); | ||
| } else { | ||
| pq.offer(new int[]{nx, ny, cnt}); | ||
| } | ||
|
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| private static boolean isValid(int x, int y, int N, int M) { | ||
| return x >= 0 && x < N && y >= 0 && y < M; | ||
| } | ||
| } | ||
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.
return 조건문 <- 이런식으로 if문을 사용하지 않는 방법도 있을 것 같아요!