-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix_greedy.java
77 lines (61 loc) · 2.19 KB
/
Matrix_greedy.java
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
69
70
71
72
73
74
75
76
77
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Matrix_greedy {
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
// A행렬 생성.
int[][] aArray = new int[N][M];
for (int i=0; i<N; i++){
String s = br.readLine();
for (int j=0; j<M; j++){
aArray[i][j] = Integer.parseInt(s.substring(j,j+1));
}
}
// B행렬 생성.
int[][] bArray = new int[N][M];
for (int i=0; i<N; i++){
String s = br.readLine();
for (int j=0; j<M; j++){
bArray[i][j] = Integer.parseInt(s.substring(j,j+1));
}
}
int count =0;
for (int i=0; i<N-2; i++){
for (int j=0; j<M-2; j++){
if(aArray[i][j] != bArray[i][j]){
count++;
transfer(aArray,i,j);
}
}
}
if (Comparator(aArray,bArray)) System.out.println(count);
else System.out.println(-1);
// for (int i=0; i<N; i++){
// for (int j=0; j<M; j++){
// System.out.print(Barray[i][j]);
// }
// System.out.println();
// }
}
public static void transfer(int[][] array, int a, int b){
for (int i=a; i<a+3; i++){
for (int j=b; j<b+3; j++){
if(array[i][j]==1) array[i][j]=0;
else array[i][j]=1;
}
}
}
public static boolean Comparator(int[][] aArray, int[][] bArray){
for (int i=0; i<aArray.length; i++){
for (int j=0; j<aArray[0].length; j++){
if (aArray[i][j] != bArray[i][j]) return false;
}
}
return true;
}
}