-
Notifications
You must be signed in to change notification settings - Fork 1
[유병규-13주차 알고리즘 스터디] #64
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
Open
Gyulguma
wants to merge
6
commits into
main
Choose a base branch
from
ybg6539/week-13
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8604743
[Baekjoon-23290] 마법사 상어와 복제
Gyulguma d39bd2e
[Baekjoon-1507] 궁금한 민호
Gyulguma 27ed4e3
[Baekjoon-1561] 놀이 공원
Gyulguma 71daa97
[Baekjoon-16940] BFS 스페셜 저지
Gyulguma 2baf26b
[Baekjoon-19236] 청소년 상어
Gyulguma 6016841
[Baekjoon-10266] 시계 사진들
Gyulguma 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,172 @@ | ||
| import java.io.*; | ||
| import java.util.*; | ||
|
|
||
| public class Main { | ||
| private static int[][] fd = {{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1}}; | ||
| private static int[][] sd = {{-1,0},{0,-1},{1,0},{0,1}}; | ||
| private static int max; | ||
| private static int[][] map; | ||
| private static Node[] path; | ||
|
|
||
|
|
||
| public static void main(String[] args) throws IOException { | ||
| BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
| int n = 4; | ||
|
|
||
| map = new int[n][n]; | ||
| int[][] smallMap = new int[n][n]; | ||
|
|
||
| StringTokenizer st = new StringTokenizer(br.readLine()); | ||
| int m = Integer.parseInt(st.nextToken()); | ||
| int s = Integer.parseInt(st.nextToken()); | ||
|
|
||
| Set<Fish> fishes = new HashSet<>(); | ||
| for(int i=0; i<m; i++) { | ||
| st = new StringTokenizer(br.readLine()); | ||
| int x = Integer.parseInt(st.nextToken())-1; | ||
| int y = Integer.parseInt(st.nextToken())-1; | ||
| int dir = Integer.parseInt(st.nextToken())-1; | ||
|
|
||
| fishes.add(new Fish(x, y, dir)); | ||
| map[x][y]++; | ||
| } | ||
|
|
||
| st = new StringTokenizer(br.readLine()); | ||
| int x = Integer.parseInt(st.nextToken())-1; | ||
| int y = Integer.parseInt(st.nextToken())-1; | ||
| int[] shark = {x, y}; | ||
|
|
||
| //풀이 시작 | ||
| int time = 0; | ||
| while(time < s) { | ||
| //물고기 냄새 시간 증가 | ||
| for(int i=0; i<4; i++) { | ||
| for(int j=0; j<4; j++) { | ||
| if(smallMap[i][j] > 0) smallMap[i][j]++; | ||
| } | ||
| } | ||
|
|
||
| //기존 물고기 저장 | ||
| Queue<Fish> q = new LinkedList<>(); | ||
|
|
||
| //물고기 이동 | ||
| for(Fish fish : fishes) { | ||
| q.add(new Fish(fish.x, fish.y, fish.dir)); | ||
|
|
||
| for(int i=0; i<fd.length; i++) { | ||
| int idx = (fish.dir-i) < 0 ? fish.dir-i+8 : fish.dir-i; | ||
| int nx = fish.x + fd[idx][0]; | ||
| int ny = fish.y + fd[idx][1]; | ||
|
|
||
| if(nx<0 || nx>=n || ny<0 || ny>=4 || smallMap[nx][ny] > 0) continue; | ||
| if(nx==shark[0] && ny==shark[1]) continue; | ||
|
|
||
| map[fish.x][fish.y]--; | ||
| map[nx][ny]++; | ||
| fish.x = nx; | ||
| fish.y = ny; | ||
| fish.dir = idx; | ||
| break; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| //상어 이동 | ||
| max = -1; | ||
| path = new Node[3]; | ||
| dfs(0, shark[0], shark[1], 0, new Node(shark[0], shark[1], null)); | ||
| List<Fish> remove = new ArrayList<>(); | ||
| for(int i=0; i<3; i++) { | ||
| Node node = path[i]; | ||
| if(map[node.x][node.y] == 0) continue; | ||
| map[node.x][node.y] = 0; | ||
| for(Fish fish : fishes) { | ||
| if(fish.x == node.x && fish.y == node.y) remove.add(fish); | ||
| } | ||
| smallMap[node.x][node.y] = 1; | ||
| } | ||
| fishes.removeAll(remove); | ||
| shark[0] = path[2].x; | ||
| shark[1] = path[2].y; | ||
|
|
||
| //물고기 냄새 제거 | ||
| for(int i=0; i<n; i++) { | ||
| for(int j=0; j<n; j++) { | ||
| if(smallMap[i][j] > 2) smallMap[i][j] = 0; | ||
| } | ||
| } | ||
|
|
||
| //물고기 복사 | ||
| while(!q.isEmpty()) { | ||
| Fish fish = q.poll(); | ||
| fishes.add(fish); | ||
| map[fish.x][fish.y]++; | ||
| } | ||
|
|
||
| time++; | ||
| } | ||
|
|
||
| int count = 0; | ||
| for(int i=0; i<n; i++) { | ||
| for(int j=0; j<n; j++) { | ||
| count += map[i][j]; | ||
| } | ||
| } | ||
|
|
||
| System.out.println(count); | ||
| } | ||
|
|
||
| private static void dfs(int dept, int x, int y, int count, Node node) { | ||
| if(dept == 3) { | ||
| if(count <= max) return; | ||
| max = count; | ||
| Node current = node; | ||
| for(int i=2; i>=0; i--) { | ||
| path[i] = current; | ||
| current = current.pre; | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| for(int i=0; i<sd.length; i++) { | ||
| int nx = x + sd[i][0]; | ||
| int ny = y + sd[i][1]; | ||
|
|
||
| if(nx<0 || nx>=4 || ny<0 || ny>=4) continue; | ||
| int nCount = count+map[nx][ny]; | ||
| Node temp = node; | ||
| while(temp.pre != null) { | ||
| if(temp.x == nx && temp.y == ny) { | ||
| nCount = count; | ||
| break; | ||
| } | ||
| temp = temp.pre; | ||
| } | ||
| dfs(dept+1, nx, ny, nCount, new Node(nx, ny, node)); | ||
| } | ||
| } | ||
|
|
||
| private static class Fish{ | ||
| int x; | ||
| int y; | ||
| int dir; | ||
|
|
||
| public Fish(int x, int y, int dir) { | ||
| this.x = x; | ||
| this.y = y; | ||
| this.dir = dir; | ||
| } | ||
| } | ||
|
|
||
| private static class Node{ | ||
| int x; | ||
| int y; | ||
| Node pre; | ||
|
|
||
| public Node(int x, int y, Node pre) { | ||
| this.x = x; | ||
| this.y = y; | ||
| this.pre = pre; | ||
| } | ||
| } | ||
| } | ||
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로 구현한 게 좋아보이네요,, 저는 삼중 반복문으로 짰는데 DFS 코드가 더 깔끔해보여요
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만 떠올라서 위와 같이 구현하였는데, 여러 해결 방법을 아는 것도 중요하다 생각해서 삼중 반복문도 참고해보겠습니다!