跳至正文

TYVJ 第三题 滑雪 解题报告

  • OI路程
题目:
背景 Background
  成成第一次模拟赛 第三道
描述 Description
    trs喜欢滑雪。他来到了一个滑雪场,这个滑雪场是一个矩形,为了简便,我们用r行c列的矩阵来表示每块地形。为了得到更快的速度,滑行的路线必须向下倾斜。
  例如样例中的那个矩形,可以从某个点滑向上下左右四个相邻的点之一。例如24-17-16-1,其实25-24-23…3-2-1更长,事实上这是最长的一条。
输入格式 Input Format
  输入文件

第1行: 两个数字r,c(1<=r,c<=100),表示矩阵的行列。
第2..r+1行:每行c个数,表示这个矩阵。
输出格式 Output Format
  输出文件

仅一行: 输出1个整数,表示可以滑行的最大长度。
我的解答:
  我以为题目就是要找出连续的最大的数字呢, 就是说顺着路径找出最大的~! 一提交,, 40分..
#include <stdio.h>
int map[100][100];
int n, m;

int getv(int x, int y)
{
        if(x < 0 || x >= m || y < 0 || y >= n){
                return –1;
        }
        return map[x][y];
}

int main(void)
{
        int i, j;
        int k;
        int a, b;
//      freopen(“abc.txt”, “r”, stdin);
        scanf(%d%d, &m, &n);
        for(i = 0; i < m; i++){
                for(j = 0; j < n; j++){
                        scanf(%d, &map[i][j]);
                        if(map[i][j] == 1){
                                a = i;
                                b = j;
                        }
                }
        }
        k = 1;
        while(k < m * n){
                if(getv(a – 1, b) == k + 1){
                        a = a – 1;
                        k++;
                        continue;
                }
                if(getv(a + 1, b) == k + 1){
                        a = a + 1;
                        k++;
                        continue;
                }
                if(getv(a, b – 1) == k + 1){
                        b = b – 1;
                        k++;
                        continue;
                }
                if(getv(a, b + 1) == k + 1){
                        b = b + 1;
                        k++;
                        continue;
                }
                break;
        }
        printf(%d\n, k);
//      getch();
        return 0;
}


  后来又以为是求最大的顺序(不一定是1 2 3, 也可以是1 3 4,)
 就是跳跃式前进的, 结果是30分..




#include <stdio.h>
int map[100][100];
int n, m;

int getv(int x, int y)
{
        if(x < 0 || x >= m || y < 0 || y >= n){
                return –1;
        }
        return map[x][y];
}

int main(void)
{
        int i, j;
        int k, t, s;
        int a, b;
        int d, f;
//      freopen(“abc.txt”, “r”, stdin);
        scanf(%d%d, &m, &n);
        for(i = 0; i < m; i++){
                for(j = 0; j < n; j++){
                        scanf(%d, &map[i][j]);
                        if(map[i][j] == 1){
                                a = i;
                                b = j;
                        }
                }
        }
        k = 1;
        while(k < m * n){
                s = 10000000
                t = getv(a – 1, b);
                if(t > k && t < s){
                        d = a – 1;
                        f = b;
                        s = t;
                }
                t = getv(a + 1, b);
                if(t > k && t < s){
                        d = a + 1;
                        f = b;
                        s = t;
                }
                t = getv(a, b – 1);
                if(t > k && t < s){
                        d = a;
                        f = b – 1;
                        s = t;
                }
                t = getv(a, b + 1);
                if(t > k && t < s){
                        d = a;
                        f = b + 1;
                        s = t;
                }
                if(t == k){
                        break;
                }
                a = d;
                b = f;
                k = s;
        }
        printf(%d\n, k);
//      getch();
        return 0;
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注