90了,有没有大佬救救我哇。。。
查看原帖
90了,有没有大佬救救我哇。。。
583482
LJQQ楼主2022/2/11 17:29
#include<iostream>
#include<algorithm>
#include<queue>
#include<string.h>
using namespace std;
int m, n;
int map[22][22];
int vis[22][22];
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
bool in (int x,int y)
{
    return x >= 1 && x <= m && y >= 1 && y <= n;
}
struct node
{
    int x;
    int y;
    int ss;
}q[406];
struct qie
{
    int x;
    int y;
    int s;
    qie(int x,int y,int s=0):x(x),y(y),s(s){}
};
bool cmp(node &a,node &b)
{
    return a.ss > b.ss;
}
int  bfs(qie l,qie p)
{
    queue<qie> q;
    q.push(l);
    while (!q.empty())
    {
        qie now=q.front();
        q.pop();
        if (now.x==p.x&&now.y==p.y)
        {
            return now.s;
        }
        for (int  i = 0; i < 4; i++)
        {
            qie next = now;
            next.x += dir[i][0];
            next.y += dir[i][1];
            if (!vis[next.x][next.y]&&in(next.x,next.y))
            {
                vis[next.x][next.y] = 1;
                next.s++;
                q.push(next);
            }
            
        }
        
    }
    
}
int main(int argc, char const *argv[])
{
    int step;
    cin >> m >> n>>step;
    int k = 0;
    for (int  i = 1; i <= m; i++)
    {
        for (int  j = 1; j <= n; j++)
        {
            cin >> map[i][j];
            if (map[i][j]>0)
            {

                q[k].x = i;
                q[k].y = j;
                q[k++].ss = map[i][j];
            }
            
        }
        
    }
    sort(q, q + k, cmp);
    int sum=q->x+1;
    int all = q->ss;
    int i;
    for ( i = 0; i < k; i++)
    {
        memset(vis, 0, sizeof(vis));
        int sx, sy,ex,ey;
        sx = q[i].x;
        sy = q[i].y;
        vis[sx][sy] = 1;
        ex = q[i + 1].x;
        ey = q[i + 1].y;
        qie o(ex, ey);
        qie x(sx, sy);
        int t = bfs(x, o)+1;
        //cout << t <<" "<<sum<< endl;
        sum+=t;
        all += q[i+1].ss;
        if (sum+ex>step)
        {
            all -= q[i+1].ss;
            break;
        }
    }
    cout << all;

    return 0;
}

2022/2/11 17:29
加载中...