알고리즘/백준 문제 풀이

[BOJ] 17143. 낚시왕

바켱서 2020. 8. 9. 23:17

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

접근 방법

그대로 구현을 해주면 된다.

상어가 움직일 때마다 구현해야 하는 부분이 쪼금 애먹였다. 더 쉬운 방법이 있을거 같았는데 해야

될 것들이 너무 많아서 빠르게 진행하였다. ( 정처기 시험... )

애먹인 부분만 보도록 하자. ( 상어가 움직일 때 배열의 크기를 벗어 날 때의 코드이다. )

                                if(nx >= R){
                    if( (nx / (R-1)) % 2 == 1 ){
                        sharks.get(i).direction = 0;
                        nx = (R-1) - (nx % (R-1));
                    }else {
                        nx = nx % (R - 1);
                    }
                }
                if(nx < 0){
                    if( (nx / (R-1)) % 2 == 0 ) {
                        sharks.get(i).direction = 1;
                        nx = -(nx%(R - 1));
                    }else {
                        nx = (R-1) + (nx % (R-1));
                    }
                }

                if(ny >= C){
                    if( ((ny / (C-1)) % 2) == 1){
                        sharks.get(i).direction = 3;
                        ny = (C-1) - (ny%(C-1));
                    }else {
                        ny = ny % (C - 1);
                    }
                }

                if( ny < 0){
                    if( (ny / (C-1)) % 2 == 0 ) {
                        sharks.get(i).direction = 2;
                        ny = -(ny % (C - 1));
                    }else {
                        ny = (C-1) + (ny % (C-1));
                    }
                }

Need Know

  1. 구현!

전체 코드 ( Java )

import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;

class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    static int R;
    static int C;
    static int M;   // 상어의 갯수
    static ArrayList<Shark> sharks;
    static int[][] map;
    static int fisher = -1;
    static int ans = 0;
    static int index = 1;
    static int[] dx = {-1,1,0,0};
    static int[] dy = {0,0,1,-1};
    public static void main(String[] args) throws IOException{
        set();
        solve();

        bw.flush();
        bw.close();
        br.close();
    }
    static void set() throws IOException {
        StringTokenizer st = new StringTokenizer(br.readLine());
        R = Integer.parseInt(st.nextToken());
        C = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        map = new int[R][C];
        sharks = new ArrayList<>();

        for(int i=0; i<M; i++){
            st = new StringTokenizer(br.readLine());
            int r = Integer.parseInt(st.nextToken())-1;
            int c = Integer.parseInt(st.nextToken())-1;
            map[r][c] = index;
            int s = Integer.parseInt(st.nextToken());
            int d = (Integer.parseInt(st.nextToken())-1);
            int z = Integer.parseInt(st.nextToken());
            sharks.add(new Shark(new Location(r,c),s,d,z,true));
            index++;
        }
    }
    static void solve() throws IOException {
        while(true){
            fisher++;

            ans += fishingShark();

            map = movingShark();

            if(fisher == C-1) break;
        }
        bw.write(ans +"");
    }

    static int[][] movingShark(){
        int[][] cloneMap = new int[R][C];
        for(int i=0; i<sharks.size(); i++){
            if(sharks.get(i).state){
                Shark shark = sharks.get(i);
                int nx = dx[shark.direction] * shark.speed + shark.loc.x;
                int ny = dy[shark.direction] * shark.speed + shark.loc.y;
                if(nx >= R){
                    if( (nx / (R-1)) % 2 == 1 ){
                        sharks.get(i).direction = 0;
                        nx = (R-1) - (nx % (R-1));
                    }else {
                        nx = nx % (R - 1);
                    }
                }
                if(nx < 0){
                    if( (nx / (R-1)) % 2 == 0 ) {
                        sharks.get(i).direction = 1;
                        nx = -(nx%(R - 1));
                    }else {
                        nx = (R-1) + (nx % (R-1));
                    }
                }

                if(ny >= C){
                    if( ((ny / (C-1)) % 2) == 1){
                        sharks.get(i).direction = 3;
                        ny = (C-1) - (ny%(C-1));
                    }else {
                        ny = ny % (C - 1);
                    }
                }

                if( ny < 0){
                    if( (ny / (C-1)) % 2 == 0 ) {
                        sharks.get(i).direction = 2;
                        ny = -(ny % (C - 1));
                    }else {
                        ny = (C-1) + (ny % (C-1));
                    }
                }

                if(cloneMap[nx][ny] != 0){
                    Shark location_shark = sharks.get(cloneMap[nx][ny]-1);
                    int thisSize = shark.size;
                    if(thisSize < location_shark.size){
                        sharks.get(i).state = false;
                    }else{
                        sharks.get(cloneMap[nx][ny]-1).state = false;
                        cloneMap[nx][ny] = i+1;
                        sharks.get(i).loc = new Location(nx,ny);
                    }
                }else{
                    cloneMap[nx][ny] = i+1;
                    sharks.get(i).loc = new Location(nx,ny);
                }
            }
        }
        return cloneMap;
    }

    static int fishingShark(){
        for(int i=0; i<R; i++){
            if(map[i][fisher] != 0){
                int fish_index = map[i][fisher] -1;
                map[i][fisher] = 0;
                sharks.get(fish_index).state = false;
                return sharks.get(fish_index).size;
            }
        }
        return 0;
    }
}
class Shark{
    /**
     * loc - 상어의 위치정보
     * speed - 스피드
     * direction -> 1 : 위 , 2: 아래 , 3: 오른쪽 , 4 : 왼쪽
     */
    Location loc;
    int speed, direction, size;
    boolean state;

    public Shark(Location loc, int speed, int direction, int size, boolean state) {
        this.loc = loc;
        this.speed = speed;
        this.direction = direction;
        this.size = size;
        this.state = state;
    }
}
class Location{
    int x,y;

    public Location(int x, int y) {
        this.x = x;
        this.y = y;
    }
}