POJ 1088 skiing and poj1088 skiing

Source: Internet
Author: User

POJ 1088 skiing and poj1088 skiing

Description

It's not surprising that Michael loves skiing because skiing is really exciting. But in order to get the speed, the slide area must be tilted down, and when you slide to the bottom, you have to go uphill again or wait for the elevator to carry you. Michael wants to know the longest landslide in a region. A region is given by a two-dimensional array. Each number in the array represents the vertex height. The following is an example.
 1  2  3  4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11 10 9

A person can slide from a certain point to one of the four adjacent points up and down, when and only when the height is reduced. In the preceding example, a slide is 24-17-16-1. Of course, 25-24-23-...-3-2-1 is longer. In fact, this is the longest one.

Input

The first line indicates the number of rows in the region R and the number of columns C (1 <= R, C <= 100 ). Below is the R row, each row has a C integer, representing the height h, 0 <= h <= 10000.

Output

The length of the maximum output area.

Sample Input

5 51 2 3 4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11 10 9

Sample Output

25

Source

SHTSC 2002


This question is generally answered using recursion + Memory search.

Here we use a new method:

According to the characteristics of the question, you must sort all the values in the descending order, and then start searching from the square of the minimum value. It is feasible to search for the adjacent four squares each time, and then store the maximum value; in this way, the answer is directly obtained without recursion.


#include <stdio.h>#include <algorithm>using namespace std;const int MAX_N = 101;int R, C;int ski[MAX_N][MAX_N];pair<int, int> arr[MAX_N*MAX_N];int skiLen[MAX_N][MAX_N];const int dx[] = {-1, 1, 0, 0};const int dy[] = { 0, 0, -1, 1};inline bool cmp(pair<int, int> &a, pair<int, int> &b){int x1 = a.first, y1 = a.second;int x2 = b.first, y2 = b.second;if (ski[x1][y1] == ski[x2][y2]) return false;return ski[x1][y1] < ski[x2][y2];}inline bool isLegeal(int x, int y){return x>=0 && y>=0 && x<R && y<R;}int getLongest(){int len = 0, n = R * C;for (int i = 0; i < n; i++){int x = arr[i].first, y = arr[i].second;for (int k = 0; k < 4; k++){int nx = x+dx[k], ny = y+dy[k];if (isLegeal(nx, ny) && ski[nx][ny] < ski[x][y]){int t = skiLen[nx][ny]+1;if (t > skiLen[x][y]) skiLen[x][y] = t;}}if (len < skiLen[x][y]) len = skiLen[x][y];}return len;}int main(){while (~scanf("%d %d", &R, &C)){int k = 0;for (int i = 0; i < R; i++){for (int j = 0; j < C; j++){scanf("%d", ski[i]+j);arr[k].first = i;arr[k++].second = j;skiLen[i][j] = 1;}}sort(arr, arr+k, cmp);printf("%d\n", getLongest());}return 0;}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.