hdu1078基础dp
2015-09-06 14:54
309 查看
/***************************************** Author :Crazy_AC(JamesQi) Time :2015 File Name : 思路:dfs + dp,,,因为只能走直线,和普通dfs一样的方向dx[i]与dy[i],由于一次最多跳k步长元,所以有nx = dx[i] * jamp + x,ny = dy[i] * jamp + y; 状态转移方程:dp[x][y] = gg[x][y] + MAX; MAX = get_Max(MAX,ans); ans = dfs(...); *****************************************/ // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream> #include <algorithm> #include <iomanip> #include <sstream> #include <string> #include <stack> #include <queue> #include <deque> #include <vector> #include <map> #include <set> #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #include <limits.h> using namespace std; #define MEM(a,b) memset(a,b,sizeof a) #define pk push_back template<class T> inline T Get_Max(const T&a,const T&b){return a < b?b:a;} template<class T> inline T Get_Min(const T&a,const T&b){return a < b?a:b;} typedef long long ll; typedef pair<int,int> ii; const int inf = 1 << 30; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; int gg[110][110]; int dp[110][110]; int dx[] = {-1,0,1,0}; int dy[] = {0,-1,0,1}; int n,k; bool check(int x,int y){ return x >= 0 && x < n && y >= 0 && y < n; } int dfs(int x,int y){ if (dp[x][y] > 0) return dp[x][y]; int MAX = 0; for (int i = 0;i < 4;i++){ for (int jamp = 1;jamp <= k;++jamp){ int nx = x + dx[i] * jamp; int ny = y + dy[i] * jamp; if (check(nx,ny) && gg[nx][ny] > gg[x][y]){ int ans = dfs(nx,ny); if (MAX < ans){ MAX = ans; } } } } return dp[x][y] = MAX + gg[x][y]; } int main() { // ios::sync_with_stdio(false); // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); while(scanf("%d%d",&n,&k) && k != -1){ for (int i = 0;i < n;++i) for (int j = 0;j < n;++j){ scanf("%d",&gg[i][j]); dp[i][j] = 0; } dfs(0,0); cout << dp[0][0] << endl; } return 0; }
相关文章推荐
- Makefile中的常用函数
- 苹果appstore审核拒绝理由列举
- MySQL常用的命令大全
- (转)IOS笔记 #pragma mark的用法
- php——<<<start运算符
- c#版 mqtt 3.1.1 client 实现
- 各种主流色彩模式及其概念
- nodeJS删除文件
- noip 2012 国王游戏
- 签名signature算法没错导致invalid signature和permission denied原因
- 为什么ConcurrentHashMap是弱一致的
- 达赖与班禅的关系
- css
- MySQL+Altas 读写分离测试(Altas 不能用存储过程,Update和Delete必须要有参数)
- asp上传是提示0104 80004005的解决方法
- 【Android控件布局】Layout 水平平分空间、垂直平分空间
- php里日期的部分显示
- 一个程序员的随感-第一次离职
- NYOJ+Map的使用利用数组下标访问
- Assembly Intro - Call Convention