Poj1088

来源:互联网 发布:魅力女生知乎 编辑:程序博客网 时间:2024/06/06 12:57
package poj;import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.util.Arrays;import java.util.Iterator;import java.util.Scanner;public class Main1088_1 {    static class Node implements Comparable<Node> {        int x;        int y;        int height;        @Override        public int compareTo(Node o) {            if (height > o.height)                return 1;            else if (height < o.height)                return -1;            else                return 0;        }    }    public static void main(String[] args) throws FileNotFoundException {        Scanner scanner = new Scanner(new BufferedInputStream(                new FileInputStream(new File("E:\\input.txt"))));        int r = scanner.nextInt();        int c = scanner.nextInt();        int[][] arr = new int[r][c];        int[][] res = new int[r][c];        Node[] list = new Node[r * c];        int t = 0;        for (int i = 0; i < r; i++) {            for (int j = 0; j < c; j++) {                arr[i][j] = scanner.nextInt();                list[t] = new Node();                list[t].x = i;                list[t].y = j;                list[t++].height = arr[i][j];            }        }        Arrays.sort(list);        int ans=0;        for (int i = 0; i < list.length; i++) {            int x = list[i].x;            int y = list[i].y;            int h = list[i].height;            if (x - 1 >= 0 && arr[x - 1][y] > h&&res[x-1][y]<res[x][y]+1)                res[x - 1][y]=res[x][y] + 1;// left            if (x + 1 < r && arr[x + 1][y] > h&&res[x+1][y]<res[x][y]+1)                res[x + 1][y]=res[x][y] + 1;// right            if (y - 1 >= 0 && arr[x][y - 1] > h&&res[x][y-1]<res[x][y]+1)                res[x][y - 1] =res[x][y]  + 1;// up            if (y + 1 < c && arr[x][y + 1] > h&&res[x][y+1]<res[x][y]+1)                res[x][y + 1]=res[x][y]  + 1;// down            if (ans<res[x][y])  ans=res[x][y];        }       System.out.println(ans+1);    }}

原创粉丝点击