본문 바로가기
문제풀이/소프티어

[소프티어] 9657번: 나무공격격 (Java/자바)

by StarDev 2024. 10. 15.
반응형
SMALL

문제

https://softeer.ai/practice/9657

 

소스코드

import java.io.*;
import java.util.*;
import java.awt.Point;

public class Main {

    public static int N, M;
    public static int map[][];
    public static Queue<Point> queue = new LinkedList<>();
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine(), " ");

        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());

        map = new int[N][M];
        for(int i = 0; i < N; i++){
            st = new StringTokenizer(br.readLine());
            for(int j = 0; j < M; j++){
                map[i][j] = Integer.parseInt(st.nextToken());
            }
        }

        process(br.readLine());// wave 1
        process(br.readLine());// wave 2

        int res = 0;
        
        for(int i = 0; i < N; i++){
            for(int j = 0; j < M; j++){
                if(map[i][j] == 1) res++;        
            }
        }

        System.out.println(res);
    }

    public static void process(String args) {
        String[] str = args.split(" ");
        
        int sy = Integer.parseInt(str[0]) - 1;
        int ey = Integer.parseInt(str[1]);

        for(int i = sy; i < ey; i++){
            queue.offer(new Point(-1, i));
        }

        while(!queue.isEmpty()){
            Point p = queue.poll();
            int y = p.y;
            int x = p.x + 1;

            if(x >= M) continue;

            if(map[y][x] == 1){
                map[y][x] = 0;
                continue;
            }

            queue.offer(new Point(x, y));
        }
    }
}
반응형
LIST

댓글