-
Notifications
You must be signed in to change notification settings - Fork 4
[6주차] 고다혜 #76
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
[6주차] 고다혜 #76
Changes from 1 commit
0a2333a
e3d36e8
8817755
2206d94
830c013
7908b54
1f8497f
6f29099
9746f3a
44b5fd5
bd730ca
cafebef
5a6e60c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
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. 혹시! 이슈에 적어주신 블럭이 위치하는 모든 경우의 수가 왜 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. 우선 맵의 크기만큼 반복문을 실행하기 때문에 N ×ばつ M을 했고, 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. 한 지점에 대해 방향을 상, 하, 좌, 우 모두 고려해도 되지만, 상, 좌, 우로도 충분히 모든 경우의 수를 나타낼 수 있습니다!! 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. 아하!!!!! 그렇군요 설명 감사합니다!!👍 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. 아 네! 하, 좌, 우로 했네요!! 근데 상, 좌, 우로 해도 상관 없을 것 같습니다! 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. 으어 전 for문타고 내려가면서 봐서 하좌우라고 생각했는데 상일 경우도 왜 상관이 없는걸까요!?ᅲᅲᄐᄏᄏᄐᄐ큐ᅲᅲ 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. 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. 헐!!!!! 완전 친절하게 그림까지 짱입니다....💖 감사합니다!!! 아하 이제 완전 이해갔습니다! 위아래 중 한쪽만 막아줘도 되서 상관없는거였군요!!!! 최고입니다!!!!!!!👍 |
||
public class DH_테트리스_블럭_안의_합_최대화_하기 { | ||
static int N, M, result, max; | ||
static int[][] map; | ||
// 중복을 최대한 줄여주기 위해 상, 좌, 우 방향만 사용 | ||
static int[] dr = {0, 1, 0}, dc = {-1, 0, 1}; | ||
static boolean[][] v; | ||
|
||
static void solution() { | ||
// dfs 과정에서 원상복구 되기 때문에 | ||
// 새로 생성하지 않아도 됨 | ||
// dfs를 할 때마다 new boolean을 한다면 메모리 초과 발생 | ||
v = new boolean[N][M]; | ||
|
||
for(int r = 0; r < N; r++) { | ||
for(int c = 0; c < M; c++) { | ||
// r, c 지점에서 dfs 시작 | ||
v[r][c] = true; | ||
dfs(r, c, map[r][c], 0); | ||
v[r][c] = false; | ||
|
||
} | ||
} | ||
|
||
System.out.println(result); | ||
} | ||
|
||
static void dfs(int r, int c, int sum, int depth) { | ||
// 현재까지의 합 + (전체 - 남은 depth 값) * map에서 최대값 <= result라면 | ||
// 현재 r, c 지점에서 남은 depth값 보다 더 dfs를 하더라도 최대값이 될 수 없으므로 | ||
// return | ||
if(sum + (3 - depth) * max <= result) return; | ||
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. 엇! 저는 이렇게 조건을 추가하니까 시간이 더 걸리던데! 혹시 더 빨라지셨나요!? 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. image 수빈님 코드 중 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. 으악 그러네요!! 보드에서 맥스값을 넣어줘야하는데 그냥 맥스값을 넣었군요 제가... 코드를 좀 더 꼼꼼하게 짜는 연습을 더해야할거같네요!ᅲᅲ 감사합니다!!! |
||
// depth가 3이라면 return | ||
if(depth == 3) { | ||
result = Math.max(result, sum); | ||
return; | ||
} | ||
|
||
for(int d = 0; d < 3; d++) { | ||
int nr = r + dr[d]; | ||
int nc = c + dc[d]; | ||
|
||
if(!check(nr, nc) || v[nr][nc]) continue; | ||
|
||
// T자형 블럭을 만들어주기 위한 부분 | ||
// nr, nc 지점은 갔다고 방문 체크해주고 | ||
// 다음 위치 값은 nr, nc가 아닌 r, c로 설정해주기 | ||
if(depth == 1) { | ||
v[nr][nc] = true; | ||
dfs(r, c, sum + map[nr][nc], depth + 1); | ||
v[nr][nc] = false; | ||
} | ||
|
||
// T자 외의 부분을 만들어 주기 위한 부분 | ||
v[nr][nc] = true; | ||
dfs(nr, nc, sum + map[nr][nc], depth + 1); | ||
v[nr][nc] = false; | ||
} | ||
|
||
} | ||
|
||
static boolean check(int r, int c) { | ||
return r >= 0 && r < N && c >= 0 && c < M; | ||
} | ||
public static void main(String[] args) throws Exception { | ||
initInput(); | ||
solution(); | ||
} | ||
|
||
static void initInput() throws Exception { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
|
||
N = Integer.parseInt(st.nextToken()); | ||
M = Integer.parseInt(st.nextToken()); | ||
|
||
map = new int[N][M]; | ||
for(int r = 0; r < N; r++) { | ||
st = new StringTokenizer(br.readLine()); | ||
for(int c = 0; c < M; c++) { | ||
map[r][c] = Integer.parseInt(st.nextToken()); | ||
max = Math.max(map[r][c], max); | ||
} | ||
} | ||
} | ||
} |