2015 程序设计实习之递归+动规作业1

  • Post author:
  • Post category:其他


A:The Sierpinski Fractal(1941POJ)

总时间限制:

1000ms

内存限制:

65536kB
描述

Consider a regular triangular area, divide it into four equal triangles of half height and remove the one in the middle. Apply the same operation recursively to each of the three remaining triangles. If we repeated this procedure infinite times, we’d obtain something with an area of zero. The fractal that evolves this way is called the Sierpinski Triangle. Although its topological dimension is 2, its Hausdorff-Besicovitch dimension is log(3)/log(2)~1.58, a fractional value (that’s why it is called a fractal). By the way, the Hausdorff-Besicovitch dimension of the Norwegian coast is approximately 1.52, its topological dimension being 1.

For this problem, you are to outline the Sierpinski Triangle up to a certain recursion depth, using just ASCII characters. Since the drawing resolution is thus fixed, you’ll need to grow the picture appropriately. Draw the smallest triangle (that is not divided any further) with two slashes, to backslashes and two underscores like this:

 /\
/__\

To see how to draw larger triangles, take a look at the sample output.

输入

The input contains several testcases. Each is specified by an integer n. Input is terminated by n=0. Otherwise 1<=n<=10 indicates the recursion depth.

输出

For each test case draw an outline of the Sierpinski Triangle with a side’s total length of 2

n

characters. Align your output to the left, that is, print the bottom leftmost slash into the first column. The output must not contain any trailing blanks. Print an empty line after each test case.

样例输入

3
2
1
0

样例输出

       /\
      /__\
     /\  /\
    /__\/__\
   /\      /\
  /__\    /__\
 /\  /\  /\  /\
/__\/__\/__\/__\

   /\
  /__\
 /\  /\
/__\/__\

 /\
/__\

提示




The Sierpinski-Triangle up to recursion depth 7

来源

Ulm Local 2002

#include<iostream>
#include<memory.h>
#include<stdio.h>
#include<cmath>
using namespace std;
char map[3000][1500];
void draw(int n,int x,int y)
{
    if(n==1)
    {    map[x][y]=map[x+1][y+1]='/';
        map[x+1][y]=map[x+2][y]='_';
        map[x+3][y]=map[x+2][y+1]='\\';
        return ;
    }
    int size=pow(2,n-1);
    draw(n-1,x,y);
    draw(n-1,x+size,y+size);
    draw(n-1,x+2*size,y);
    return;
}

int main()
{
    int n;
    while (cin>>n){
        if(n==0)
            break;
    memset(map,' ',sizeof(map));
        draw(n,0,0);
        int size=pow(2,n);
        for(int i=size-1;i>=0;i--)
        {for(int j=0;j<=2*size-1;j++)
            { cout<<map[j][i];}
            cout<<endl;
        }
        cout<<endl;
    }
        return 0;
}

B:滑雪(1088POJ)

总时间限制:

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

来源

Don’t know
#include<iostream>
using namespace std;
int height[110][110]={0};
int f[110][110]={0};
int row,col;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,1,-1};
bool valid(int x_,int y_)
{
    if(x_>=1&&x_<=row&&y_>=1&&y_<=col)
        return true;
    else
        return false;
}
int adventure(int x,int y)
{
    if(f[x][y]>0)
        return f[x][y];
    int temp[4]={0,0,0,0};
    for(int i=0;i<4;++i)
    {
        int tempx=x+dx[i];
        int tempy=y+dy[i];
        if(height[tempx][tempy]>=height[x][y])
            continue;
        if(f[tempx][tempy]!=0)
            temp[i]=f[tempx][tempy]+1;
        else if(valid(tempx,tempy))
        {
            temp[i]=1+adventure(tempx, tempy);
        }
    }
    int max=0;
    for(int i=0;i<4;++i)
        if(temp[i]>max)
            max=temp[i];
    f[x][y]=max;
    return max;
}
int main()
{
        cin>>row>>col;
    for(int i=1;i<=row;++i)
        for(int j=1;j<=col;++j)
            cin>>height[i][j];
    for(int i=1;i<=row;++i)
        for(int j=1;j<=col;++j)
            if((height[i][j]<=height[i-1][j]||!valid(i-1,j))&&(height[i][j]<=height[i+1][j]||!valid(i+1,j))&&(height[i][j]<=height[i][j+1]||!valid(i,j+1))&&(height[i][j]<=height[i][j-1]||!valid(i,j-1)))
                f[i][j]=1;
    int max=0;
    for(int i=1;i<=row;++i)
        for(int j=1;j<=col;++j)
        {
            f[i][j]=adventure(i,j);
            if(f[i][j]>=max)
             max=f[i][j];
        }
    cout<<max<<endl;
    return 0;
}



版权声明:本文为Irene_ruru原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。