程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 10285 - Longest Run on a Snowboard

10285 - Longest Run on a Snowboard

編輯:C++入門知識

Problem C

Longest Run on a Snowboard

Input:standard input

Output:standard output

Time Limit: 5 seconds

Memory Limit: 32 MB

 

Michael likes snowboarding. That'snot very surprising, since snowboarding is really great. The bad thing is that inorder to gain speed, the area must slide downwards. Another disadvantage isthat when you've reached the bottom of the hill you have to walk up again orwait for the ski-lift.

Michaelwould like to know how long the longest run in an area is. That area is givenby a grid of numbers, defining the heights at those points. Look at thisexample:

 1  2  3  4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11 10 9Onecan slide down from one point to a connected other one if and only if theheight decreases. One point is connected to another if it's at left, at right,above or below it. In the sample map, a possible slide would be 24-17-16-1 (start at 24, end at 1). Of course if you would go 25-24-23-...-3-2-1,it would be a much longer run. In fact, it's the longest possible.

Input

Thefirst line contains the number of test cases N. Each test case starts with a line containing the name (it's asingle string), the number of rows Rand the number of columns C. Afterthat follow R lines with C numbers each, defining the heights. R and C won't be bigger than 100, N not bigger than 15 andthe heights are always in the range from 0to 100.

Foreach test case, print a line containing the name of the area, a colon, a spaceand the length of the longest run one can slide down in that area.

Sample Input2Feldberg 10 556 14 51 58 8826 94 24 39 4124 16 8 51 5176 72 77 43 1038 50 59 84 815 23 37 71 7796 10 93 53 8294 15 96 69 974 0 62 38 9637 54 55 82 38Spiral 5 51 2 3 4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11 10 9

SampleOutput

Feldberg: 7Spiral: 25[cpp] view plaincopyprint?#include<stdio.h>  
#include<string.h>  
#define max(x,y) ((x)>(y) ? (x):(y))  
int r,c,f[105][105],h[105][105]; 
char name[25]; 
const int x[4]={1,-1}; 
const int y[4]={0,0,1,-1}; 
int dp(int i, int j) 

    int a; 
    if (f[i][j] > 0) return f[i][j]; 
    f[i][j]=1; 
    for(a=0;a<4;a++) 
            if(h[i+x[a]][j+y[a]]!=-1&&h[i][j]>h[i+x[a]][j+y[a]]) 
                f[i][j] = max(f[i][j], dp(i+x[a], j+y[a])+1); 
    return f[i][j]; 

int main() 

    int t,i,j; 
    scanf("%d", &t); 
    while (t--) 
    { 
        memset(f,0,sizeof(f)); 
        memset(h,-1,sizeof(h)); 
        scanf("%s%d%d",name, &r, &c); 
        for (i=1;i<=r;++i) 
            for (j=1;j<=c;++j) 
                scanf("%d", &h[i][j]); 
        int ans=1; 
        for (i=1;i<=r;i++) 
            for (j=1;j<=c;j++) 
            { 
                dp(i, j); 
                ans=(f[i][j]>ans)?f[i][j]:ans; 
            }        
            printf("%s: %d\n", name, ans); 
    } 
    return 0; 

#include<stdio.h>
#include<string.h>
#define max(x,y) ((x)>(y) ? (x):(y))
int r,c,f[105][105],h[105][105];
char name[25];
const int x[4]={1,-1};
const int y[4]={0,0,1,-1};
int dp(int i, int j)
{
 int a;
 if (f[i][j] > 0) return f[i][j];
 f[i][j]=1;
 for(a=0;a<4;a++)
   if(h[i+x[a]][j+y[a]]!=-1&&h[i][j]>h[i+x[a]][j+y[a]])
    f[i][j] = max(f[i][j], dp(i+x[a], j+y[a])+1);
 return f[i][j];
}
int main()
{
 int t,i,j;
 scanf("%d", &t);
 while (t--)
 {
  memset(f,0,sizeof(f));
  memset(h,-1,sizeof(h));
  scanf("%s%d%d",name, &r, &c);
  for (i=1;i<=r;++i)
   for (j=1;j<=c;++j)
    scanf("%d", &h[i][j]);
  int ans=1;
  for (i=1;i<=r;i++)
   for (j=1;j<=c;j++)
   {
    dp(i, j);
    ans=(f[i][j]>ans)?f[i][j]:ans;
   }      
   printf("%s: %d\n", name, ans);
 }
 return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved