Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 호유동
- 문자열 압축
- 서블릿
- 완도산회
- 1로 만들기
- 설탕 배달
- 쓰레드 풀
- Servlet
- 2020 KAKAO BLIND
- 고모네 콩탕
- mvc
- 스프링 MVC
- dp
- 프로그래머스
- 알고리즘
- 2638
- 스프링
- 동적 프로그래밍
- 2589
- Spring
- BFS
- HTTP API
- 맛집
- 다이나믹 프로그래밍
- 맛집 투어
- 2839
- 양꼬치
- 투어
- 백준
- 포두부 보쌈
Archives
- Today
- Total
프로그래밍 공방
[백준] 19236번 : 청소년 상어 본문
문제
19236번: 청소년 상어
첫째 줄부터 4개의 줄에 각 칸의 들어있는 물고기의 정보가 1번 행부터 순서대로 주어진다. 물고기의 정보는 두 정수 ai, bi로 이루어져 있고, ai는 물고기의 번호, bi는 방향을 의미한다. 방향 bi는
www.acmicpc.net
문제해결방법
이 문제는 DFS를 통해 상어의 모든 경로를 구해주었다.
DFS로 상어의 경로를 탐색하면서 4X4X2 크기의 공간에 현재 상어, 물고기, 빈 공간과 방향을 가지고 다녔고
17X2 크기의 공간에 물고기들의 현재 위치를 담아서 물고기들이 이동하는걸 빠르게 구현해주었다.
* 배열을 복사할 때 clone()을 하면 deep copy가 되는 줄 알았는데 아니여서 그냥 같은 크기의 배열을 만들고 값을 복사해주었다.
코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | package baekjoon; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main19236_청소년상어 { public static int max = 0; public static int[][] dir = {{-1, 0}, {-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}}; public static int[][] fish = new int[17][2]; public static int[][][] map = new int[4][4][2]; public static void dfs(int x, int y, int[][][] map, int[][] fish, int sum) { // 상어가 물고기를 먹음 sum += map[x][y][0]; int sharkDir = map[x][y][1]; fish[map[x][y][0]][0] = -1; fish[map[x][y][0]][1] = -1; map[x][y][0] = -1; map[x][y][1] = -1; // 물고기 이동 for(int i=1; i<=16; i++) { if(fish[i][0]!=-1) { int curX = fish[i][0]; int curY = fish[i][1]; int curDir = map[curX][curY][1]; for(int d=0; d<dir.length; d++) { int nextDir = (curDir+d)%dir.length; int nextX = curX + dir[nextDir][0]; int nextY = curY + dir[nextDir][1]; if(nextX>=0 && nextX<4 && nextY>=0 && nextY<4 && map[nextX][nextY][0]!=-1) { fish[i][0] = nextX; fish[i][1] = nextY; if(map[nextX][nextY][0]!=0) { fish[map[nextX][nextY][0]][0] = curX; fish[map[nextX][nextY][0]][1] = curY; } int temp = map[nextX][nextY][0]; map[nextX][nextY][0] = map[curX][curY][0]; map[curX][curY][0] = temp; temp = map[nextX][nextY][1]; map[nextX][nextY][1] = nextDir; map[curX][curY][1] = temp; break; } } } } // 상어가 다음 위치로 이동 int nX = x+dir[sharkDir][0], nY = y+dir[sharkDir][1]; while(nX>=0 && nX<4 && nY>=0 && nY<4) { if(map[nX][nY][0]>0) { int[][][] tempMap = new int[4][4][2]; int[][] tempFish = new int[17][2]; for(int i=0; i<map.length; i++) { for(int j=0; j<map[i].length; j++) { for(int k=0; k<map[i][j].length; k++) tempMap[i][j][k] = map[i][j][k]; } } for(int i=0; i<fish.length; i++) { for(int j=0; j<fish[i].length; j++) tempFish[i][j] = fish[i][j]; } tempMap[x][y][0] = 0; tempMap[x][y][1] = -1; dfs(nX, nY, tempMap, tempFish, sum); } nX += dir[sharkDir][0]; nY += dir[sharkDir][1]; } if(max<sum) max = sum; } public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); for(int i=1; i<fish.length; i++) { fish[i][0] = -1; fish[i][1] = -1; } for(int i=0; i<map.length; i++) { StringTokenizer st = new StringTokenizer(br.readLine()); for(int j=0; j<map[i].length; j++) { int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); map[i][j][0] = a; map[i][j][1] = b-1; fish[a][0] = i; fish[a][1] = j; } } dfs(0, 0, map, fish, 0); System.out.println(max); } } | cs |
코드에 대한 피드백이나 더 좋은 아이디어는 언제나 환영입니다.
'개발 > 문제해결' 카테고리의 다른 글
[프로그래머스] 카드 짝 맞추기 (0) | 2021.02.21 |
---|---|
[백준] 19235번 : 모노미노도미노 (0) | 2021.02.21 |
[프로그래머스] 자동완성 (0) | 2021.02.18 |
[백준] 3694번 : 로봇 프로젝트 (0) | 2021.02.16 |
[백준] 1062번 : 가르침 (0) | 2021.02.15 |