본문 바로가기
Algorithm

완전탐색_2019 삼성 낚시왕

by forkballpitch 2021. 11. 8.
728x90
728x90

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

 

17143번: 낚시왕

낚시왕이 상어 낚시를 하는 곳은 크기가 R×C인 격자판으로 나타낼 수 있다. 격자판의 각 칸은 (r, c)로 나타낼 수 있다. r은 행, c는 열이고, (R, C)는 아래 그림에서 가장 오른쪽 아래에 있는 칸이다.

www.acmicpc.net

출처: https://www.youtube.com/watch?v=5JPJcoQTP1U&t=934s 

import java.util.Scanner;

public class Main {
    static class Shark{
        //속도,방향,무게
        Shark(int s, int d, int z){
            this.s = s;
            this.d = d;
            this.z = z;
        }
        int s,d,z;
    }
// 행,열,상어의 개수
    static int R, C, M;
    static Shark Arr[][] = new Shark[100][100];
    static int[] Dr = {-1,1,0,0};
    static int[] Dc = {0,0,1,-1};

    static int solve(){
        int sum = 0;
        Shark backup[][] = new Shark[100][100];
        for(int t =0; t<C; ++t){
            for(int i = 0; i < R; ++i){
                if(Arr[i][t] != null){
                    sum += Arr[i][t].z;
                    Arr[i][t] = null;
                    break;
                }
            }

            for(int i = 0; i< R; ++i){
                for(int j = 0; j<C; ++j){
                    backup[i][j] = Arr[i][j];
                    Arr[i][j] = null;
                }
            }

            for(int i = 0; i < R; ++i){
                for(int j = 0; j < C; ++j){
                    Shark curr = backup[i][j];
                    if(curr != null){
                        int nr = i + curr.s * Dr[curr.d];
                        int nc = j + curr.s * Dc[curr.d];
                        //음수가 되는 경우 부호 변경
                        if(nr < 0){
                            nr = -nr;
                            curr.d = 1;
                        }
                        //부호를 변경했는데 행-1 의 개수보다 클때 처리
                        if(nr > R - 1){
                            int a = nr / (R-1);
                            int b = nr % (R-1);
                            //짝수행 홀수 다르게 처리
                            if(a % 2 == 0){
                                nr = b;
                            }else {
                                nr = R - 1 - b;
                                curr.d = 0;

                            }
                        }
                        //열 처리도 동일
                        if(nc < 0){
                            nc = -nc;
                            curr.d = 2;
                        }
                        if(nc > C - 1){
                            int a = nc / (C-1);
                            int b = nc % (C-1);
                            //짝수열 홀수열 다르게 처리
                            if(a % 2 == 0){
                                nc = b;
                            }else{
                                nc = C - 1 - b;
                                curr.d = 3;
                            }
                        }

                        if(Arr[nr][nc] == null || (Arr[nr][nc] !=null && Arr[nr][nc].z < curr.z ))
                            Arr[nr][nc] = curr;
                    }

                }
            }
        }

        return sum;

    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        R = sc.nextInt();
        C = sc.nextInt();
        M = sc.nextInt();

        for(int i =0; i < M; ++i){
            int r = sc.nextInt() - 1;
            int c = sc.nextInt() - 1;
            int s = sc.nextInt();
            int d = sc.nextInt() - 1;
            int z = sc.nextInt();

            Arr[r][c] = new Shark(s,d,z);

        }

        System.out.println(solve());
    }
}
728x90
728x90