Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 백준
- 스프링 에러
- 자바
- java 프로그래머스
- BFS
- 프로그래머스
- 코테
- 백준 18222
- java leetcode
- dfs
- 인텔리제이 에러
- 리트코드 자바
- 분할정복
- 자바 리트코드
- 카카오
- Java
- 리트코드
- leetcode 1721
- daily challenge
- 리트코드 1557
- 코딩테스트
- leetcode
- DP
- 스택
- 자바 5464
- 그래프 자바
- 파이썬
- 구현
- 백준 16935
- 프로그래머스 java
Archives
- Today
- Total
레벨업 일지
[Java] 백준 16935 배열 돌리기 3 본문
문제
https://www.acmicpc.net/problem/16935
16935번: 배열 돌리기 3
크기가 N×M인 배열이 있을 때, 배열에 연산을 R번 적용하려고 한다. 연산은 총 6가지가 있다. 1번 연산은 배열을 상하 반전시키는 연산이다. 1 6 2 9 8 4 → 4 2 9 3 1 8 7 2 6 9 8 2 → 9 2 3 6 1 5 1 8 3 4 2 9 →
www.acmicpc.net
풀이
조금 시간이 걸렸던 구현 문제
문제 요구사항은 다음과 같다.
주어진 배열의 순서를 바꾸는데 구현만 하면 되고, 최적화는 필요없다.
따라서 N번 연산의 요구사항에 맞추어 구현했다.
1번 연산의 구현 코드이다.
public void upAndDownFlip(int[][] m){
int r, c,t;
r = m.length;
c = m[0].length;
for(int j = 0 ;j < c;j ++){
for(int i = 0 ;i < r/2; i++){
t = m[i][j];
m[i][j] = m[r-i-1][j];
m[r-i-1][j] = t;
}
}
}
2번 연산의 구현 코드이다
public void leftAndRightFlip(int[][]m){
int r, c,t;
r = m.length;
c = m[0].length;
for(int i = 0 ;i < r; i++){
for(int j= 0 ;j < c/2;j ++){
t = m[i][j];
m[i][j] = m[i][c - j - 1];
m[i][c - j - 1] = t;
}
}
}
3번 연산의 구현 코드이다
public int[][] rotateRight(int[][]m){
int r, c,t;
r = m.length;
c = m[0].length;
int[][] ret = new int[c][r];
for(int i= 0 ;i < c; i++){
for(int j=0 ;j < r; j++){
ret[i][j] = m[r-j-1][i];
}
}
return ret;
}
4번 연산의 구현 코드이다
public int[][] rotateLeft(int[][] m){
int r, c,t;
r = m.length;
c = m[0].length;
int[][] ret = new int[c][r];
for(int i= 0 ;i < c; i++){
for(int j=0 ;j < r; j++){
ret[i][j] = m[j][c-i-1];
}
}
return ret;
}
5, 6번 연산은 새로운 2차원 배열에 4가지 섹터 카피한 후 , 주어진 위치에 삽입하였다.
5번, 6번 연산의 구현 코드이다
public void operate5(int[][]m){
int r, c,t, gOne, gTwo, gThree, gFour;
r = m.length/2;
c = m[0].length/2;
int [][]arrOne = new int[r][c];
int [][]arrTwo = new int[r][c];
int [][]arrThree = new int[r][c];
int [][]arrFour = new int[r][c];
InitsubArr(m, arrOne, 0, 0);
InitsubArr(m, arrTwo, 0, c);
InitsubArr(m, arrThree, r, 0);
InitsubArr(m, arrFour, r, c);
deepCopy2D(m, arrThree, 0, 0);
deepCopy2D(m, arrOne, 0, c);
deepCopy2D(m, arrFour, r, 0);
deepCopy2D(m, arrTwo, r, c);
}
public void operate6(int[][] m){
int r, c,t, gOne, gTwo, gThree, gFour;
r = m.length/2;
c = m[0].length/2;
int [][]arrOne = new int[r][c];
int [][]arrTwo = new int[r][c];
int [][]arrThree = new int[r][c];
int [][]arrFour = new int[r][c];
InitsubArr(m, arrOne, 0, 0);
InitsubArr(m, arrTwo, 0, c);
InitsubArr(m, arrThree, r, 0);
InitsubArr(m, arrFour, r, c);
deepCopy2D(m, arrTwo, 0, 0);
deepCopy2D(m, arrFour, 0, c);
deepCopy2D(m, arrOne, r, 0);
deepCopy2D(m, arrThree, r, c);
}
public void InitsubArr(int[][] t, int[][]o, int x, int y){
int r, c;
r = o.length;
c = o[0].length;
for(int i = x ; i< x + r ;i++){
for(int j = y ; j< y + c;j ++){
o[i-x][j-y] = t[i][j];
}
}
}
public void deepCopy2D(int[][] t, int[][]o, int x, int y){
//(x,y) 시작 위치
int r, c;
r = o.length;
c = o[0].length;
for(int i = x ; i< x + r ;i++){
for(int j = y ; j< y + c;j ++){
t[i][j] = o[i-x][j-y];
}
}
}
코드
전체 코드는 다음과 같다.
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
BufferedWriter bw;
int[][] arr;
public static void main(String args[]) throws IOException {
Main m = new Main();
m.solution();
}
public void solution() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
String str = br.readLine();
StringTokenizer st;
int N = Integer.parseInt(str.split(" ")[0]);
int M = Integer.parseInt(str.split(" ")[1]);
int R = Integer.parseInt(str.split(" ")[2]);
arr = new int[N][M];
for(int i = 0 ;i < N ;i++){
st = new StringTokenizer(br.readLine());
for(int j= 0 ;j < M ;j++){
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
ArrayList<Integer> l = new ArrayList<>();
st = new StringTokenizer(br.readLine());
for (int i = 0; i < R; i++) {
l.add(Integer.parseInt(st.nextToken()));
}
int s = l.size();
for(int i =0 ;i < s; i++){
if(l.get(i) == 1){
upAndDownFlip(arr);
}else if(l.get(i) == 2){
leftAndRightFlip(arr);
}else if(l.get(i) == 3){
arr = rotateRight(arr);
}else if(l.get(i) == 4){
arr = rotateLeft(arr);
}else if(l.get(i) == 5){
operate5(arr);
}else if(l.get(i) == 6){
operate6(arr);
}
}
for(int x[] : arr){
for(int y : x){
bw.write(Integer.toString(y)+ " ");
}
bw.write("\n");
}
bw.close();
}
public void upAndDownFlip(int[][] m){
int r, c,t;
r = m.length;
c = m[0].length;
for(int j = 0 ;j < c;j ++){
for(int i = 0 ;i < r/2; i++){
t = m[i][j];
m[i][j] = m[r-i-1][j];
m[r-i-1][j] = t;
}
}
}
public void leftAndRightFlip(int[][]m){
int r, c,t;
r = m.length;
c = m[0].length;
for(int i = 0 ;i < r; i++){
for(int j= 0 ;j < c/2;j ++){
t = m[i][j];
m[i][j] = m[i][c - j - 1];
m[i][c - j - 1] = t;
}
}
}
public int[][] rotateRight(int[][]m){
int r, c,t;
r = m.length;
c = m[0].length;
int[][] ret = new int[c][r];
for(int i= 0 ;i < c; i++){
for(int j=0 ;j < r; j++){
ret[i][j] = m[r-j-1][i];
}
}
return ret;
}
public int[][] rotateLeft(int[][] m){
int r, c,t;
r = m.length;
c = m[0].length;
int[][] ret = new int[c][r];
for(int i= 0 ;i < c; i++){
for(int j=0 ;j < r; j++){
ret[i][j] = m[j][c-i-1];
}
}
return ret;
}
public void operate5(int[][]m){
int r, c,t, gOne, gTwo, gThree, gFour;
r = m.length/2;
c = m[0].length/2;
int [][]arrOne = new int[r][c];
int [][]arrTwo = new int[r][c];
int [][]arrThree = new int[r][c];
int [][]arrFour = new int[r][c];
InitsubArr(m, arrOne, 0, 0);
InitsubArr(m, arrTwo, 0, c);
InitsubArr(m, arrThree, r, 0);
InitsubArr(m, arrFour, r, c);
deepCopy2D(m, arrThree, 0, 0);
deepCopy2D(m, arrOne, 0, c);
deepCopy2D(m, arrFour, r, 0);
deepCopy2D(m, arrTwo, r, c);
}
public void operate6(int[][] m){
int r, c,t, gOne, gTwo, gThree, gFour;
r = m.length/2;
c = m[0].length/2;
int [][]arrOne = new int[r][c];
int [][]arrTwo = new int[r][c];
int [][]arrThree = new int[r][c];
int [][]arrFour = new int[r][c];
InitsubArr(m, arrOne, 0, 0);
InitsubArr(m, arrTwo, 0, c);
InitsubArr(m, arrThree, r, 0);
InitsubArr(m, arrFour, r, c);
deepCopy2D(m, arrTwo, 0, 0);
deepCopy2D(m, arrFour, 0, c);
deepCopy2D(m, arrOne, r, 0);
deepCopy2D(m, arrThree, r, c);
}
public void InitsubArr(int[][] t, int[][]o, int x, int y){
int r, c;
r = o.length;
c = o[0].length;
for(int i = x ; i< x + r ;i++){
for(int j = y ; j< y + c;j ++){
o[i-x][j-y] = t[i][j];
}
}
}
public void deepCopy2D(int[][] t, int[][]o, int x, int y){
//(x,y) 시작 위치
int r, c;
r = o.length;
c = o[0].length;
for(int i = x ; i< x + r ;i++){
for(int j = y ; j< y + c;j ++){
t[i][j] = o[i-x][j-y];
}
}
}
}
'알고리즘 > 백준' 카테고리의 다른 글
[Java] 백준 1074 Z (0) | 2023.07.04 |
---|---|
[Java] 백준 17829 222-풀링 (0) | 2023.07.04 |
[Java] 백준 10799 쇠막대기 (0) | 2023.06.29 |
[Java] 백준 5464.주차장 (0) | 2023.06.27 |
[Java] 백준 1992. 쿼드트리 (0) | 2023.06.27 |
Comments