[소프티어] 9657번 나무공격(Java)

윤성모·2024년 10월 30일

알고리즘 문제 풀이

내 풀이

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);
    }
}

느낀점

  • 너무 문제 기능 구현에만 신경을 써서 깔끔하게 리팩토링을 해야할것 같네요!!!
profile
Java / Spring 개발 공부 일지

0개의 댓글