Home 16967번(배열 복원하기)
Post
Cancel

16967번(배열 복원하기)

첫 시도

  • X와 Y가 1이상인 것에 집중 -> 즉, input[0][?], input[?][0]은 바뀌지 않은 원본 값이라는 뜻
  • 원본 값을 사용해서 값이 변한 배열의 값을 유추해 낼 수 있음
  • 그리디한 풀이로 해결

해결

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {

    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

       st = new StringTokenizer(br.readLine());
       int H = Integer.parseInt(st.nextToken());
       int W = Integer.parseInt(st.nextToken());
       int X = Integer.parseInt(st.nextToken());
       int Y = Integer.parseInt(st.nextToken());

       int[][] input = 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++){
               input[i][j] = Integer.parseInt(st.nextToken());
           }
       }
       int[][] result = new int[H+X][W+Y];
        for(int i = 0; i<H; i++){
            for(int j = 0; j<W; j++){
                input[i+X][j+Y]-= input[i][j];
            }
        }

        for(int i = 0; i<H; i++){
            for(int j = 0; j<W; j++){
                System.out.print(input[i][j]+" ");
            }
            System.out.println();
        }

    }


}

참고

  • 직접구현
This post is licensed under CC BY 4.0 by the author.