알고리즘
[java] 백준 16967 : 배열 복원하기
_DAMI
2023. 8. 6. 22:29
https://www.acmicpc.net/problem/16967
16967번: 배열 복원하기
크기가 H × W인 배열 A와 두 정수 X와 Y가 있을 때, 크기가 (H + X) × (W + Y)인 배열 B는 배열 A와 배열 A를 아래로 X칸, 오른쪽으로 Y칸 이동시킨 배열을 겹쳐 만들 수 있다. 수가 겹쳐지면 수가 합쳐
www.acmicpc.net
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.*;
public class Main {
static int h,w,x,y;
static boolean isRange(int x,int y) {
if(x>=0 && y>=0 && x<h && y<w) {
return true;
}
return false;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
h = Integer.parseInt(st.nextToken());
w = Integer.parseInt(st.nextToken());
x = Integer.parseInt(st.nextToken());
y = Integer.parseInt(st.nextToken());
int[][] a = new int[h][w];
int[][] b = new int[h+x][w+y];
for(int i=0; i<h+x; i++) {
st = new StringTokenizer(br.readLine());
for(int j=0; j<w+y; j++) {
b[i][j] = Integer.parseInt(st.nextToken());
}
}
for(int i=0; i<h+x; i++) {
for(int j=0; j<w+y; j++) {
int num = b[i][j];
if(num == 0) {
continue;
}
if(isRange(i,j) && isRange(i-x,j-y)) {
a[i][j] = num - a[i-x][j-y];
}
if(isRange(i,j) && !isRange(i-x,j-y)) {
a[i][j] = num;
}
if(!isRange(i,j) && isRange(i-x,j-y)) {
a[i-x][j-y] = num;
}
}
}
for(int i=0; i<h; i++) {
for(int j=0; j<w; j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}