昨晚的笔试题Skiing,动归+深搜
2016-03-23 10:27
267 查看
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #define MAX(a,b) ((a)>(b)?(a):(b)) using namespace std; int height[120][120] = { 0 };//保存输入高度值 int length[120][120] = { 0 };//计算后的长度值 int R, C; int dfs(int r, int c) { if (height[r][c] == -1) { return 0; } if(length[r][c] > 0) return length[r][c]; int p = 0; int q = 0; int m = 0; int n = 0; if (height[r - 1][c] != -1 && height[r - 1][c] < height[r][c]) { p = dfs(r - 1, c); } if (height[r + 1][c] != -1 && height[r + 1][c] < height[r][c]) { q = dfs(r + 1, c); } if (height[r ][c - 1] != -1 && height[r][c - 1] < height[r][c]) { m = dfs(r , c - 1); } if (height[r][c + 1] != -1 && height[r][c + 1] < height[r][c]) { n = dfs(r , c + 1); } length[r][c]=MAX(MAX(p,q),MAX(m,n))+1; return length[r][c]; } int ans() { int i, j; for (i = 1; i <= R; ++i) for (j = 1; j <= C; ++j) length[i][j] = 0; int maxLength = 0; for (i = 1; i <= R; ++i) for (j = 1; j <= C; ++j) { int num = dfs(i, j); if(maxLength < num) maxLength=num; } return maxLength; } int main() { int T; int i, j; memset(height,-1,sizeof(height)); //freopen("in.txt", "r", stdin); cin >> T; while (T--) { cin >> R >> C; for (i = 1; i <= R; ++i) for (j = 1; j <= C; ++j) cin >> height[i][j];//最外层为-1 cout << ans() << endl; } //fclose(stdin); //system("pause"); return 0; }
相关文章推荐
- 希尔排序--ShellSort
- iOS中数组转字符串,字符串转数组
- Linux Screen 超简单用法
- 解决VM克隆centos系统网卡问题
- JAVA Socket超时浅析
- 一道JS 面试题
- zabbix多实例监控tomcat
- 猜字母
- 包装类的自动装箱,自动拆箱
- IOS属性(@property)的修饰词assign、weak、strong、retain、copy、nonatomic、atomic、readonly、readwrite解释、以及一部分内存优化方法
- Redis快速入门
- 使用流光法实现物体跟踪
- redis 消息队列(发布订阅)、持久化(RDB、AOF)、集群(cluster)
- 笔试题:数据库 (2)
- View Controller生命周期的各个方法的调用
- JDK各个版本的新特性jdk1.5-jdk8
- Android WebView和JavaScript之间的交互
- 苹果电脑mac安装nginx教程
- 记录下过*P的内核调试(蓝屏 无法显示进程)的问题
- svn服务器LInux搭建