Untitled

 avatar
unknown
plain_text
6 months ago
2.5 kB
2
Indexable
#include <queue>
#include <vector>
#include <set>
#include <iostream>
#define MAX_N 100
using namespace std;

int map[MAX_N + 1][MAX_N + 1];
int visit[MAX_N + 1][MAX_N + 1];
int attend[MAX_N + 1][MAX_N + 1];

struct cmp{
    bool operator()(pair<int, int> a, pair<int, int> b) {
        if(map[a.first][a.second] == map[b.first][b.second]) {
            if(a.first == b.first) {
                return a.second < b.second;
            }
            return a.first < b.first;
        }
        return map[a.first][a.second] > map[b.first][b.second];
    }
};

priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> pq;
set<pair<int, int>> medicine[3];

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
int n;
int step;
void init(int N, int mDish[MAX_N][MAX_N])
{
    n = N;
    step = 1;
    for(int i=1; i<=N; i++) {
        for(int j=1; j<=N; j++) {
            map[i][j] = mDish[i][j];
            visit[i][j] = 0;
            attend[i][j] = 0;
        }
    }
}

int dropMedicine(int mTarget, int mRow, int mCol, int mEnergy)
{
    int e = mEnergy;
    if(map[mRow][mCol] == 0) {
        e = e - map[mRow][mCol];
        visit[mRow][mCol] = step++;
        attend[mRow][mCol] = 1;
        medicine[mTarget].insert(make_pair(mRow, mCol));
    }
    for(int k=0; k<4; k++) {
        int nx = mRow + dx[k];
        int ny = mCol + dy[k];
        if(nx >=1 && nx <= n && ny >= 1 && ny <= n && !attend[nx][ny] ) {
            attend[nx][ny] = 1;
            pq.push(make_pair(nx, ny));
        }
    }
    while(e > 0 && !pq.empty()) {
        auto topPair = pq.top();
        pq.pop();
        int r = topPair.first;
        int c = topPair.second;	
        if(!visit[r][c]) { // Kiểm tra xem ô này có bị thăm hay chưa
            visit[r][c] = step++;
            medicine[mTarget].insert(make_pair(r,c));
            e = e - map[r][c];
            for(int k = 0; k < 4; k++) {
                int nx = r + dx[k];
                int ny = c + dy[k];
                if(nx >=1 && nx <= n && ny >= 1 && ny <= n && !attend[nx][ny] ) {
                    attend[nx][ny] = 1;
                    pq.push(make_pair(nx, ny));
                }
            }
        }
    }
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=n; j++) {
            cout<<visit[i][j]<<" ";
        }
        cout<<endl;
    }
    return medicine[mTarget].size();
}

int cleanBacteria(int mRow, int mCol)
{
    return -1;
}
Editor is loading...
Leave a Comment