总时间限制:
1000ms
内存限制:
65536kB
描述
Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长的滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-…-3-2-1更长。事实上,这是最长的一条。
输入
输入的第一行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。
输出
输出最长区域的长度。
样例输入
5 5
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
样例输出
25
思路:用一个结构体存储这里面一个点的所有的属性:行号,列号,初始化高度,这个点能求到的最大的距离。对这里面的点进行排序,然后从最小的点开始优化,比较这个点周围的点的高度,如果可以走,就比较这个点原来的最大距离和向周围的点走了之后的距离(周围点+1),用来判断走不走,然后更新这个点的最大距离。
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct Point{
int col;
int row;
int high;
int max_high;
Point(int _row, int _col, int _high):row(_row),col(_col),high(_high),max_high(1){};
bool operator < (const Point &p) const{
//小到大
return high<p.high;
}
bool operator == (const Point x) const{
return x.col == col && x.row == row;
}
};
int main()
{
vector<Point> p_v;
int n, m, h;//n行,m列,临时高度:h
cin>>n>>m;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin>>h;
p_v.push_back(Point(i, j, h));
}
}
// show(p_v, n, m);
//排序,从小到大
sort(p_v.begin(), p_v.end());
//查找
// Point x = Point(1,2,3);
// vector<Point>::iterator it = find(p_v.begin(), p_v.end(), x);
for (int i = 0; i < n*m; ++i) {
Point *x = &p_v[i];
//和四个方向进行比较,并且更新max_high值
//上边
if(x->row-1>=0){
vector<Point>::iterator it = find(p_v.begin(), p_v.end(), Point(x->row-1, x->col, 1));
if((*it).high<x->high)
x->max_high = max(x->max_high,(*it).max_high+1);
}
//下边
if(x->row+1<n){
vector<Point>::iterator it = find(p_v.begin(), p_v.end(), Point(x->row+1, x->col, 1));
if((*it).high<x->high)
x->max_high = max(x->max_high, (*it).max_high+1);
}
if(x->col-1>=0){
vector<Point>::iterator it = find(p_v.begin(), p_v.end(), Point(x->row,x->col-1,1));
if((*it).high<x->high)
x->max_high = max(x->max_high, (*it).max_high+1);
}
if(x->col+1<m){
vector<Point>::iterator it = find(p_v.begin(), p_v.end(), Point(x->row, x->col+1, 1));
if((*it).high<x->high)
x->max_high = max(x->max_high, (*it).max_high+1);
}
}
int maxs =0;
for (int i = 0; i < n*m; ++i) {
if(maxs<p_v[i].max_high)
maxs = p_v[i].max_high;
}
cout<<maxs;
return 0;
}
这里注意到时间比较长,优化的时候,有时间用在了查找上面,下面用一个三维数组进行优化
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Point{
int r, c;
int h;
Point(int _r, int _c, int _h):r(_r), c(_c),h(_h){};
bool operator < (const Point &p) const{
return h<p.h;
}
};
int main()
{
vector<Point> p_v;
int field[110][110][2];//z轴上面的数字是[0]代表高度[1]代表最长的路径长度
int r, c, t;
cin>>r>>c;
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
cin>>t;
p_v.push_back(Point(i, j, t));
field[i][j][0]=t;
field[i][j][1]=1;
}
}
int rr,cc, maxs;
sort(p_v.begin(), p_v.end());
for (int i = 0; i < r*c; ++i) {
rr = p_v[i].r;
cc = p_v[i].c;
if(rr-1>=0 && field[rr-1][cc][0]<field[rr][cc][0])
{
field[rr][cc][1] = max(field[rr][cc][1],field[rr-1][cc][1]+1);
}
if(rr+1<r && field[rr+1][cc][0]<field[rr][cc][0])
{
field[rr][cc][1] = max(field[rr][cc][1], field[rr+1][cc][1]+1);
}
if(cc-1>=0 && field[rr][cc-1][0]<field[rr][cc][0])
{
field[rr][cc][1] = max(field[rr][cc][1], field[rr][cc-1][1]+1);
}
if(cc+1<c && field[rr][cc+1][0]<field[rr][cc][0])
{
field[rr][cc][1] = max(field[rr][cc][1], field[rr][cc+1][1]+1);
}
}
int max = 0;
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
if(max<field[i][j][1]) max = field[i][j][1];
}
}
cout<<max<<endl;
}