Untitled

mail@pastecode.io avatar
unknown
plain_text
4 days ago
1.7 kB
4
Indexable
Never
#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;
			}
			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>, cmp> 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])
{
	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 && !pq.empty()) {
		auto topPair = pq.top();
		cout<<topPair.first<<" "<<topPair.second<<endl;
		pq.pop();
		int r = topPair.first;
		int c = topPair.second;
		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]) {
				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;
}
Leave a Comment