博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj1088
阅读量:6981 次
发布时间:2019-06-27

本文共 1491 字,大约阅读时间需要 4 分钟。

题意:有一个矩阵,要求在矩阵上找到一个四方向行走的路线,使得每走一步走到的格子中的数字都比上一步的要小,问路线最长有多长。

分析:先将所有格子按数字大小排序。然后从最大的开始,每次看其四周是否有能走的格子,并更新四周格子的路线长度(每个格子中有一个变量记录以该格子为终点的最长路线长度)。

当然,除了上述方法,还可以用记忆化搜索

#include 
#include
using namespace std;const int maxr = 102;struct cpoint{ int x, y, height;}f[maxr * maxr];int r, c, total, map[maxr][maxr], pos[maxr][maxr], dir[4][2] = {
0, 1, 1, 0, -1, 0, 0, -1};bool operator< (cpoint a, cpoint b){ return a.height > b.height;}void init(){ int i, j; scanf("%d%d", &r, &c); total = 0; for (i = 1; i <= r; i++) for (j = 1; j <= c; j++) { scanf("%d", &f[total].height); map[i][j] = f[total].height; pos[i][j] = 1; f[total].x = i; f[total].y = j; total++; }}void work(){ int i, j, ans = 1; for (i = 0; i < total; i++) for (j = 0; j < 4; j++) if (map[f[i].x][f[i].y] < map[f[i].x + dir[j][0]][f[i].y + dir[j][1]] && pos[f[i].x][f[i].y] < pos[f[i].x + dir[j][0]][f[i].y + dir[j][1]] + 1) { pos[f[i].x][f[i].y] = pos[f[i].x + dir[j][0]][f[i].y + dir[j][1]] + 1; if (pos[f[i].x][f[i].y] > ans) ans = pos[f[i].x][f[i].y]; } printf("%d\n", ans);}int main(){ //freopen("t.txt", "r", stdin); init(); sort(f, f + r * c); work(); return 0;}
View Code

 

转载于:https://www.cnblogs.com/rainydays/p/3199975.html

你可能感兴趣的文章
在Centos 7下编译openwrt+njit-client
查看>>
微软重新释出MS10-015 解决蓝屏问题
查看>>
计算机组成原理-第3章-3.1
查看>>
约瑟夫环 猴子选大王
查看>>
win7安装mysql-8.0.13-winx64
查看>>
jQuery事件
查看>>
golang笔记——struct
查看>>
汇编与反汇编
查看>>
状态和面向对象编程——1.定位步骤
查看>>
一点代码
查看>>
mysql> select * f
查看>>
树莓派搭建WEB服务器
查看>>
MySQL学习----各种字符的长度总结
查看>>
更加安全的存取账户密码
查看>>
PHP函数之无极分类
查看>>
ReactiveCocoa代码实践之-更多思考
查看>>
普通帧,关键帧,空白关键帧的区别
查看>>
系统利益相关者描述案例
查看>>
Pandas Cheat Sheet
查看>>
NOIP模板
查看>>