Problem Solving/Baekjoon
[백준] 14457 Cow Tipping - Greedy / Java
graycode
2023. 10. 23. 18:25
• 문제 링크
14457번: Cow Tipping
Farmer John occasionally has trouble with bored teenagers who visit his farm at night and tip over his cows. One morning, he wakes up to find it has happened again -- his N2 cows began the night grazing in a perfect N×N square grid arrangement (1 ≤ N
www.acmicpc.net
• 풀이 코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
static char[][] mat;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
mat = new char[n][n];
for (int i = 0; i < n; i++) mat[i] = br.readLine().toCharArray();
int cnt = 0;
for (int i = n - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
if (mat[i][j] == '1') {
swap(i, j);
cnt++;
}
}
}
bw.write(String.valueOf(cnt));
bw.flush();
}
private static void swap(int y, int x) {
for (int i = y; i >= 0; i--)
for (int j = x; j >= 0; j--) mat[i][j] = mat[i][j] == '1' ? '0' : '1';
}
}