Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21.08.17 - [BOJ] 16926. 배열 돌리기 1 #159

Closed
suhyunsim opened this issue Aug 17, 2021 · 0 comments
Closed

21.08.17 - [BOJ] 16926. 배열 돌리기 1 #159

suhyunsim opened this issue Aug 17, 2021 · 0 comments
Assignees
Labels
시뮬레이션 실버 BOJ - 실버 실패 시도했지만 맞지 못한 문제

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented Aug 17, 2021

문제

핵심 아이디어

  1. 배열을 먼저 그룹 짓는 것이 필요 -> 1차원 배열
  2. 1차원 배열 R번 회전
  3. 그룹에 넣기

어려운 점, 실수

풀이

package main.java.com.poogle.BOJ.Q16927;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int r = sc.nextInt();
        int[][] a = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                a[i][j] = sc.nextInt();
            }
        }
        ArrayList<ArrayList<Integer>> groups = new ArrayList<>();
        int groupn = Math.min(n, m) / 2;
        for (int k = 0; k < groupn; k++) {
            ArrayList<Integer> group = new ArrayList<>();
            for (int j = k; j < m - k; j++) {
                group.add(a[k][j]);
            }
            for (int i = k + 1; i < n - k - 1; i++) {
                group.add(a[i][m - k - 1]);
            }
            for (int j = m - k - 1; j > k; j--) {
                group.add(a[n - k - 1][j]);
            }
            for (int i = n - k - 1; i > k; i--) {
                group.add(a[i][k]);
            }
            groups.add(group);
        }
        for (int k = 0; k < groupn; k++) {
            ArrayList<Integer> group = groups.get(k);
            int len = group.size();
            int index = r % len;
            for (int j = k; j < m - k; j++, index = (index + 1) % len) {
                a[k][j] = group.get(index);
            }
            for (int i = k + 1; i < n - k - 1; i++, index = (index + 1) % len) {
                a[i][m - k - 1] = group.get(index);
            }
            for (int j = m - k - 1; j > k; j--, index = (index + 1) % len) {
                a[n - k - 1][j] = group.get(index);
            }
            for (int i = n - k - 1; i > k; i--, index = (index + 1) % len) {
                a[i][k] = group.get(index);
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                System.out.print(a[i][j] + " ");
            }
            System.out.println();
        }
    }
}
@suhyunsim suhyunsim self-assigned this Aug 17, 2021
@suhyunsim suhyunsim changed the title 21.08.17 - [BOJ] 16926. 배열 돌리기1 21.08.17 - [BOJ] 16926. 배열 돌리기 1 Aug 22, 2021
@suhyunsim suhyunsim added 시뮬레이션 실버 BOJ - 실버 실패 시도했지만 맞지 못한 문제 labels Aug 22, 2021
@suhyunsim suhyunsim modified the milestones: 8월 2주 차, 8월 3주 차 Aug 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
시뮬레이션 실버 BOJ - 실버 실패 시도했지만 맞지 못한 문제
Projects
None yet
Development

No branches or pull requests

1 participant