标签:
虽然是很老很简单的题目了,但是再一次做起来体会不同了.....
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 79919 | Accepted: 29747 |
Description
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
Input
Output
Sample Input
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
Sample Output
25
题解:当初一直不能理解为什么能够又是dfs又是dp,现在终于理解了,因为这是一个DAG。
代码:
1 #include <cstdio> 2 #include <cmath> 3 #include <cstring> 4 #include <ctime> 5 #include <iostream> 6 #include <algorithm> 7 #include <set> 8 #include <vector> 9 #include <sstream> 10 #include <queue> 11 #include <typeinfo> 12 #include <fstream> 13 #include <map> 14 #include <stack> 15 typedef long long ll; 16 using namespace std; 17 #define test freopen("1.txt","r",stdin) 18 #define maxn 2000001 19 #define mod 10007 20 #define eps 1e-9 21 const int inf=0x3f3f3f3f; 22 const ll infll = 0x3f3f3f3f3f3f3f3fLL; 23 inline int read() 24 { 25 ll x=0,f=1;char ch=getchar(); 26 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 27 while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 28 return x*f; 29 } 30 inline void out(int x) { 31 if(x>9) out(x/10); 32 putchar(x%10+‘0‘); 33 } 34 //************************************************************************************** 35 int n,m; 36 int a[105][105]; 37 int dp[105][105]; 38 int dy[4]={-1,1,0,0}; 39 int dx[4]={0,0,-1,1}; 40 int cal[105][105]; 41 void init() 42 { 43 n=read();m=read(); 44 for(int i=1;i<=n;i++) 45 for(int j=1;j<=m;j++) 46 {a[i][j]=read();dp[i][j]=1;} 47 } 48 bool check(int y,int x) 49 { 50 if(y<1||y>n||x<1||x>m) 51 return 0; 52 return 1; 53 } 54 int dfs(int y,int x) 55 { 56 if(cal[y][x]) return dp[y][x]; 57 int maxl=dp[y][x]; 58 for(int i=0;i<4;i++) 59 { 60 int newx=x+dx[i];int newy=y+dy[i]; 61 if(check(newy,newx)&&a[newy][newx]<a[y][x]) 62 { 63 maxl=max(maxl,dfs(newy,newx)+1); 64 } 65 } 66 cal[y][x]=1; 67 return dp[y][x]=maxl; 68 } 69 int main() 70 { 71 init(); 72 int ans=-1; 73 for(int y=1;y<=n;y++) 74 for(int x=1;x<=m;x++) 75 { 76 ans=max(ans,dfs(y,x)); 77 } 78 printf("%d\n",ans); 79 return 0; 80 }
标签:
原文地址:http://www.cnblogs.com/diang/p/4747611.html