자료구조&알고리즘/알고리즘-문제풀이
[코드업 기초100제] 1097-[기초-2차원배열] 바둑알 십자 뒤집기
파이3.14
2020. 5. 6. 01:52
부모님을 기다리던 영일이는 검정/흰 색 바둑알을 바둑판에 꽉 채워 깔아 놓고 놀다가...
"십(+)자 뒤집기를 해볼까?"하고 생각했다.
바둑판(19 * 19)에 흰 돌(1) 또는 검정 돌(0)이 모두 꽉 채워져 놓여있을 때,
n개의 좌표를 입력받아 십(+)자 뒤집기한 결과를 출력하는 프로그램을 작성해보자.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[][] plate = new int[19][19];
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19; j++) {
plate[i][j] = scan.nextInt();
}
}
// 겹치는 부분은 뒤집은 값
// 1이면 0으로 0이면 1로
int count = scan.nextInt();
for (int j = 0; j < count; j++) {
int x = scan.nextInt()-1;
int y = scan.nextInt()-1;
// left (variable y-1)
for (int n1 = y-1; n1 >= 0; n1--) {
if (plate[x][n1] == 1) {
plate[x][n1] = 0;
} else {
plate[x][n1] = 1;
}
}
// right (variable y+1)
for (int n2 = y+1; n2 < 19; n2++) {
if (plate[x][n2] == 1) {
plate[x][n2] = 0;
} else {
plate[x][n2] = 1;
}
}
// up (variable x-1)
for (int n3 = x-1; n3 >= 0; n3--) {
if (plate[n3][y] == 1) {
plate[n3][y] = 0;
} else {
plate[n3][y] = 1;
}
}
// down (variable x+1)
for (int n4 = x+1; n4 < 19; n4++) {
if (plate[n4][y] == 1) {
plate[n4][y] = 0;
} else {
plate[n4][y] = 1;
}
}
}
for (int n = 0; n < 19; n++) {
for (int m = 0; m < 19; m++) {
System.out.printf("%d ", plate[n][m]);
}
System.out.println();
}
}
}
반응형