곽로그

경사로 temp 본문

카테고리 없음

경사로 temp

일도이동 2021. 2. 8. 17:08
반응형
package feburary.second;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

/*
    길을 지나갈 수 있다 : 모든 칸의 높이가 같다 || 경사로를 놓아서 지나갈 수 있다
 */
public class SlopeDemo {
    static int N;
    static int L;
    static int[][] mapRow;
    static int[][] mapColumn;
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st;

        st  = new StringTokenizer(br.readLine()," ");
        N = Integer.parseInt(st.nextToken());
        L = Integer.parseInt(st.nextToken());

        mapRow = new int[N][N];
        mapColumn = new int[N][N];
        for(int r= 0; r<N; r++){
            st = new StringTokenizer(br.readLine()," ");
            for(int c=0; c<N; c++){
                int value = Integer.parseInt(st.nextToken());
                mapRow[r][c] = value;
                mapColumn[c][r] = value;

            }
        }
        int result = findNumberOfPath(mapRow);
        result += findNumberOfPath(mapColumn);

        bw.write(String.valueOf(result));
        bw.flush();



    }

    static int findNumberOfPath(int[][] map){
        int numberOfPath = 0;
        // 행기준
        for(int r =0; r<N; r++){
            int baseSlope = map[r][0];
            int slopeCount = 1;
            boolean isPath = true;
            int currentPoint = 1;

            while(currentPoint<N){
                int currentSlope = map[r][currentPoint];
                if(baseSlope<currentSlope){
                    if(currentSlope-baseSlope==1 && L<=slopeCount){
                        baseSlope = currentSlope;
                        slopeCount = 1;
                        ++currentPoint;
                    }
                    else{
                        isPath =false;
                        break;
                    }
                }
                else if(baseSlope==currentSlope){
                    ++slopeCount;
                    ++currentPoint;
                }
                else{
                    if(baseSlope-currentSlope!=1){
                        isPath = false;
                        break;
                    }
                    else{
                        int lowerSlopeCount =1;
                        int lowerSlope = currentSlope;
                        int lowerPoint = currentPoint+1;

                        while (lowerPoint<N){
                            if(map[r][lowerPoint] == lowerSlope){
                                ++lowerSlopeCount;
                                ++lowerPoint;
                            }
                            else{
                                if(lowerSlopeCount>=L){
                                    currentPoint = lowerPoint+1;
                                    baseSlope = map[r][lowerPoint];
                                    slopeCount = 1;
                                    break;
                                }
                                else{
                                    isPath = false;
                                    break;
                                }
                            }
                        }
                    }
                }
                if(!isPath) break;
            }
            if(isPath) ++numberOfPath;
            System.out.println("row:"+r+"isPath?"+isPath);
        }
        return numberOfPath;
    }

}
반응형
Comments