[알고리즘/백준] 14500 테트로미노 - 삼성 SW 역량테스트, 자바(Java)

[알고리즘/백준] 14500 테트로미노 - 삼성 SW 역량테스트, 자바(Java)

문제

https://www.acmicpc.net/problem/14500

풀이 코드

ㅜ 를 제외한 나머지 4개 모양은 DFS와 백트래킹을 통해 구한다.

ㅜ 모양은 DFS를 통해 구할 수 없기 때문에 추가적으로 구한다.(코드상 checkOShape 메서드)

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { static int n, m; static int[][] board; static boolean[][] check; static int answer = Integer.MIN_VALUE; static int[] dx = {-1, 0, 1, 0}; static int[] dy = {0, 1, 0, -1}; static int[][] oDx = {{0, 0, 0, 1}, {0, 1, 2, 1}, {0, 0, 0, -1}, {0, -1, 0, 1}}; static int[][] oDy = {{0, 1, 2, 1}, {0, 0, 0, 1}, {0, 1, 2, 1}, {0, 1, 1, 1}}; public static void dfs(int level, int x, int y, int sum) { if (level == 4) { answer = Math.max(answer, sum); } else { for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx >= 0 && nx < n && ny >= 0 && ny < m && !check[nx][ny]) { check[nx][ny] = true; dfs(level + 1, nx, ny, asum + board[nx][ny]); check[nx][ny] = false; } } } } /** * ㅗ 모양은 dfs로 구할수 없기 때문에 추가적으로 구한다. */ public static void checkOShape(int x, int y) { for (int i = 0; i < 4; i++) { int sum = 0; boolean flag = true; for (int j = 0; j < 4; j++) { int nx = x + oDx[i][j]; int ny = y + oDy[i][j]; if (nx < 0 || nx >= n || ny < 0 || ny >= m) { flag = false; break; } sum += board[nx][ny]; } if (flag) { answer = Math.max(answer, sum); } } } 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]; check = new boolean[n][m]; for (int i = 0; i < n; i++) { st = new StringTokenizer(br.readLine()); for (int j = 0; j < m; j++) { board[i][j] = Integer.parseInt(st.nextToken()); } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { check[i][j] = true; dfs(0, i, j, 0); check[i][j] = false; checkOShape(i, j); } } System.out.println(answer); } }

728x90

from http://developer-hm.tistory.com/199 by ccl(A) rewrite - 2021-10-18 15:27:58