Untitled
unknown
plain_text
a year ago
4.2 kB
7
Indexable
#define MAX_N 100
#include <queue>
#include <iostream>
using namespace std;
struct Node{
int row;
int col;
int energy;
int bacteriumType;
};
Node dish[MAX_N][MAX_N];
Node queueN[MAX_N * MAX_N];
struct cmp{
bool operator() (Node x, Node y){
if (x.energy != y.energy){
return x.energy < y.energy;
}
else if (x.row != y.row){
return x.row > y.row;
}
else
return x.col > y.col;
}
};
int n, cnt[2], front, rear, point;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int visited[MAX_N][MAX_N];
priority_queue<Node, vector<Node>, cmp> priorityQueue;
void initQueue(){
front = rear = -1;
}
void enQueue(Node a){
front++;
queueN[front] = a;
}
Node deQueue(){
rear++;
return queueN[rear];
}
bool isEmpty(){
if(front == rear)
return true;
return false;
}
bool checked(int a, int b){
if(a >= 0 && a < n && b >= 0 && b < n)
return true;
return false;
}
void init(int N, int mDish[MAX_N][MAX_N]){
n = N;
cnt[0] = cnt[1] = point = 0;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
dish[i][j].row = i;
dish[i][j].col = j;
dish[i][j].energy = mDish[i][j];
dish[i][j].bacteriumType = 0;
visited[i][j] = 0;
}
}
}
int dropMedicine(int mTarget, int mRow, int mCol, int mEnergy){
int r = mRow - 1;
int c = mCol - 1;
if (dish[r][c].bacteriumType == 0 || dish[r][c].bacteriumType == mTarget){
if (dish[r][c].bacteriumType == 0){
dish[r][c].bacteriumType = mTarget;
cnt[mTarget - 1]++;
mEnergy -= dish[r][c].energy;
}
point++;
priorityQueue = priority_queue<Node, vector<Node>, cmp>();
initQueue();
visited[r][c] = point;
enQueue(dish[r][c]);
while (mEnergy > 0){
while (!isEmpty()){
Node temp = deQueue();
int i=0;
while(i < 4){
int x = temp.row + dx[i];
int y = temp.col + dy[i];
if (checked(x, y) && visited[x][y] < point){
visited[x][y] = point;
if (dish[x][y].bacteriumType == 0){
priorityQueue.push(dish[x][y]);
}
else if (dish[x][y].bacteriumType == mTarget)
enQueue(dish[x][y]);
}
i++;
}
}
if(!priorityQueue.empty()){
Node temp = priorityQueue.top();
priorityQueue.pop();
dish[temp.row][temp.col].bacteriumType = mTarget;
cnt[mTarget - 1]++;
mEnergy -= dish[temp.row][temp.col].energy;
enQueue(dish[temp.row][temp.col]);
}
else
break;
}
}
int res = cnt[mTarget - 1];
return res;
}
int cleanBacteria(int mRow, int mCol){
int r = mRow - 1;
int c = mCol - 1;
int type = dish[r][c].bacteriumType;
if(type == 0)
return -1;
initQueue();
point++;
visited[r][c] = point;
cnt[type - 1]--;
dish[r][c].bacteriumType = 0;
enQueue(dish[r][c]);
while (!isEmpty()){
Node temp = deQueue();
int i = 0;
while(i < 4){
int x = temp.row + dx[i];
int y = temp.col + dy[i];
if (checked(x, y) && visited[x][y] < point && dish[x][y].bacteriumType == type){
dish[x][y].bacteriumType = 0;
cnt[type - 1]--;
visited[x][y] = point;
enQueue(dish[x][y]);
}
i++;
}
}
int res = cnt[type - 1];
return res;
}Editor is loading...
Leave a Comment