시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
---|---|---|---|---|---|
2 초 | 512 MB | 51566 | 19344 | 12532 | 35.452% |
폴리오미노란 크기가 1×1인 정사각형을 여러 개 이어서 붙인 도형이며, 다음과 같은 조건을 만족해야 한다.
정사각형 4개를 이어 붙인 폴리오미노는 테트로미노라고 하며, 다음과 같은 5가지가 있다.
아름이는 크기가 N×M인 종이 위에 테트로미노 하나를 놓으려고 한다. 종이는 1×1 크기의 칸으로 나누어져 있으며, 각각의 칸에는 정수가 하나 쓰여 있다.
테트로미노 하나를 적절히 놓아서 테트로미노가 놓인 칸에 쓰여 있는 수들의 합을 최대로 하는 프로그램을 작성하시오.
테트로미노는 반드시 한 정사각형이 정확히 하나의 칸을 포함하도록 놓아야 하며, 회전이나 대칭을 시켜도 된다.
첫째 줄에 종이의 세로 크기 N과 가로 크기 M이 주어진다. (4 ≤ N, M ≤ 500)
둘째 줄부터 N개의 줄에 종이에 쓰여 있는 수가 주어진다. i번째 줄의 j번째 수는 위에서부터 i번째 칸, 왼쪽에서부터 j번째 칸에 쓰여 있는 수이다. 입력으로 주어지는 수는 1,000을 넘지 않는 자연수이다.
첫째 줄에 테트로미노가 놓인 칸에 쓰인 수들의 합의 최댓값을 출력한다.
5 5
1 2 3 4 5
5 4 3 2 1
2 3 4 5 6
6 5 4 3 2
1 2 1 2 1
19
4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
20
4 10
1 2 1 2 1 2 1 2 1 2
2 1 2 1 2 1 2 1 2 1
1 2 1 2 1 2 1 2 1 2
2 1 2 1 2 1 2 1 2 1
7
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class P_14500 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] b_info = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int[][] board = new int[b_info[0]][b_info[1]];
for (int i = 0; i < b_info[0]; i++) {
board[i] = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
}
int answer = first_polyomino(board);
answer = (answer > second_polyomino(board)) ? answer : second_polyomino(board);
answer = (answer > third_polyomino(board)) ? answer : third_polyomino(board);
answer = (answer > fourth_polyomino(board)) ? answer : fourth_polyomino(board);
answer = (answer > fifth_polyomino(board)) ? answer : fifth_polyomino(board);
System.out.println(answer);
}
public static int first_polyomino(int[][] board) {
int max = 0;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i].length - j >= 4) {
int res = board[i][j] + board[i][j + 1] + board[i][j + 2] + board[i][j + 3];
max = (max > res) ? max : res;
}
if (board.length - i >= 4) {
int res = board[i][j] + board[i + 1][j] + board[i + 2][j] + board[i + 3][j];
max = (max > res) ? max : res;
}
}
}
return max;
}
public static int second_polyomino(int[][] board) {
int max = 0;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i].length - j >= 2 && board.length - i >= 2) {
int res = board[i][j] + board[i][j + 1] + board[i + 1][j] + board[i + 1][j + 1];
max = (max > res) ? max : res;
}
}
}
return max;
}
public static int third_polyomino(int[][] board) {
int max = 0;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i].length - j >= 2 && board.length - i >= 3) {
int res = board[i][j] + board[i + 1][j] + board[i + 2][j] + board[i + 2][j + 1];
max = (max > res) ? max : res;
res = board[i][j] + board[i][j + 1] + board[i + 1][j + 1] + board[i + 2][j + 1];
max = (max > res) ? max : res;
res = board[i][j + 1] + board[i + 1][j + 1] + board[i + 2][j + 1] + board[i + 2][j];
max = (max > res) ? max : res;
res = board[i][j] + board[i][j + 1] + board[i + 1][j] + board[i + 2][j];
max = (max > res) ? max : res;
}
if (board[i].length - j >= 3 && board.length - i >= 2) {
int res = board[i][j] + board[i][j + 1] + board[i][j + 2] + board[i + 1][j];
max = (max > res) ? max : res;
res = board[i + 1][j] + board[i + 1][j + 1] + board[i + 1][j + 2] + board[i][j + 2];
max = (max > res) ? max : res;
res = board[i][j] + board[i + 1][j] + board[i + 1][j + 1] + board[i + 1][j + 2];
max = (max > res) ? max : res;
res = board[i][j] + board[i][j + 1] + board[i][j + 2] + board[i + 1][j + 2];
max = (max > res) ? max : res;
}
}
}
return max;
}
public static int fourth_polyomino(int[][] board) {
int max = 0;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i].length - j >= 2 && board.length - i >= 3) {
int res = board[i][j] + board[i + 1][j] + board[i + 1][j + 1] + board[i + 2][j + 1];
max = (max > res) ? max : res;
res = board[i][j + 1] + board[i + 1][j] + board[i + 1][j + 1] + board[i + 2][j];
max = (max > res) ? max : res;
}
if (board[i].length - j >= 3 && board.length - i >= 2) {
int res = board[i + 1][j] + board[i][j + 1] + board[i + 1][j + 1] + board[i][j + 2];
max = (max > res) ? max : res;
res = board[i][j] + board[i][j + 1] + board[i + 1][j + 1] + board[i + 1][j + 2];
max = (max > res) ? max : res;
}
}
}
return max;
}
public static int fifth_polyomino(int[][] board) {
int max = 0;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i].length - j >= 3 && board.length - i >= 2) {
int res = board[i][j] + board[i][j + 1] + board[i][j + 2] + board[i + 1][j + 1];
max = (max > res) ? max : res;
res = board[i + 1][j] + board[i + 1][j + 1] + board[i + 1][j + 2] + board[i][j + 1];
max = (max > res) ? max : res;
}
if (board[i].length - j >= 2 && board.length - i >= 3) {
int res = board[i + 1][j] + board[i][j + 1] + board[i + 1][j + 1] + board[i + 2][j + 1];
max = (max > res) ? max : res;
res = board[i][j] + board[i + 1][j] + board[i + 2][j] + board[i + 1][j + 1];
max = (max > res) ? max : res;
}
}
}
return max;
}
}
브루트포스를 이용해도 1000 * 1000 = 1000000이기 때문에 2초를 넘지 않아서 각각의 테트로미노를 모두 브루트포스를 이용해서 수의 합의 최댓값을 구해주었다.
회전이나 대칭을 시켰을 때 하늘색 테트로미노는 2가지, 노란색 테트로미노는 1가지, 주황색 테트로미노는 8가지, 초록색 테트로미노는 4가지, 분홍색 테트로미노는 4가지가 나온다.
이중반복문을 순회하면서 기준 인덱스는 [i][j]로 하였고, 각각의 테트로미노의 모양에 맞게 인덱스 범위를 설정하고 이 값이 board의 크기를 넘지 않을 때에만 계산을 하도록 처리를 해 주어 segmentation falut가 발생하는 것을 방지했다.