알고리즘 도전기 - 13

김치전사·2022년 3월 19일
0

알고리즘 도전기

목록 보기
13/89

16967 배열 복원하기

문제

크기가 H × W인 배열 A와 두 정수 X와 Y가 있을 때, 크기가 (H + X) × (W + Y)인 배열 B는 배열 A와 배열 A를 아래로 X칸, 오른쪽으로 Y칸 이동시킨 배열을 겹쳐 만들 수 있다. 수가 겹쳐지면 수가 합쳐진다.
즉, 배열 B의 (i, j)에 들어있는 값은 아래 3개 중 하나이다.

배열 B와 정수 X, Y가 주어졌을 때, 배열 A를 구해보자.

입력

첫째 줄에 네 정수 H, W, X, Y가 주어진다. 둘째 줄부터 H + X개의 줄에 배열 B의 원소가 주어진다.
항상 배열 A가 존재하는 경우만 입력으로 주어진다.

출력

총 H개의 줄에 배열 A의 원소를 출력한다.

제한

예제 입력

2 4 1 1
1 2 3 4 0
5 7 9 11 4
0 5 6 7 8

예제 출력

1 2 3 4
5 6 7 8

코드

import java.util.*;

public class Main{
    public static int[][] BList;
    public static int[][] AList;
    public static String[][] AStringList;
    public static int i;
    public static int j;
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int H = scanner.nextInt();
        int W = scanner.nextInt();
        int X = scanner.nextInt();
        int Y = scanner.nextInt();

        BList = new int[H+X][W+Y];
        AList = new int[H][W];
        AStringList = new String[H][W];
        for(i=0;i<H+X;i++){
            for(j=0;j<W+Y;j++){
                BList[i][j]=scanner.nextInt();
            }
        }
        findAList(H,W,X,Y);

        for(i=0;i<H;i++){
            for(j=0;j<W;j++){
                AStringList[i][j]=Integer.toString(AList[i][j]);
            }
        }

        for(i=0;i<H;i++) {
            System.out.println(String.join(" ",AStringList[i]));
        }
    }

    public static void findAList(int H, int W, int X, int Y){

       for(i=0;i<X;i++){
           for(j=0;j<W;j++){
               AList[i][j]=BList[i][j];
           }
       }

       for(i=X;i<H;i++){
           for(j=0;j<Y;j++){
               AList[i][j]=BList[i][j];
           }

           for(j=W;j<W+Y;j++){
               AList[i-X][j-Y]=BList[i][j];
           }
       }

       for(i=H;i<H+X;i++){
           for(j=Y;j<W+Y;j++){
               AList[i-X][j-Y]=BList[i][j];
           }
       }

       for(i=H-1;i>=X;i--){
           for(j=W-1;j>=Y;j--){
               AList[i-X][j-Y]=BList[i][j]-AList[i][j];
           }
       }

    }
}
profile
개인공부 블로그입니다. 상업적 용도 X

0개의 댓글