社区讨论

站外题求助

学术版参与者 2已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@m4p2wyw5
此快照首次捕获于
2024/12/15 12:01
去年
此快照最后确认于
2025/11/04 12:48
4 个月前
查看原帖
代码
CPP
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
const int N = 30;
int mp[N][N];
int n, m, k;
int tm;
int fx, fy, ex, ey;
int pn;
int ans;
struct dire
{
    int x, y, sum;
} stu[N * N];
bool comp(struct dire a, struct dire b)
{
    return a.sum > b.sum;
}
int main()
{
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> mp[i][j];
            if (mp[i][j] > 0)
            {
                stu[++pn].sum = mp[i][j];
                stu[pn].x = i;
                stu[pn].y = j;
            }
        }
    }
    sort(stu + 1, stu + pn + 1, comp);
    fx = 1;
    fy = stu[1].y;
    k--;
    for (int i = 1; i <= pn; i++)
    {
        tm = 0;
        ex = stu[i].x;
        ey = stu[i].y;
        tm = abs(fx - ex) + abs(fy - ey);
        k--;
        k -= tm;
        if (k >= ex)
        {
            ans += mp[ex][ey];
            fx = ex;
            fy = ey;
        }
        else
        {
            cout << ans << endl;
            return 0;
        }
    }
    cout << ans << endl;
    system("pause");
    return 0;
}

回复

2 条回复,欢迎继续交流。

正在加载回复...