알고리즘 문제 풀이
내 풀이
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String tableInfo = br.readLine();
int[][] table = createTable(tableInfo);
setTable(br, table);
String firstAttackInfo = br.readLine();
attackDestroyer(table, firstAttackInfo);
String secondAttackInfo = br.readLine();
attackDestroyer(table, secondAttackInfo);
printDestoyersCount(table);
}
private static int[][] createTable(String tableInfo) {
int row = Integer.parseInt(tableInfo.split(" ")[0]);
int col = Integer.parseInt(tableInfo.split(" ")[1]);
return new int[row][col];
}
private static void setTable(BufferedReader br, int[][] table) throws IOException {
for(int i=0; i<table.length; i++) {
String rowInfo = br.readLine();
for(int j=0; j<table[0].length; j++) {
table[i][j] = Integer.parseInt(rowInfo.split(" ")[j]);
}
}
}
private static void attackDestroyer(int[][] table, String attackInfo) {
int attackRow1 = Integer.parseInt(attackInfo.split(" ")[0]);
int attackRow2 = Integer.parseInt(attackInfo.split(" ")[1]);
for (int i = attackRow1 - 1; i < attackRow2; i++) {
for (int j = 0; j < table[0].length; j++) {
if (table[i][j] == 1) {
table[i][j] = 0;
break;
}
}
}
}
private static void printDestoyersCount(int[][] table) {
int count = 0;
for (int[] row : table) {
for (int j = 0; j < table[0].length; j++) {
if (row[j] == 1) {
count++;
}
}
}
System.out.println(count);
}
}
느낀점
- 너무 문제 기능 구현에만 신경을 써서 깔끔하게 리팩토링을 해야할것 같네요!!!