java实现:
public class Demo {
/**
* 计算区域最长长度
*
* Michael喜欢滑雪这并不奇怪,因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,
* 而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道在一个区域中最长底滑坡。
* 区域由一个二维数组给出。数组的每个数字代表点的高度
* 下面是一个例子
* 1 2 3 4 5
* 16 17 18 19 6
* 15 24 25 20 7
* 14 23 22 21 8
* 13 12 11 10 9
* 一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。
* 在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。
*
* 思路:1、先把各点的高度按照从小到大排序
* 2、再依次从最小的点遍历,看它周围【上下左右】有没有比它高的点
* 3、一旦有,就更新比它高的点的路径长度
* 注意:最后输出的是路径长度而不是最长路径的起始高度
* 由于开始时路径长度初始化为 0,所以最后的结果要 +1
* @param all 存储所有区域高度数据
* @return 区域最长长度
*/
public int getMaxDistance(int[][] all) {
int i = 0, j = 0, n = 0;
int ilen = all.length;
int jlen = all[0].length;
int len = jlen * jlen;
int maxDistance = 0;
int[][] dirlen = new int[ilen][jlen];
int[][] dir = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };//向右、下、左、上
Point[] points = new Point[ilen * jlen];
for (i = 0; i < ilen; i++) {
for (j = 0; j < jlen; j++) {
points[n] = new Point();
points[n].x = i;
points[n].y = j;
points[n].height = all[i][j];
//System.out.println(points[n].height + ", ");
n++;
}
}
// 按照高度从小到大排序
quickSort(points, 0, points.length - 1);
for (int k = 0; k < len; k++) {
int x = points[k].x;
int y = points[k].y;
int nx, ny;
for (int d = 0; d < dir.length; d++)
{
nx = x + dir[d][0];
ny = y + dir[d][1];
if (judge(all, nx, ny) && all[nx][ny] > all[x][y]) {
if (dirlen[nx][ny] > dirlen[x][y] + 1){
dirlen[nx][ny] = dirlen[nx][ny];
}
else{
dirlen[nx][ny] = dirlen[x][y] + 1;
}
}
}
}
for (i = 0; i < ilen; i++) {
for (j = 0; j < jlen; j++) {
if (dirlen[i][j] > maxDistance){
maxDistance = dirlen[i][j];
}
}
}
//System.out.println(maxDistance + 1);
return maxDistance + 1;
}
//判断点是否超出范围
Boolean judge(int[][] all, int x, int y) {
if (x >= 0 && x < all.length && y >= 0 && y < all[0].length){
return true;
}
else{
return false;
}
}
// 用快速排序法把所有点按照高度从小到大排序
void quickSort(Point[] points, int low, int high) {
int loc = 0;
if (low < high) {
loc = partition(points, low, high);
quickSort(points, low, loc - 1);
quickSort(points, loc + 1, high);
}
}
int partition(Point[] points, int low, int high) {
Point pivot = points[low];
while (low < high) {
while (low < high && points[high].height > pivot.height){
high--;
}
points[low] = points[high];
while (low < high && points[low].height <= pivot.height){
low++;
}
points[high] = points[low];
}
points[low] = pivot;
return low;
}
}
public class Point {
int x;
int y;
int height;
}
import junit.framework.Assert;
import junit.framework.TestCase;
public class DemoTest extends TestCase {
public void testCase01() {
Demo demo =new Demo();
int [][]all={{1,2,3,4,5},
{16,17,18,19,6},
{15,24,25,20,7},
{14,23,22,21,8},
{13,12,11,10,9}};
Assert.assertEquals(25, demo.getMaxDistance(all));
}
}
c实现:
#include
#include
#include
using namespace std;
const int maxn = 110;
int map[maxn][maxn];
int dp[maxn][maxn];
int r,c;
int dir[4][2] = {0,1, 1,0, 0,-1, -1,0};
struct Point{
int x,y;
int high;
}p[maxn*maxn];
bool cmp(Point a, Point b)
{
return a.high < b.high;
}
int judge(int x, int y)
{
if(x >= 1 && x <= r && y >= 1 && y <= c)
return 1;
else return 0;
}
void solve(int x, int y)
{
int nx, ny;
int m = 0;
for(int i = 0; i < 4; i++) //临近的点比当前点高
{
nx = x+dir[i][0];
ny = y+dir[i][1];
if( map[nx][ny] > map[x][y] && judge(nx,ny))
dp[nx][ny] = max(dp[nx][ny], dp[x][y]+1);
}
}
int main()
{
while(scanf("%d%d", &r,&c) != EOF)
{
memset(map, 0, sizeof(map));
memset(dp, 0, sizeof(dp));
int n = 0; //点的个数
for(int i = 1; i <= r; i++)
{
for(int j = 1; j <= c; j++)
{
scanf("%d", &map[i][j]);
p[n].x = i;
p[n].y = j;
p[n].high = map[i][j];
n++;
}
}
sort(p, p+n, cmp); //按照高度从小到大排序
for(int i = 0; i < n; i++)
{
int x = p[i].x;
int y = p[i].y;
solve(x,y);
}
/*
for(int i = 1; i <= r; i++)
{
for(int j = 1; j <= c; j++)
printf("%d ", dp[i][j]); printf("\n");
}
*/
int len = 0;
for(int i = 1; i <= r; i++)
{
for(int j = 1; j <= c; j++)
{
if(dp[i][j] > len)
{
len = dp[i][j];
}
}
}
printf("%d\n", len+1);
}
return 0;
}