1 |
|
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
25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 |
#include<iostream> #include<cstring> #include<cstdio> #include <memory> #include<queue> #include<cmath> #include<set> #include<algorithm> using
namespace std; int
map[101][101]; int
visit[101][101]; int
r,c; int
f[4][2]={-1,0,0,1,1,0,0,-1}; int
dfs( int
x, int
y) { int
i,s,k,count; if (visit[x][y]>1) return
visit[x][y]; s=map[x][y]; for (i=0;i<4;i++) if (x+f[i][0]>=1&&x+f[i][0]<=r&&y+f[i][1]>=1&&y+f[i][1]<=c) { k=map[x+f[i][0]][y+f[i][1]]; if (k<s) { count=dfs(x+f[i][0],y+f[i][1]); if (visit[x][y]<count+1) visit[x][y]=count+1; } } return
visit[x][y]; } int
main( int
argc, char * argv[]) { int
m; cin>>m; while (m--) { int
i,j,max; max=-1; cin>>r>>c; memset (map,0, sizeof (map)); for (i=1;i<=r;i++) for (j=1;j<=c;j++) {cin>>map[i][j]; visit[i][j]=1;} for (i=1;i<=r;i++) for (j=1;j<=c;j++) { dfs(i,j); if (max<visit[i][j]) max=visit[i][j]; } cout<<max<<endl; } return
0; } |
原文:http://www.cnblogs.com/52Cyan/p/3703349.html