교육/알고리즘_기본

📌 배열(개념)

Jedy_Kim 2024. 7. 11. 13:28
728x90

🎯 회전시키기

🏃🏻90도 회전

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
public class Sample { 
 
    public static void main(String[] args) { 
        int[][] matrix = {{123}, {456}, {789}};
        rotate90(matrix);
        printMatrix(matrix); 
    }
 
    public static void rotate90(int[][] matrix) {
 
        int N = matrix.length;
        int[][] temp = new int[N][N];
 
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                temp[i][j] = matrix[N - j - 1][i];
            }
        }
 
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                matrix[i][j] = temp[i][j];
            }
        }
    }
 
    public static void printMatrix(int[][] matrix) {
        for(int i = 0; i < matrix.length; i++) {
            for(int j = 0; j < matrix[i].length; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }
 
}
cs

🏃🏻180도 회전

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
public class Sample {
 
    public static void main(String[] args) {
        int[][] matrix = {{123}, {456}, {789}};
        rotate180(matrix);
        printMatrix(matrix);
    } 
 
    public static void rotate180(int[][] matrix) {
        int N = matrix.length;
        int[][] temp = new int[N][N];
 
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                temp[i][j] = matrix[N - i - 1][N - j - 1];
            }
        }
 
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                matrix[i][j] = temp[i][j];
            }
        }
    }
 
    public static void printMatrix(int[][] matrix) {
        for(int i = 0; i < matrix.length; i++) {
            for(int j = 0; j < matrix[i].length; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }
 
}
cs

🏃🏻270도 회전

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
public class Sample {
 
    public static void main(String[] args) {
        int[][] matrix = {{123}, {456}, {789}};
        rotate270(matrix);
        printMatrix(matrix);
    }
 
    public static void rotate270(int[][] matrix) {
        int N = matrix.length;
        int[][] temp = new int[N][N];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                temp[i][j] = matrix[j][N - i - 1];
            }
        }
 
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                matrix[i][j] = temp[i][j];
            }
        }
    }
 
    public static void printMatrix(int[][] matrix) {
        for(int i = 0; i < matrix.length; i++) {
            for(int j = 0; j < matrix[i].length; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }
 
}
cs

 

관련문제 : https://school.programmers.co.kr/learn/courses/30/lessons/120844

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

응용 : https://school.programmers.co.kr/learn/courses/30/lessons/68645

 

🎯 4방탐색

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
public class Sample {
 
    // 상하좌우
    private static final int[] dx = {00-11};
    private static final int[] dy = {1-100};
    
    public static void main(String[] args) {
        int[][] matrix = {{123}, {456}, {789}};
        searchInFourDirections(matrix, 11);
        printMatrix(matrix);
    }
 
    // 주어진 격자(grid) 내에서 특정 위치(x, y)에서 4방향 탐색을 수행하는 메소드
    public static void searchInFourDirections(int[][] matrix, int x, int y) {
 
        int row = matrix.length;
        int col = matrix[0].length;
 
        for (int dir = 0; dir < 4; dir++) {
            int newX = x + dx[dir];
            int newY = y + dy[dir];
 
            // 범위체크
            if ( (newX < 0 || newX > row) || (newY < 0 || newY > col) ) continue;
 
            System.out.println("Value at (" + newX + ", " + newY + "): " + matrix[newX][newY]);
        }
 
    }
 
    public static void printMatrix(int[][] matrix) {
        for(int i = 0; i < matrix.length; i++) {
            for(int j = 0; j < matrix[i].length; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }
 
}
cs

관련문제 : https://www.acmicpc.net/problem/18428

응용(8방탐색) : https://mallange.tistory.com/m/48

 

[백준 4963번] 섬의 개수 (Java)

https://www.acmicpc.net/problem/4963 4963번: 섬의 개수 입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스의 첫째 줄에는 지도의 너비 w와 높이 h가 주어진다. w와 h는 50보다 작거나 같은

mallange.tistory.com

 

 

 

반응형

'교육 > 알고리즘_기본' 카테고리의 다른 글

📌 자료구조 : Set, Map  (0) 2024.07.18
📌 재귀함수  (0) 2024.07.18
📌 자료구조 : 스택, 큐, 덱  (0) 2024.07.01
📌 배열(문제집)  (0) 2024.06.26
📌시간 복잡도  (0) 2024.06.26