코테/소프티어

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

zsunny 2024. 11. 1. 17:47

https://softeer.ai/practice/9657

 

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

 

softeer.ai

 

 

소프티어는 처음 풀어보게 되었는데, 와 나 IDE에 엄청난 의존을 하고 있었구나를 느끼게 해주었다ㅠ 프로그래머스랑도 또 다름ㅠ

무튼 이 문제를 읽고 솔직히 처음엔 배열돌리기 생각했는데 아무리 생각해도 넘 단순노동인 것 같고, 큐를 이용하기로 했다. 공격해서 제거만하면 되니까!

작성해놓고 보니 입출력 빼곤 짧고 단순한 구현문제.

 

1. 환경파괴범의 위치를 큐에 넣는다.

2. 하나씩 빼서 입력받은 행 안에 있는 지 확인한다.

3. 안에 있고 &*이전에 공격 당한 환경파괴범이 없는 행*이면 visited에 체크해주고 q에 다시 넣지 않는다 (=제거)

4. 그외(공격 행에 있지 않거나 이전에 공격 당한 환경파괴범이 있는 경우 즉, visited가 true인 경우)는 다시 q에 넣어준다.

5. q 사이즈를 출력해주면 답!

 

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

public class Main {

    static int n, m;
    static Queue<Point> q;

    static class Point{
        int x, y;
        Point(int x, int y){
            this.x = x;
            this.y = y;
        }
    }

    public static void calc(int s, int e){
        boolean[] visited = new boolean[n+1];
        int qSize = q.size();
        while(qSize --> 0){
            Point p = q.poll();
            // 환경파괴범이 공격 행에 있으면 삭제. 단 1명만.
            if(p.x >= s && p.x <= e && !visited[p.x]) visited[p.x] = true;
            else q.add(new Point(p.x, p.y));
        }
    }

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

        // 환경 파괴범 위치 저장
        q = new LinkedList<>();
        for(int i=0; i<n; i++){
            st = new StringTokenizer(br.readLine());
            for(int j=0; j<m; j++){
                int now = Integer.parseInt(st.nextToken());
                if(now == 1) q.add(new Point(i, j));
            }
        }
        // 공격 정보
        for(int i=0; i<2; i++){
            st = new StringTokenizer(br.readLine());
            int s = Integer.parseInt(st.nextToken());
            int e = Integer.parseInt(st.nextToken());
            calc(s-1, e-1);
        }
        System.out.println(q.size());
    }
}

 

처음에 저 while 문 안의 조건을 잘못 설정해줘서 애먹었다ㅠ 정신 똑디 차리자

*** if - else 로 해주어야 하는지, 그냥 if만 해줘도 되는지 확인할 것!!!

*** 그냥 if(visited[]) continue; 해도 되는지, 아님 조건문 안에서 같이 체크해주어야하는 지 확인할 것!!!