`greedy
greedy한 문제를 풀고 싶어서 내가 선택한 문제.
단순히 처음부터 검사해서 다르면 전환해서 풀면 되는 문제였다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class B1080 {
static int N, M,cnt=0;
static boolean [][] A, B;
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
// matrix를 받는 함수
A = getMatrix(A);
B = getMatrix(B);
if (N < 3 || M < 3) {
System.out.println(isSame() ? cnt : -1);
return ;
}
for(int i=0;i<N-2;i++) {
for(int j=0;j<M-2;j++) {
if(A[i][j] ^ B[i][j]) {
// 다를 경우 바꾸는 함수
cnt += change(i,j);
}
}
}
// 전체적으로 확인
System.out.println(isSame() ? cnt : -1);
}
private static boolean[][] getMatrix(boolean[][] getM) throws IOException{
getM = new boolean[N][M];
for(int i=0;i<N;i++) {
String b = br.readLine();
for(int j=0;j<M;j++) {
getM[i][j] = b.charAt(j) == '1' ? true : false;
}
}
return getM;
}
private static boolean isSame() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (A[i][j] != B[i][j])
return false;
}
}
return true;
}
// 3 * 3 영역을 바꾸는 함수
private static int change(int row, int col) {
for (int i = row; i < row + 3; i++) {
for (int j = col; j < col + 3; j++) {
A[i][j] = !A[i][j];
}
}
return 1;
}
}
|