기초 5-3 2차원 배열
## 1096
19*19의 바둑 판이 있다 > 바둑알 몇 개 놓을건지 입력 > 차례로 그만큼 좌표(두 수) 입력 > 바둑판 빈거 0 있는거 1로 출력
import java.util.Scanner;
public class CodeUp1096 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] map = new int[19][19];
int n = sc.nextInt();
int[][] inp = new int[n][2];
for(int i = 0;i<n;i++) {
inp[i][0]=sc.nextInt();
inp[i][1]=sc.nextInt();
}
sc.close();
for(int i = 0;i<n;i++) {
map[inp[i][0]-1][inp[i][1]-1]=1;
}
for (int i =0;i<19;i++) {
for(int j=0;j<19;j++) {
System.out.printf("%d ",map[i][j]);
}
System.out.println();
}
}
}
- 판이 크지만 간단했다.
## 1097
19*19의 바둑 판이 있다 > 바둑판 전체를 19*19로 입력받고 > 몇 번 뒤집을지 입력 > 그만큼 차례로 좌표 입력 > 좌표 해당하는 행과 열 전부를 뒤집음 (0은 1로 1은 0으로)
import java.util.Scanner;
public class CodeUp1097 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] map = new int[19][19];
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19; j++) {
map[i][j] = sc.nextInt();
}
}
int n = sc.nextInt();
int[][] inp = new int[n][2];
for (int i = 0; i < n; i++) {
inp[i][0] = sc.nextInt();
inp[i][1] = sc.nextInt();
}
sc.close();
//
for (int i = 0; i < n; i++) {
for (int j = 0; j < 19; j++) {
map[inp[i][0] - 1][j] = (map[inp[i][0] - 1][j] == 1) ? 0 : 1;
}
for (int j = 0; j < 19; j++) {
map[j][inp[i][1] - 1] = (map[j][inp[i][1] - 1] == 1) ? 0 : 1;
}
}
//
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19; j++) {
System.out.printf("%d ", map[i][j]);
}
System.out.println();
}
}
}
- 엄 비트반전 연산으로 할 수 있을 것 같은데 안해봐서 그냥 아는대로 if로 두 경우 나눠서 했다.
## 1098
행과 열을 입력받음 > 막대 개수 입력받음 > 개수만큼의 4칸짜리 배열 입력받음. index별로 [0]: 막대길이, [1]: 가로는 0 세로는 1(가로면 우로, 세로면 아래로), [2][3]: 시작 좌표 > 빈칸 0, 막대 있는 칸 1로 판을 출력
import java.util.Scanner;
public class CodeUp1098 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int r = sc.nextInt();
int c = sc.nextInt();
int[][] map = new int[r][c];
int n = sc.nextInt();
int[][] info = new int[n][4];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
info[i][j] = sc.nextInt();
}
}
sc.close();
for (int i = 0; i < n; i++) {
if (info[i][1] == 0) {
for (int j = 0; j < info[i][0]; j++) {
map[info[i][2] - 1][info[i][3] - 1 + j] = 1;
}
} else {
for (int j = 0; j < info[i][0]; j++) {
map[info[i][2] - 1 + j][info[i][3] - 1] = 1;
}
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
System.out.printf("%d ", map[i][j]);
}
System.out.println();
}
}
}
- 약간 길어져서 마지막에 좀 x y가 헷갈렸지만 생각보다 간단하다.
'PS - CodeUp' 카테고리의 다른 글
1411, 1492 - 빠진 카드, 1차원 누적 합 배열 만들기 (결국 둘 다 새로 짬) (0) | 2022.01.20 |
---|---|
3011 - 버블 정렬 + (0) | 2022.01.20 |
1093, 1094, 1095 - 이상한 출석 번호 부르기 1, 2, 3 (0) | 2022.01.20 |
1441, 1442, 1443 - 버블 정렬, 선택 정렬, 삽입 정렬 (0) | 2022.01.20 |
1099 - 성실한 개미 (0) | 2022.01.19 |