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<stdio.h>
#include<string.h>
#include<algorithm>
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;
}