码迷,mamicode.com
首页 > 其他好文 > 详细

L - Phalanx

时间:2019-02-06 11:53:54      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:include   str   mes   stream   while   ppt   fst   algo   mmm   

最近一直在写dp,然后别的就啥也不管了(wtcl),很明显的最简单的搜索题竟然卡了,一开始的思路是每一个格子都只能是从四周的格子转化过来的,只要找到四周最大的那个那么dp[i][j]=max+a[i][j],但是无法确定四周的状态,不知道i,j该怎么开始,所以就卡了,竟然不往搜索上去想,emmmm,希望记住一下,看cls的ppt,说刷1000到poj的傻逼题还是个傻逼,但是现在连傻逼题都写不出来。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <string>
 4 #include <map>
 5 #include <set>
 6 #include <algorithm>
 7 #include <fstream>
 8 #include <cstdio>
 9 #include <cmath>
10 #include <stack>
11 #include <queue>
12 using namespace std;
13 typedef long long ll;
14 int n,k;
15 int dp[1000+5][1000+5];
16 int a[1000+5][1000+5];
17 int d[][2]={{0,1},{0,-1},{1,0},{-1,0}};
18 int dfs(int x,int y)
19 {
20     int maxn=0;
21     if(dp[x][y]!=0) return dp[x][y];
22     for(int i=0;i<=3;i++)
23         {
24             for(int j=1;j<=k;j++)
25             {
26                 int dx=x+d[i][0]*j;
27                 int dy=y+d[i][1]*j;
28                 if(dx>=1&&dx<=n&&dx>=1&&dy<=n&&a[x][y]<a[dx][dy])
29                 {
30                     int t=dfs(dx,dy);
31                     if(t>maxn)
32                         maxn=t;
33                 }    
34             }    
35         } 
36     dp[x][y]=maxn+a[x][y];
37     return dp[x][y];         
38 }
39 
40 
41 int main()
42 {
43     while(cin>>n>>k)
44     {
45         if(n==-1&&k==-1) break;
46         for(int i=1;i<=n;i++)
47         {
48             for(int j=1;j<=n;j++)
49                 cin>>a[i][j];
50         }
51         memset(dp,0,sizeof(dp));
52         cout <<dfs(1,1)<<endl;        
53     }
54     return 0;
55 }

 

L - Phalanx

标签:include   str   mes   stream   while   ppt   fst   algo   mmm   

原文地址:https://www.cnblogs.com/Msmw/p/10353565.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!