ZOJ-2235

来源:互联网 发布:查看oracle数据库oid 编辑:程序博客网 时间:2024/05/18 04:00

开始的时候把打印语句放到else块里了,导致k很大时不会输出结果,WA调了半天。。坑啊,粗心害死人,其实还是逻辑不严密导致的

#include<stdio.h>#include<stdlib.h>#include<math.h>struct Peanut{    int amount;    int hor;    int ver;    int time;};int cmp2235(const void *p1, const void *p2){    struct Peanut *pp1 = (struct Peanut *) p1;    struct Peanut *pp2 = (struct Peanut *) p2;    return pp2->amount - pp1->amount;}int main(){    int t, m, n, k, i, j, num;    scanf("%d", &t);    struct Peanut *peanut = malloc(2500 * sizeof(struct Peanut));    while (t--)    {        int index = 0;        scanf("%d %d %d", &m, &n, &k);        for (i = 0; i < m; i++)            for (j = 0; j < n; j++)            {                scanf("%d", &num);                if (num)                {                    peanut[index].amount = num;                    peanut[index].hor = j;                    peanut[index++].ver = i;                }            }        qsort(peanut, index, sizeof(struct Peanut), cmp2235);        int picked = 0;        for (i = 0; i < index; i++)        {            if (!i)                peanut[i].time = 1 + peanut[i].ver + 1;            else                peanut[i].time = peanut[i - 1].time                        + abs(peanut[i].ver - peanut[i - 1].ver)                        + abs(peanut[i].hor - peanut[i - 1].hor) + 1;            if (peanut[i].time + peanut[i].ver + 1 <= k)                picked += peanut[i].amount;            else                break;        }        printf("%d\n", picked);    }    free(peanut);    return 0;}


0 0
原创粉丝点击