poj 1088 滑雪 【记忆化搜索】+【DFS】
来源:互联网 发布:百世汇通软件 编辑:程序博客网 时间:2024/06/06 06:56
策略:如题
题目链接:http://poj.org/problem?id=1088
代码:
#include<stdio.h>#include<string.h>int map[105][105], dp[105][105], n, m;const int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};int limit(int x, int y){if(x<1||x>n||y<1||y>m) return 0;return 1;}int dfs(int x, int y){if(dp[x][y]) return dp[x][y];int pre, flag, sum, i;pre = sum = flag = 0;for( i =0; i < 4; i ++){int nx = x+dir[i][0];int ny = y+dir[i][1];if( limit(nx, ny)&&map[nx][ny] < map[x][y]){pre = dfs(nx, ny);sum = sum>pre?sum:pre;flag = 1;}}if(flag) dp[x][y] =sum+1;else dp[x][y] = 1;return dp[x][y];}int main(){int i, j;while(scanf("%d%d", &n, &m) == 2){for(i = 1; i <= n; i ++)for(j = 1; j<= m; j ++)scanf("%d", &map[i][j]);int max = 0;memset(dp, 0, sizeof(dp));for(i = 1; i <= n; i ++)for(j = 1; j <= m; j ++){dp[i][j] = dfs(i, j);//printf("%d..", dp[i][j]);if(max < dp[i][j])max = dp[i][j];}printf("%d\n", max);}return 0;}
#include<stdio.h>#include<string.h>int map[105][105], dp[105][105], n, m;const int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; //四个方向int limit(int x, int y) //判断是不是越界了{if(x<1||x>n||y<1||y>m) return 0;return 1;}int dfs(int x, int y) 查找{if(dp[x][y]) return dp[x][y]; //如果以前找找过了,就不用再找了, 记忆化搜索int pre, flag, sum, i; //flag 是标记周围有没有比他小的高度, pre是储存周围某一方向的长度, sum是选择四个方向最大的长度pre = sum = flag = 0;for( i =0; i < 4; i ++){int nx = x+dir[i][0];int ny = y+dir[i][1];if( limit(nx, ny)&&map[nx][ny] < map[x][y]){pre = dfs(nx, ny);sum = sum>pre?sum:pre;flag = 1;}}if(flag) dp[x][y] =sum+1; else dp[x][y] = 1;<strong></strong><pre name="code" class="cpp" style="display: inline !important; "><strong style="font-family: Arial, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255); "></strong><pre name="code" class="cpp" style="display: inline !important; "><strong style="font-family: Arial, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255); "></strong><pre name="code" class="cpp" style="display: inline !important; ">//如果flag == 0,就意味着周围没有比他低的高度, 就将该处赋值为1
return dp[x][y];}int main(){int i, j;while(scanf("%d%d", &n, &m) == 2){for(i = 1; i <= n; i ++)for(j = 1; j<= m; j ++)scanf("%d", &map[i][j]);int max = 0;memset(dp, 0, sizeof(dp)); //初始化for(i = 1; i <= n; i ++)for(j = 1; j <= m; j ++){dp[i][j] = dfs(i, j);// printf("%d..", dp[i][j]);if(max < dp[i][j]) //找最大的max = dp[i][j];}printf("%d\n", max);}return 0;}
0 0
- POJ 1088 滑雪 DFS 记忆化搜索
- POJ 1088 滑雪 DFS 记忆化搜索
- poj 1088 滑雪 【记忆化搜索】+【DFS】
- POJ 1088 滑雪(记忆化搜索+DFS)
- poj 1088 滑雪(dfs记忆化搜索)
- poj 1088 滑雪(DFS +DP 记忆化搜索)
- [ACM] poj 1088 滑雪 (记忆化搜索DFS)
- AOJ 455 POJ 1088 滑雪 dfs + 记忆化搜索
- poj 1088 滑雪 DP(dfs的记忆化搜索)
- poj 1088 滑雪(DFS+记忆化搜索)
- poj 1088 滑雪(DP+记忆化dfs)
- POJ 1088--滑雪【DFS(记忆化)】
- POJ 1088 滑雪(DFS+记忆化)
- POJ 1088 滑雪(记忆化dfs)
- POJ 1088 滑雪 记忆化搜索
- poj 1088 滑雪[zz] 记忆化搜索
- poj 1088 滑雪 记忆化搜索
- POJ 1088 滑雪(DP+记忆化搜索)
- git学习 - 忽略版本控制文件
- 登陆界面 LinearLayout的属性的用法
- 我之见--java进程,线程
- VS自定义代码折叠的位置
- 中期报告:多平台网络探测器
- poj 1088 滑雪 【记忆化搜索】+【DFS】
- malloc/free 的使用要点 以及 new/delete 的使用要点
- XE6 安卓电量检测
- uva 705 Slash Maze
- poj3692二分图最大团
- 1037. Magic Coupon (25)
- Android之旅十五 android中的网络操作
- Codeforce 453 C. Little Pony and Summer Sun Celebration
- 子查询里有rownum函数时