judgejudge @ 2019-02-25 18:38:03
#include <iostream>
#include <cstdio>
using namespace std;
int a[10001][10001];
int f[10001][10001];
int n,m;
void read(int &a){
a=0;int d=1;char c;
while(c=getchar(),c<'0'||c>'9')if(c=='-')d=-1;a=a*10+c-48;
while(c=getchar(),c>='0'&&c<='9')a=a*10+c-48;
a*=d;
}
void write(int x){
if(x<0){
x=-x;
putchar(45);
}
if(x) write(x/10);
else return;
putchar(x%10+48);
}
inline int dp(int s1,int s2){
int i,j;
if(s1>=1&&s1<=n&&s2>=1&&s2<=m){
if(a[s1-1][s2]>a[s1][s2]){
f[s1-1][s2]=max(f[s1-1][s2],f[s1][s2]+1);
dp(s1-1,s2);
}
if(a[s1+1][s2]>a[s1][s2]){
f[s1+1][s2]=max(f[s1+1][s2],f[s1][s2]+1);
dp(s1+1,s2);
}
if(a[s1][s2-1]>a[s1][s2]){
f[s1][s2-1]=max(f[s1][s2-1],f[s1][s2]+1);
dp(s1,s2-1);
}
if(a[s1][s2+1]>a[s1][s2]){
f[s1][s2+1]=max(f[s1][s2+1],f[s1][s2]+1);
dp(s1,s2+1);
}
}
}
int main(){
int i,j,k;
int row,col;
int minl=1000000;
read(n);
read(m);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++){
read(a[i][j]);
if(a[i][j]<minl){
minl=a[i][j];
row=i;
col=j;
}
}
int maxl=0;
f[row][col]=1;
dp(row,col);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(maxl<f[i][j])maxl=f[i][j];
write(maxl);
return 0;
}
by 万万没想到 @ 2019-02-25 18:46:58
要记忆化一下
by aminoas @ 2019-02-26 17:59:31
@judgejudge
红名神犇又开始刷题了...
by judgejudge @ 2019-02-26 18:12:46
@万万没想到 怎么一个记忆化的方法??我感觉我好像有记忆化了吧。。。qwq
by judgejudge @ 2019-02-26 18:13:01
@2018J1605 我是蒟蒻。。。。
by aminoas @ 2019-02-26 18:21:44
@judgejudge
我也是蒟蒻...
by 万万没想到 @ 2019-02-26 19:21:02
@judgejudge
在递归函数开头就判断坐标是否有值有就直接返回
by judgejudge @ 2019-02-26 20:13:12
@万万没想到 返回是return还是return 0???
by judgejudge @ 2019-02-26 20:13:48
@万万没想到
#include <iostream>
#include <cstdio>
using namespace std;
int a[10001][10001];
int f[10001][10001];
int n,m;
void read(int &a){
a=0;int d=1;char c;
while(c=getchar(),c<'0'||c>'9')if(c=='-')d=-1;a=a*10+c-48;
while(c=getchar(),c>='0'&&c<='9')a=a*10+c-48;
a*=d;
}
void write(int x){
if(x<0){
x=-x;
putchar(45);
}
if(x) write(x/10);
else return;
putchar(x%10+48);
}
inline int dp(int s1,int s2){
int i,j;
if(f[s1][s2]>0)return;
if(s1>=1&&s1<=n&&s2>=1&&s2<=m){
if(a[s1-1][s2]>a[s1][s2]){
f[s1-1][s2]=max(f[s1-1][s2],f[s1][s2]+1);
dp(s1-1,s2);
}
if(a[s1+1][s2]>a[s1][s2]){
f[s1+1][s2]=max(f[s1+1][s2],f[s1][s2]+1);
dp(s1+1,s2);
}
if(a[s1][s2-1]>a[s1][s2]){
f[s1][s2-1]=max(f[s1][s2-1],f[s1][s2]+1);
dp(s1,s2-1);
}
if(a[s1][s2+1]>a[s1][s2]){
f[s1][s2+1]=max(f[s1][s2+1],f[s1][s2]+1);
dp(s1,s2+1);
}
}
}
int main(){
int i,j,k;
int row,col;
int minl=1000000;
read(n);
read(m);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++){
read(a[i][j]);
if(a[i][j]<minl){
minl=a[i][j];
row=i;
col=j;
}
}
int maxl=0;
f[row][col]=1;
dp(row,col);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(maxl<f[i][j])maxl=f[i][j];
write(maxl);
return 0;
}
by 万万没想到 @ 2019-02-26 20:35:09
@judgejudge
return f[s1][s2]
by 万万没想到 @ 2019-02-26 20:37:53
@judgejudge
dp要循环遍历时调用
如果你这样用void函数,返回直接return