踩盾滑行是林克的最愛,作爲滑行愛好者,只有高度差的斜坡,無論是草地、雪地、沙地還是空氣,林克都可以踩盾滑行。
給定一個有高度差的區域(用二維數組表示)數組的每個數字代表點的高度(如下圖),如何求出最長的滑行路徑呢?
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
提示:林克可以從某個點滑向上下左右相鄰四個點之一,當且僅當高度減小。
在上面的例子中,最長的滑行路徑爲25->24->23->22->21->20…->5->4->3->2->1(你看出來了嗎?)
輸入
輸入的第一行表示區域的行數R和列數C(1 <= R,C <= 100)。下面是R行,每行有C個整數,代表高度h,0<=h<=10000。
輸出
輸出最長區域的長度。
輸入樣例 1
5 5
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
輸出樣例 1
25
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#define For(a,begin,end) for(register int a=begin;a<=end;a++)
using namespace std;
struct Mount
{
int x;
int y;
int value;
} mount[1000011];
struct Mount *p=mount;
int high[110][110]={0};
int d[110][110];
int n=0,r,c;
void dp()//dp用於求出此處的最大滑行距離
{
for(int i=0;i<n;p++,i++)//n= r*c;
{ //邊界處理!
int x=p->x,y=p->y;// 原來的條件 p<=p+n+1;這是必然死循環的!!前後都在一直變化啊!!!
//一個山的d = 四周比他低的山的d +1 與 其本身初始值 二者中大的那個
if(x>1)
{
if(high[x-1][y] < high[x][y])// up
d[x][y]=max(d[x][y] , d[x-1][y]+1);
}
if(x<r)
{
if(high[x+1][y] < high[x][y])//down
d[x][y]=max(d[x][y] , d[x+1][y]+1);
}
if(y>1)
{
if(high[x][y-1] < high[x][y])// left
d[x][y]=max(d[x][y] , d[x][y-1]+1);
}
if(y<c)
{
if(high[x][y+1] < high[x][y])// right
d[x][y]=max(d[x][y] , d[x][y+1]+1);
}
}
}
int partition1(struct Mount a[],int i,int j)
{
struct Mount base=a[i];
while(i<j)
{
while(i<j&&a[j].value>=base.value)
j--;
a[i]=a[j];
while (i<j&&a[i].value<=base.value)
i++;
a[j]=a[i];
}
a[i]=base;
return i;
}
void quickSort(struct Mount a[],int low,int high)
{
if(low<high)
{
int m=partition1(a,low,high);//返回一個基準的位置
quickSort(a,low,m-1);
quickSort(a,m+1,high);
}
}
int main()
{
cin>>r>>c;//row & column
// memset(d,1,sizeof(d));//d 初始化爲 1
//初始化出錯,memset 按照字節 賦值 只能爲 0 或 -1
For(i,1,r)//for(int i=1;i<=r;i++)
For(j,1,c)//for(int j=1;j<=c;j++)
d[i][j]=1;
For(i,1,r)//for(int i=1;i<=r;i++)
For(j,1,c)//for(int j=1;j<=c;j++)
{
cin>>high[i][j];
mount[n].x=i;
mount[n].y=j;
mount[n].value=high[i][j];
n++;
}
/*****排序******/
//按高度的從小到大排序
//一個mount的 d 只與比它低的山有關
quickSort(mount,0,n-1);//n -> r*c+1
p=mount;
dp();
int max1=1;
For(i,1,r)//for(int i=1;i<=r;i++)
For(j,1,c)//for(int j=1;j<=c;j++)
max1= max(d[i][j],max1);
cout<<max1;
return 0;
}
錯誤 ↓
for(;p<=p+n+1;p++)
memset(d,1,sizeof(d));