Updated:

1. 문제 링크

https://www.acmicpc.net/problem/1080

2. 사용 알고리즘

그리디

3. 풀이

왼쪽 위에서부터 오른쪽으로 차례대로 순회하며, 행렬 B와 같은값이 되도록 수를 뒤집음

4. 소스 코드

4-1. C++

https://github.com/dev-aiden/problem-solving/blob/main/boj/1080.cpp

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
#include <iostream>

using namespace std;

int arr[53][53], arr2[53][53];

int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    int n, m; cin >> n >> m;
    for(int i = 0; i < n; ++i) {
        string s; cin >> s;
        for(int j = 0; j < m; ++j) arr[i][j] = s[j] - '0';
    }
    for(int i = 0; i < n; ++i) {
        string s; cin >> s;
        for(int j = 0; j < m; ++j) arr2[i][j] = s[j] - '0';
    }
    int ans = 0;
    for(int i = 0; i < n - 2; ++i) {
        for(int j = 0; j < m - 2; ++j) {
            if(arr[i][j] != arr2[i][j]) {
                ++ans;
                for(int k = 0; k < 3; ++k) {
                    for(int l = 0; l < 3; ++l) {
                        arr[i + k][j + l] = !arr[i + k][j + l];
                    }
                }
            }
        }
    }
    int flag = 1;
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < m; ++j) {
            if(arr[i][j] != arr2[i][j]) flag = 0;
        }
    }
    if(!flag) cout << -1 << "\n";
    else cout << ans << "\n";
    return 0;
}

4-2. JAVA

https://github.com/dev-aiden/problem-solving/blob/main/boj/1080.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

    static int arr[][] = new int[53][53];
    static int arr2[][] = new int[53][53];

    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());
        for(int i = 0; i < n; ++i) {
            String s = br.readLine();
            for(int j = 0; j < m; ++j) arr[i][j] = s.charAt(j) - '0';
        }
        for(int i = 0; i < n; ++i) {
            String s = br.readLine();
            for(int j = 0; j < m; ++j) arr2[i][j] = s.charAt(j) - '0';
        }
        int ans = 0;
        for(int i = 0; i < n - 2; ++i) {
            for(int j = 0; j < m - 2; ++j) {
                if(arr[i][j] != arr2[i][j]) {
                    ++ans;
                    for(int k = 0; k < 3; ++k) {
                        for(int l = 0; l < 3; ++l) {
                            if(arr[i + k][j + l] == 1) arr[i + k][j + l] = 0;
                            else arr[i + k][j + l] = 1;
                        }
                    }
                }
            }
        }
        int flag = 1;
        for(int i = 0; i < n; ++i) {
            for(int j = 0; j < m; ++j) {
                if(arr[i][j] != arr2[i][j]) flag = 0;
            }
        }
        if(flag == 0) System.out.println(-1);
        else System.out.println(ans);
    }
}

Updated:

Leave a comment