[코드업 기초 100제] [기초 - 2차원 배열] 코드업 1096 ~ 1099 자바 풀이

2022. 2. 16. 00:24코딩테스트/코드업

728x90
반응형

문제 출처


https://github.com/haessae0/MCTP

 

1096. 바둑판에 흰 돌 놓기

import java.util.*;

public class codeup1096 {
  public static void main(String[] args) throws Exception {
    Scanner sc = new Scanner(System.in);
    int[][] array = new int[19][19];
    int a = sc.nextInt();

    for (int i = 0; i < a; i++) {
      int x = sc.nextInt();
      int y = sc.nextInt();

      array[x - 1][y - 1] = 1;
    }

    for (int i = 0; i < 19; i++) {
      for (int j = 0; j < 19; j++) {
        System.out.print(array[i][j] + " ");
      }
      System.out.println();
    }

  }

}

 

1097. 바둑알 십자 뒤집기

import java.util.Scanner;

public class codeup1097 {
  public static void main(String[] args) throws Exception {
    Scanner sc = new Scanner(System.in);
    int[][] array = new int[19][19];

    for (int i = 0; i < array.length; i++) {
      for (int j = 0; j < array.length; j++) {
        array[i][j] = sc.nextInt();
      }
    }

    int a = sc.nextInt();

    for (int n = 0; n < a; n++) {
      int x = sc.nextInt() - 1;
      int y = sc.nextInt() - 1;

      for (int i = 0; i < array.length; i++) {
        if (array[x][i] == 0) {
          array[x][i] = 1;
        } else {
          array[x][i] = 0;
        }
      }

      for (int j = 0; j < array.length; j++) {
        if (array[j][y] == 0) {
          array[j][y] = 1;
        } else {
          array[j][y] = 0;
        }
      }
    }

    for (int i = 0; i < array.length; i++) {
      for (int j = 0; j < array.length; j++) {
        System.out.print(array[i][j] + " ");
      }
      System.out.println();
    }
  }
}

 

1098. 설탕과자 뽑기

import java.util.Scanner;

public class codeup1098 {
  public static void main(String[] args) throws Exception {
    Scanner sc = new Scanner(System.in);

    int w = sc.nextInt();
    int h = sc.nextInt();
    int[][] array = new int[w][h];

    int n = sc.nextInt();

    for (int time = 0; time < n; time++) {
      int l = sc.nextInt();
      int d = sc.nextInt();
      int x = sc.nextInt() - 1;
      int y = sc.nextInt() - 1;

      for (int i = 0; i < l; i++) {
        if (d == 0) {
          if (y - 1 * i < 100 - h) {
            array[x][y + i] = 1;
          }
        } else {
          if (x - 1 * i < 100 - w) {
            array[x + i][y] = 1;
          }
        }
      }

    }
    for (int i = 0; i < w; i++) {
      for (int j = 0; j < h; j++) {
        System.out.print(array[i][j] + " ");
      }
      System.out.println();
    }
  }

}

 

1099. 성실한 개미

import java.util.Scanner;

public class codeup1099 {
  public static void main(String[] args) throws Exception {
    Scanner sc = new Scanner(System.in);
    int[][] maze = new int[10][10];

    for (int i = 0; i < maze.length; i++) {
      for (int j = 0; j < maze.length; j++) {
        maze[i][j] = sc.nextInt();
      }
    }

    int x = 1;
    int y = 1;

    while (x < 10 && y < 10) {
      if (maze[1][1] == 2) {
        maze[1][1] = 9;
        break;
      } else {
        maze[1][1] = 9;
      }

      if (x < 10 && y + 1 < 10 && maze[x][y + 1] == 0) {
        maze[x][y + 1] = 9;
        y++;
      } else if (x < 10 && y + 1 < 10 && maze[x][y + 1] == 2) {
        maze[x][y + 1] = 9;
        break;
      } else if (x + 1 < 10 && y < 10 && maze[x + 1][y] == 0) {
        maze[x + 1][y] = 9;
        x++;
      } else if (x + 1 < 10 && y < 10 && maze[x + 1][y] == 2) {
        maze[x + 1][y] = 9;
        break;
      } else {
        break;
      }
    }

    for (int i = 0; i < maze.length; i++) {
      for (int j = 0; j < maze[i].length; j++) {
        System.out.print(maze[i][j] + " ");
      }
      System.out.println();
    }

  }

}
728x90
반응형