Untitled
unknown
plain_text
6 months ago
3.7 kB
2
Indexable
Never
#include<queue> #include<vector> #include<iostream> using namespace std; #define MAX_N 100 #define SIZE 105 int map[SIZE][SIZE], dish[SIZE][SIZE], type[SIZE][SIZE]; int visit[SIZE][SIZE]; int indexVisit = 0; int n, total[3]; int dx[4] = { -1, 0, 1, 0 }; int dy[4] = { 0, 1, 0, -1 }; struct Point { int x, y, point; }; struct Cmp{ bool operator()(Point A, Point B) const{ if(A.point != B.point) return A.point < B.point; else { if(A.x != B.x) return A.x > B.x; return A.y > B.y; } } }; priority_queue<Point, vector<Point>, Cmp> pq; queue<Point> q; void init(int N, int mDish[MAX_N][MAX_N]) { n = N; total[1] = total[2] = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { map[i+1][j+1] = dish[i+1][j+1] = mDish[i][j]; type[i+1][j+1] = 0; } } } Point data; Point createData(int x, int y, int point){ data.x = x; data.y = y; data.point = point; return data; } void bfs(int mRow, int mCol, int mTarget){ if(type[mRow][mCol] == mTarget){ data = createData(mRow, mCol, dish[mRow][mCol]); q.push(data); while (!q.empty()) { Point current = q.front(); q.pop(); for (int i = 0; i < 4; i++){ int newX = current.x + dx[i]; int newY = current.y + dy[i]; if(newX < 1 || newX > n || newY < 1 || newY > n || visit[newX][newY] == indexVisit) continue; if(dish[newX][newY] == 0 && type[newX][newY] == mTarget){ visit[newX][newY] = indexVisit; data = createData(newX, newY, 0); q.push(data); continue; } if(type[newX][newY] == 0) { visit[newX][newY] = indexVisit; data = createData(newX, newY, dish[newX][newY]); pq.push(data); } } } } } int dropMedicine(int mTarget, int mRow, int mCol, int mEnergy) { indexVisit++; // if(type[mRow][mCol] != 0 && type[mRow][mCol] != mTarget) return total[mTarget]; pq = priority_queue<Point, vector<Point>, Cmp> (); q = queue<Point> (); bfs(mRow, mCol, mTarget); if(pq.empty() && type[mRow][mCol] == 0){ data = createData(mRow, mCol, dish[mRow][mCol]); visit[mRow][mCol] = indexVisit; pq.push(data); } while (mEnergy > 0 && !pq.empty()) { Point current = pq.top(); pq.pop(); total[mTarget]++; mEnergy -= dish[current.x][current.y]; dish[current.x][current.y] = 0; type[current.x][current.y] = mTarget; for (int i = 0; i < 4; i++) { int newX = current.x + dx[i]; int newY = current.y + dy[i]; if(newX < 1 || newX > n || newY < 1 || newY > n || visit[newX][newY] == indexVisit) continue; if(dish[newX][newY] == 0 && type[newX][newY] == mTarget){ bfs(newX, newY, mTarget); } if(type[newX][newY] == 0) { visit[newX][newY] = indexVisit; data = createData(newX, newY, dish[newX][newY]); pq.push(data); } } } return total[mTarget]; } int cleanBacteria(int mRow, int mCol) { indexVisit++; if(type[mRow][mCol] == 0) return -1; int mTarget = type[mRow][mCol]; q = queue<Point> (); data = createData(mRow, mCol, 0); q.push(data); while(!q.empty()){ Point current = q.front(); q.pop(); dish[current.x][current.y] = map[current.x][current.y]; type[current.x][current.y] = 0; total[mTarget]--; for (int i = 0; i < 4; i++){ int newX = current.x + dx[i]; int newY = current.y + dy[i]; if(newX < 1 || newX > n || newY < 1 || newY > n || visit[newX][newY] == indexVisit) continue; if(type[newX][newY] == mTarget){ visit[newX][newY] = indexVisit; data = createData(newX, newY, 0); q.push(data); } } } return total[mTarget]; }
Leave a Comment