본문 바로가기

Algorithm/CodeUp

[CodeUp_Java] Q1096 : [기초-2차원배열] 바둑판에 흰 돌 놓기

반응형

안녕하세요! Plitche(플리체)입니다.
이번 포스팅의 주제는 Q1096 : [기초-2차원배열] 바둑판에 흰 돌 놓기 (자바, JAVA)입니다.

intro

Question

문제 설명

입력

바둑판에 올려 놓을 흰 돌의 개수(n)가 첫 줄에 입력된다.
둘째 줄 부터 n+1 번째 줄까지 힌 돌을 놓을 좌표(x, y)가 n줄 입력된다.
n은 10이하의 자연수이고 x, y 좌표는 1 ~ 19 까지이며, 같은 좌표는 입력되지 않는다.

출력

흰 돌이 올려진 바둑판의 상황을 출력한다.
흰 돌이 있는 위치는 1, 없는 곳은 0으로 출력한다.

예시

  • 입력 :
    5
    1 1
    2 2
    3 3
    4 4
    5 5
  • 출력 :
    1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 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 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 : 메모리 14476, 시간 128
public class Answer1 {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int stone = sc.nextInt();    // 바둑판에 놓인 돌의 개수

        int[][] location = new int[19][19];    // 바둑판 가로세로 길이
        for (int i=0; i<stone; i++) {
            int x = sc.nextInt()-1;    // 인덱스는 0부터 시작함으로 줄수 -1
            int y = sc.nextInt()-1;    // 인덱스는 0부터 시작함으로 줄수 -1

            location[x][y] = 1;    // 놓인 돌 위치의 배열 값을 1로 바꾸기
        }

        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 : 메모리 11204, 시간 65
public class Answer2 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        int stone = Integer.parseInt(br.readLine());    // 바둑판에 놓일 돌의 개수

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

        for (int i=0; i<stone; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine(), " ");
            // 바둑알이 놓일 때마다 x좌표 y좌표에 저장
            location[Integer.parseInt(st.nextToken())][Integer.parseInt(st.nextToken())] = 1;  
        }

        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(순위)

반응형