본문 바로가기

Algorithm/CodeUp

[CodeUp_Java] Q1097 : [기초-2차원배열] 바둑알 십자 뒤집기

반응형

안녕하세요! Plitche(플리체)입니다.
이번 포스팅의 주제는 Q1097 : [기초-2차원배열] 바둑알 십자 뒤집기 (자바, JAVA)입니다.

intro

Question

문제 설명

입력

바둑알이 깔려 있는 상황이 19 * 19 크기의 정수값으로 입력된다.
십자 뒤집기 횟수(n)가 입력된다.
십자 뒤집기 좌표가 횟수(n) 만큼 입력된다. 단, n은 10이하의 자연수이다.

출력

십자 뒤집기 결과를 출력한다.

예시

  • 입력 :
    0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
    2
    10 10
    12 12
  • 출력 :
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Solution (풀이)

  • 풀이 1 : 메모리 14908, 시간 131
public class Answer1 {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int[][] location = new int[19][19];    // 바둑판 가로세로 길이
        for (int i=0; i<19; i++) {
            for (int j=0; j<19; j++) {
                // 바둑알이 놓인 자리에 흰 돌(1), 검은 돌(0)을 입력받아 저장한다.
                location[i][j] = sc.nextInt();
            }
        }

        int n = sc.nextInt(); // 뒤집을 횟수
        int[] x = new int[n]; // 뒤집을 횟수만큼 배열 길이선언 (가로)
        int[] y = new int[n]; // 뒤집을 횟수만큼 배열 길이선언 (세로)

        for (int i=0; i<n; i++) { // 뒤집기 횟수만큼 for문 반복
            x[i] = sc.nextInt()-1; //뒤집을 좌표(가로) 저장
            y[i] = sc.nextInt()-1; //뒤집을 좌표(세로) 저장
        }

        // 바둑알 뒤집기
        for (int i=0; i<n; i++) { // 뒤집을 횟수만큼 for문 반복
            for (int j=0; j<19; j++) { //해당 줄 모두 뒤집기 위해서
                // x좌표 고정에 y줄 뒤집기
                if(location[x[i]][j]==0) {    // 기존이 검은돌이면
                    location[x[i]][j] = 1;    // 흰돌로 뒤집기
                } else {        
                    location[x[i]][j] = 0;    // 검은돌로 뒤집기
                }

                // y좌표 고정에 x줄 뒤집기                
                if(location[j][y[i]]==0) {
                    location[j][y[i]] = 1;
                } else {
                    location[j][y[i]] = 0;
                }
            }
        }

        for (int i=0; i<location.length; i++) {
            for (int j=0; j<location.length; j++) {
                System.out.print(location[i][j] + " ");    //2중 포문을 사용하여 출력
            }
            System.out.println();    // inner 포문 종료 후 줄 띄우기
        }

        sc.close();
    }
}
  • 풀이 2 : 메모리 11316, 시간 77
public class Answer2 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();

        int[][] location = new int[20][20];    // 바둑판 가로세로 길이

        for (int i=1; i<20; i++) {
            // 한 줄 씩 입력받기
            StringTokenizer st = new StringTokenizer(br.readLine(), " ");
            for (int j=1; j<20; j++) {
                // 바둑알이 놓인 자리에 흰 돌(1), 검은 돌(0)을 입력받아 저장한다.
                location[i][j] = Integer.parseInt(st.nextToken());
            }
        }

        int n = Integer.parseInt(br.readLine()); // 뒤집을 횟수

        for (int i=0; i<n; i++) { // 뒤집기 횟수만큼 for문 반복
            // 뒤집을 좌표 저장하기
            StringTokenizer st = new StringTokenizer(br.readLine(), " ");
            int x = Integer.parseInt(st.nextToken()); //뒤집을 좌표(가로) 저장
            int y = Integer.parseInt(st.nextToken()); //뒤집을 좌표(세로) 저장

            for (int j=1; j<20; j++) {
                // x좌표 고정에 y줄 뒤집기
                if(location[x][j]==0) {    // 기존이 검은돌이면
                    location[x][j] = 1;    // 흰돌로 뒤집기
                } else {        
                    location[x][j] = 0;    // 검은돌로 뒤집기
                }

                // y좌표 고정에 x줄 뒤집기                
                if(location[j][y]==0) {
                    location[j][y] = 1;
                } else {
                    location[j][y] = 0;
                }
            }
        }

        for (int i=1; i<20; i++) {
            for (int j=1; j<20; j++) {
                sb.append(location[i][j]).append(" "); //2중 포문을 사용하여 sb에 저장
            }
            sb.append("\n");    // 줄 바꿈
        }

        System.out.println(sb);
        br.close();
    }
}

Ranking(순위)

반응형