-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
74 lines (65 loc) · 1.93 KB
/
Main.java
File metadata and controls
74 lines (65 loc) · 1.93 KB
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int[][] A, B;
static int answer, M, N;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
A = new int[N][M];
for(int i=0; i<N; i++){
String str = br.readLine();
for(int j=0; j<M; j++){
A[i][j] = Integer.parseInt(String.valueOf(str.charAt(j)));
}
}
B = new int[N][M];
for(int i=0; i<N; i++){
String str = br.readLine();
for(int j=0; j<M; j++){
B[i][j] = Integer.parseInt(String.valueOf(str.charAt(j)));
}
}
answer = 0;
// 3 x 3 을 뒤집기 위한 loop
for(int i=0; i<N-2; i++){
for(int j=0; j<M-2; j++){
if(A[i][j] != B[i][j]){
flip(i,j,A);
answer++;
}
}
}
if(check()){
System.out.println(answer);
} else {
System.out.println(-1);
}
br.close();
}
public static void flip(int x, int y, int[][] A) {
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
if(A[x+i][y+j] == 1) {
A[x+i][y+j] = 0;
} else {
A[x+i][y+j] = 1;
}
}
}
}
public static boolean check(){
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;
}
}