알고리즘(BOJ)/Silver
백준 - 행렬 1080
ch.0
2023. 2. 3. 21:37
문제 분석
0,0 좌표부터 비교해보면서 다르다면 3x3 영역을 바꿔주었다.
전체소스
package test;
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int[][] array_a = new int[x][y];
int[][] array_b = new int[x][y];
for (int i = 0; i < x; i++) {
String str = br.readLine();
for (int j = 0; j < y; j++) {
array_a[i][j] = str.charAt(j) - '0';
}
}
for (int i = 0; i < x; i++) {
String str = br.readLine();
for (int j = 0; j < y; j++) {
array_b[i][j] = str.charAt(j) - '0';
}
}
int result = 0;
for (int i = 0; i <= x - 3; i++) {
for (int j = 0; j <= y - 3; j++) {
if (array_a[i][j] != array_b[i][j]) {
for (int x_p = i; x_p < i + 3; x_p++) {
for (int y_p = j; y_p < j + 3; y_p++) {
if (array_a[x_p][y_p] == 0) {
array_a[x_p][y_p] = 1;
} else {
array_a[x_p][y_p] = 0;
}
}
}
result++;
}
}
}
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
if (array_a[i][j] != array_b[i][j]) {
result=-1;
}
}
}
System.out.println(result);
}
}
전체적인 풀이 방법만 알면 소스 구현은 쉬운 문제였다. 다만 0,0부터 비교해보면서 바꿔나가 발상을 생각하기는 쉽지 않았다고 생가한다.
문제
https://www.acmicpc.net/problem/1080
1080번: 행렬
첫째 줄에 행렬의 크기 N M이 주어진다. N과 M은 50보다 작거나 같은 자연수이다. 둘째 줄부터 N개의 줄에는 행렬 A가 주어지고, 그 다음줄부터 N개의 줄에는 행렬 B가 주어진다.
www.acmicpc.net