-
[java] 백준 16967 : 배열 복원하기알고리즘 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(); } } }
'알고리즘' 카테고리의 다른 글
[java] 프로그래머스 크레인 인형뽑기 (0) 2023.08.09 프로그래머스 할인행사 (0) 2023.08.08 [java] 프로그래머스 두 큐 합 같게 만들기 (0) 2023.08.06 [java] 프로그래머스 lv2 전력망을 둘로 나누기 (0) 2023.08.06 프로그래머스 게임 맵 최단거리 (0) 2023.08.05