PAT段错误问题


#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

typedef struct {
    int x;
    int y;
    int num;
}PeaNut;


bool DWGreater(const PeaNut rhs, const PeaNut lhs)
{
    return (rhs.num > lhs.num);
}

const int gettime(1);
const int inouttime(1);

int main()
{
    int N,M,K;    
    PeaNut VecPeanut[400];
    int sumtime(0);
    int sumcnt(0);
    int reachtime(0);    /* 到达下一位置所需时间 */
    int rettime(0);    /* 从下一位置返回所需时间 */
    PeaNut lastpea;    /* 当前所处位置 */
    int pos(0);
    N = 0;
    M = 0;
    K = 0;
    while (cin >> N >> M >> K) {
        if (K == 0) {
            cout << sumcnt;
            continue;
        }
        for (int idx = 0; idx < N; idx++) {
            for (int jdx = 0; jdx < M; jdx++) {
                VecPeanut[idx*M + jdx].x = idx;
                VecPeanut[idx*M + jdx].y = jdx;
                cin >> VecPeanut[idx*M + jdx].num;
            }
        }
        sort(VecPeanut, VecPeanut+(M*N), DWGreater);    /* 按num从大到小排序 */
        lastpea.x = -1;
        lastpea.y = VecPeanut[0].y;
        sumtime = 0;
        sumcnt  = 0;
        reachtime = 0;
        rettime = 0;    
        pos = 0;
        while ((pos < M*N) && (VecPeanut[pos].num > 0)) {
            reachtime = abs(VecPeanut[pos].x - lastpea.x) + abs(VecPeanut[pos].y - lastpea.y);
            rettime = VecPeanut[pos].x + inouttime;
            if ((sumtime + (reachtime + gettime + rettime)) > K) {
                break;
            } else {
                sumtime += (reachtime + gettime);    /* 累计时间 */
                lastpea = VecPeanut[pos];    /* 更新上一次位置 */
                sumcnt += VecPeanut[pos].num;    /* 获取花生数量 */
                pos++;
            }
        }
        cout << sumcnt;
    }
    return 0;
}


在codeblock上运行测试几组正常,

放到牛客pat上直接段错误,麻烦大神指点下


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值