백준공부/java

[백준] 1937번 욕심쟁이 판다 (gold 3

gomduri43 2024. 3. 30. 21:26

import com.sun.security.jgss.GSSUtil;

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

class Point{
    int x;
    int y;
    public Point(int x, int y){
        this.x=x;
        this.y=y;
    }
}
public class Main{
    static int[] numx={-1,1,0,0};
    static int[] numy={0,0,-1,1};
    public static void main(String[] args) throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
        int n=Integer.valueOf(br.readLine());
        int[][] map=new int[n+1][n+1];
        //차피 작은칸에서 높은 칸 순으로 가므로, 결국 작은칸부터 시작해서 가면됨.
        PriorityQueue<Point> que =new PriorityQueue<>(new Comparator<Point>(){
            public int compare(Point o1, Point o2){
                return map[o1.y][o1.x]-map[o2.y][o2.x];
            }
        });

        StringTokenizer st;

        for(int i=1; i<=n; i++){
            st=new StringTokenizer(br.readLine());
            for(int j=1; j<=n; j++){
                map[i][j]=Integer.valueOf(st.nextToken());
                que.offer(new Point(j,i));
            }
        }

        //dp가 0이면 출발지점이므로 1로 초기화,
        //사방에서 자기보다 높은 수이면 갈 수 있으므로,
        //현재 그 땅의 경로 수랑 비교해서 높은 수로 초기화,
        //해당 temp.v에서 새땅으로 진입하므로 +1한 값이랑,
        //현재 그땅의 경로수랑 비교하기.
        int answer=1;
        int[][] dp=new int[n+1][n+1];
        while(!que.isEmpty()){
            Point temp=que.poll();
            if(dp[temp.y][temp.x]==0){
                dp[temp.y][temp.x]=1;
            }
            else{
                answer= answer < dp[temp.y][temp.x] ? dp[temp.y][temp.x] : answer;
            }

            for(int i=0; i<4; i++){
                int tx=numx[i]+temp.x;
                int ty=numy[i]+temp.y;
                if(tx<1 || tx>n || ty<1 || ty>n || map[temp.y][temp.x]>=map[ty][tx]){
                    continue;
                }
                dp[ty][tx]=Math.max(dp[ty][tx], dp[temp.y][temp.x]+1);
            }
        }

        bw.write(answer+"");
        bw.flush();

    }
}