떼닝로그

[Java] Softeer 나무 공격 (배열) 본문

Algorithms/Softeer

[Java] Softeer 나무 공격 (배열)

떼닝 2024. 10. 31. 17:14

[Java] Softeer 나무 공격 (배열)

문제 링크 : https://softeer.ai/practice/9657

 

Softeer - 현대자동차그룹 SW인재확보플랫폼

 

softeer.ai

 

기록의 이유...

별 거 없다...

그냥 굉장히 지저분하게 푼 나의 코드 구경하라고... 글 올려본다...

 

입력 받을 땐 Scanner sc = new Scanner(System.in); 해줘야 한다

 

그리고 다행히 옛날에 C 했던 게 조금 남아있어서 배열 선언정도는 할 수 있다 (하핫)

 

등호 기준으로

- 왼쪽에는 자료형에 내가 구현하고 싶은 차원만큼 [] 개수 작성하고 변수 명 작성하고

- 오른쪽에는 new로 자료형 객체 생성, 그리고 [] 안에 배열 크기 작성해주면 된다

ex. int[][] board = new int[6][5];

 

배열 선언할 때 0으로 값 다 채우기 이런 게 옛날에 C에는 있었던 것 같은데, 자바는 잘 모르겠네,,, 물론 찾아보면 나올듯

import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int row = sc.nextInt();
        int col = sc.nextInt();

        int[][] board = new int[row][col];
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                board[i][j] = sc.nextInt();
            }
        }

        int start1 = sc.nextInt();
        int end1 = sc.nextInt();
        int start2 = sc.nextInt();
        int end2 = sc.nextInt();

        int[] rowcnt = new int[row];
        for(int i=0;i<row;i++){
            rowcnt[i] = 0;
        }

        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                if(board[i][j] == 1)
                    rowcnt[i] += 1;
            }
        }

        for(int i=start1-1;i<=end1-1;i++){
            if(rowcnt[i] > 0)
                rowcnt[i] -= 1;
        }

        for(int i=start2-1;i<=end2-1;i++){
            if(rowcnt[i] > 0)
                rowcnt[i] -= 1;
        }


        int result = 0;
        for(int i=0;i<row;i++)
            result += rowcnt[i];

        System.out.print(result);
    }
}



Comments