xiaobai12138 @ 2019-02-26 15:02:36
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
private class Gu implements Comparable<Gu>{
int gu;
int x;
int y;
public Gu(int gu, int x, int y) {
super();
this.gu = gu;
this.x = x;
this.y = y;
}
public int getGu() {
return gu;
}
public void setGu(int gu) {
this.gu = gu;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Gu() {
// TODO 自动生成的构造函数存根
}
@Override
public int compareTo(Gu o) {
//高度高的坐标优先级高
return this.gu-o.gu;
}
}
static int jl[][];
static int a[][];
static int dx[]={-1,0,1,0};//四个方向
static int dy[]={0,1,0,-1};
static int r;
static int c;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
r=sc.nextInt();
c=sc.nextInt();
a=new int[r][c];
jl=a;
ArrayList<Gu> arr=new ArrayList<>();
for (int i = 0; i < r; i++) {
for (int j = 0; j <c; j++) {
a[i][j]=sc.nextInt();
arr.add(new Main().new Gu(a[i][j], i, j));
}
}
//按高度从小到大排序
Collections.sort(arr);
for (int i = 0; i < arr.size(); i++) {
//如果已经计算出是可以达到多少就不用bfs了
if (jl[arr.get(i).x][arr.get(i).y]!=0) {
continue;
}else {
bfs(arr.get(i).x,arr.get(i).y);
}
}
//找到最后最大值输出
int max=0;
for (int i = 0; i < jl.length; i++) {
for (int j = 0; j <jl[i].length; j++) {
if (jl[i][j]>max) {
max=jl[i][j];
}
}
}
System.out.println(max);
}
private static void bfs(int x, int y) {
Queue<int []> que=new LinkedList<>();
que.add(new int[]{x,y});
jl[x][y]=1;
while (!que.isEmpty()) {
int z[]=que.poll();
int min[]={Integer.MAX_VALUE,-1,-1};
for (int i = 0; i < dx.length; i++) {
int xd=z[0]+dx[i];
int yd=z[1]+dy[i];
//找到合法并且并比原高度大的最小高度添加到队列
if (xd>=r||xd<0||yd>=c||yd<0||a[xd][yd]<a[z[0]][z[1]]||a[xd][yd]>min[0]) {
continue;
}
min[0]=a[xd][yd];
min[1]=xd;
min[2]=yd;
}if (min[0]!=Integer.MAX_VALUE) {
que.add(new int[]{min[1],min[2]});
jl[min[1]][min[2]]=jl[z[0]][z[1]]+1;
}
}
}
}
by xht @ 2019-02-26 15:16:30
C++多好啊
by 142857cs @ 2019-02-26 15:19:50
C++多好啊++
by xiaobai12138 @ 2019-02-26 15:36:38
不会c++啊QAQ