Untitled
unknown
plain_text
a year ago
2.3 kB
9
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];
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; // Thay đổi dấu để ưu tiên cột nhỏ hơn
}
return a.first > b.first; // Thay đổi dấu để ưu tiên hàng nhỏ hơn
}
return map[a.first][a.second] > map[b.first][b.second]; // Đảm bảo thứ tự từ nhỏ đến lớn
}
};
priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> pq;
set<pair<int, int>> medicine[3]; // Loại bỏ cmp vì set tự động sắp xếp
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]) {
step = 1;
n = N;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
map[i][j] = mDish[i][j];
visit[i][j] = 0;
}
}
}
int dropMedicine(int mTarget, int mRow, int mCol, int mEnergy) {
int remainEnergy = mEnergy;
pq.push(make_pair(mRow, mCol));
while (remainEnergy > 0 && !pq.empty()) {
auto topPair = pq.top();
pq.pop();
int r = topPair.first;
int c = topPair.second;
if (visit[r][c] != 0) continue; // Kiểm tra nếu đã thăm địa điểm này
visit[r][c] = step++;
medicine[mTarget].insert(make_pair(r, c));
remainEnergy -= 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 && visit[nx][ny] == 0) {
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; // Cần triển khai logic cho hàm này nếu cần
}
Editor is loading...
Leave a Comment