-
Notifications
You must be signed in to change notification settings - Fork 0
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
[백준] 14500번 테트로미노 #45
Comments
풀이 언어
코드import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
private static int n;
private static int m;
private static int[][] board;
private static boolean[][] visit;
private static int[] dx = {0,0,-1,1};
private static int[] dy = {-1,1,0,0};
private static int max = 0;
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];
visit = 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++) {
visit[i][j] = true;
dfs(i, j, 0, 0);
visit[i][j] = false;
}
}
System.out.println(max);
}
private static void dfs(int x, int y, int depth, int sum) {
if (depth == 4) {
max = Math.max(max, sum);
return;
}
if (depth == 0) {
int[] temp = new int[4];
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || ny < 0 || nx >= n || ny >= m) {
continue;
}
temp[i] = board[nx][ny];
}
check(temp, board[x][y]);
}
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || ny < 0 || nx >= n || ny >= m) {
continue;
}
if (!visit[nx][ny]) {
visit[nx][ny] = true;
dfs(nx, ny, depth + 1, sum + board[x][y]);
visit[nx][ny] = false;
}
}
}
private static void check(int[] arr, int start) {
for (int i = 0; i < 4; i++) {
int sum = start;
for (int j = 0; j < 4; j++) {
if(j != i) {
sum += arr[j];
}
}
max = Math.max(sum, max);
}
}
} 핵심 로직 혹은 자료구조
시간 복잡도
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TITLE
테트로미노
LINK
📷 Screenshots
댓글 양식
The text was updated successfully, but these errors were encountered: