
-Code
import java.io.*;
import java.util.*;
public class Main {
static int height;
static int width;
public static void main(String[] args) throws IOException {
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
height = Integer.parseInt(st.nextToken());
width = Integer.parseInt(st.nextToken());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
// 변형된 배열 크기
int totalRow = height + x;
int totalCol = width + y;
// 변형된 배열 받기
int[][] arr = new int[totalRow][totalCol];
for (int r = 0; r < totalRow; r++) {
st = new StringTokenizer(br.readLine());
for (int c = 0; c < totalCol; c++) {
arr[r][c] = Integer.parseInt(st.nextToken());
}
}
// 배열 복구 및 출력
// 변형된 배열 값에 복구한 배열의 (r - x, c - y)위치의 값 빼주기
StringBuilder answer = new StringBuilder();
int[][] decodeArr = new int[height][width];
for (int r = 0; r < height; r++) {
for (int c = 0; c < width; c++) {
int decodeRow = r - x;
int decodeCol = c - y;
// 디코딩에 사용할 숫자의 위치가 범위를 넘지 않은경우
if (isOk(decodeRow, decodeCol)) {
// 해당 위치의 숫자를 빼줌
decodeArr[r][c] = arr[r][c] - decodeArr[decodeRow][decodeCol];
} else {
// 범위를 넘은 경우는 기존값 넣기
decodeArr[r][c] = arr[r][c];
}
answer.append(decodeArr[r][c]).append(" ");
}
answer.append("\n");
}
System.out.println(answer);
}
private static boolean isOk(int row, int col) {
return (0 <= row && row < height) && (0 <= col && col < width);
}
}
처음에 문제 이해를 못해서 좀 찾아보니 그냥 row - x, col - y의 값을 빼주는 것이었습니다.
하지만 한가지 추가적인 문제가 arr[r][c] - arr[r - x][c - y]가 아니라 디코딩된 배열에 저장된 값의 (r - x, c - y)위치의 값을 빼줘야 했습니다.
'Beakjoon&프로그래머스 > Java' 카테고리의 다른 글
| [백준/Java] 6693번 Complicated Route (0) | 2026.02.16 |
|---|---|
| [백준/Java] 15803번 PLAYERJINAH’S BOTTLEGROUNDS (0) | 2026.02.14 |
| [백준/Java] 4201번 Snakes and Ladders (0) | 2026.02.14 |
| [백준/Java] 2303번 숫자 게임 (0) | 2026.02.13 |
| [백준/Java] 2641번 다각형그리기 (0) | 2026.02.13 |