https://www.acmicpc.net/problem/20056

 

20056번: 마법사 상어와 파이어볼

첫째 줄에 N, M, K가 주어진다. 둘째 줄부터 M개의 줄에 파이어볼의 정보가 한 줄에 하나씩 주어진다. 파이어볼의 정보는 다섯 정수 ri, ci, mi, si, di로 이루어져 있다. 서로 다른 두 파이어볼의 위치

www.acmicpc.net

이번주 연휴여서  가족끼리 간단히 여행다녀와서 주말에 공부를 많이 못했습니다.

 

여기서 핵심 포인트는 파이어볼이 나눠질때 나눠지는 순간에는 움직이지 않는다는 점을 주의 해야합니다.

그걸 모르면 음 나눠질때도 움직이게 계산을 하는 실수를 범할수 있습니다. 

문제는 조금 많이 생각해야하는 문제네요.

package test01;
import java.util.*;
import java.util.*;
import java.io.*;

public class Main {
    static int N, M, K;
    static ArrayList<Fireball>[][] map;
    static int[] dr = {-1, -1, 0, 1, 1, 1, 0, -1};
    static int[] dc = {0, 1, 1, 1, 0, -1, -1, -1};

    public static class Fireball {
        int r, c, m, s, d;

        public Fireball(int r, int c, int m, int s, int d) {
            this.r = r;
            this.c = c;
            this.m = m;
            this.s = s;
            this.d = d;
        }
    }

    public static void main(String[] args) throws IOException {
       Scanner scan=new Scanner(System.in);
        N = scan.nextInt();
        M =  scan.nextInt();
        K =  scan.nextInt();

        map = new ArrayList[N][N];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                map[i][j] = new ArrayList<Fireball>();
            }
        }

        for (int i = 0; i < M; i++) {
           
            int r =  scan.nextInt()- 1;
            int c =  scan.nextInt() - 1;
            int m =  scan.nextInt();
            int s =  scan.nextInt();
            int d = scan.nextInt();
            map[r][c].add(new Fireball(r, c, m, s, d));
        }

        for (int k = 0; k < K; k++) {
            moveFireballs();
            DivideFireballs();
        }

        int result = 0;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                for (Fireball f : map[i][j]) {
                    result += f.m;
                }
            }
        }

        System.out.println(result);
    }

    public static void moveFireballs() {
        ArrayList<Fireball>[][] newMap = new ArrayList[N][N];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                newMap[i][j] = new ArrayList<Fireball>();
            }
        }

        for (int r = 0; r < N; r++) {
            for (int c = 0; c < N; c++) {
            	for(int z=0;z<map[r][c].size();z++) {
            		Fireball f=map[r][c].get(z);
            		int nr = (f.r + dr[f.d] * f.s % N + N) % N;
                     int nc = (f.c + dc[f.d] * f.s % N + N) % N;
            		
            			newMap[nr][nc].add(new Fireball(nr, nc, f.m, f.s, f.d));
            		
            	
            	}
                
            }
        }

        map = newMap;
    }

    public static void DivideFireballs() {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (map[i][j].size() > 1) {
                    int sumM = 0;
                    int sumS = 0;
                    boolean even = true; 
                    boolean odd = true;
                    for (Fireball f : map[i][j]) {
                        sumM += f.m;
                        sumS += f.s;
                        if (f.d % 2 == 0) odd = false;
                        else even = false;
                    }

                    int nm = sumM / 5;
                    int ns = sumS / map[i][j].size();
                    map[i][j].clear();
                    if (nm > 0) {
                        for (int d = 0; d < 8; d += 2) {
                            if (even || odd) {
                            	map[i][j].add(new Fireball(i, j, nm, ns, d));
                            } 
                            else {
                            	map[i][j].add(new Fireball(i, j, nm, ns, d + 1));
                            }
                        }
                    }
                }
            }
        }
    }
}

'알고리즘 공부' 카테고리의 다른 글

백준 배열돌리기4  (0) 2024.03.30
백준-배열돌리기3  (1) 2024.03.25
백준 - 마법사 상어와 비바라기  (1) 2024.02.24
백준 컨베이어 밸트 위의 로봇  (1) 2024.02.17
백준 빗물  (0) 2024.02.11

+ Recent posts