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更長。事實上,這是最長的一條。
輸入的第一行表示區域的行數R和列數C(1 <= R,C <= 100)。下面是R行,每行有C個整數,表明高度h,0<=h<=10000。
#include
<
algorithm
>
#include
<
cstdio
>
#include
<
cmath
>
#include
<
cstring
>
#include
<
iostream
>
#include
<
cstdlib
>
#include
<
set
>
#include
<
vector
>
#include
<
cctype
>
#include
<
iomanip
>
#include
<
sstream
>
#include
<
climits
>
#include
<
queue
>
#include
<
stack
>
using
namespace
std;
typedef
long
long ll
;
#define
INF
0X3f3f3f3f
const ll MAXN
=
1e3
+
7
;
const ll MOD
=
1e9
+
7
;
int r
, c
;
int mp
[MAXN
][MAXN
];
int dp
[MAXN
][MAXN
];
int dir
[
4
][
2
]
=
{
0
,
1
,
0
,
-
1
,
1
,
0
,
-
1
,
0
};
/* 好比在這個題目中咱們的dfs函數是一個倒着遞歸的過程,
每次的遞歸都能將前面的結果記錄下來,這就是一個記憶化的過程 */
bool
check
(
int x
,
int y
)
{
return
(x
>=
1
&& x
<= r
&& y
>=
1
&& y
<= c
);
}
int
dfs
(
int x
,
int y
)
{
if
(dp
[x
][y
])
//記住了就不用再次dfs搜索了
return dp
[x
][y
];
for
(
int i
=
0
; i
<
4
; i
++
)
{
int tx
= x
+ dir
[i
][
0
];
int ty
= y
+ dir
[i
][
1
];
if
(
check
(
tx
,
ty
)
&& mp
[tx
][ty
]
< mp
[x
][y
])
dp
[x
][y
]
=
max
(
dfs
(
tx
,
ty
)
+
1
,
dp
[
x
][
y
]);
}
return dp
[x
][y
];
}
int
main
()
{
ios::sync_with_stdio
(
false
);
memset
(dp
,
0
,
sizeof
(dp
));
cin
>> r
>> c
;
for
(
int i
=
1
; i
<= r
; i
++
)
for
(
int j
=
1
; j
<= c
; j
++
)
cin
>> mp
[i
][j
];
int ans
=
-
1
;
for
(
int i
=
1
; i
<= r
; i
++
)
for
(
int j
=
1
; j
<= c
; j
++
)
ans
=
max
(
ans
,
dfs
(
i
,
j
));
cout
<< ans
+
1
<< endl
;
// system("pause");
return
0
;
}