Untitled
unknown
plain_text
a month ago
6.6 kB
4
Indexable
Never
#include<iostream> #include<algorithm> #include<vector> #include<unordered_map> #include<unordered_set> using namespace std; #define MAXN 3001 #define MAXD 6001 #define MAXH 30001 unordered_set<int> rowmap[MAXN]; unordered_set<int> colmap[MAXN]; unordered_set<int> leftdiag[MAXD]; unordered_set<int> rightdiag[MAXD]; struct Hole { int row; int col; bool isavailable; int count; }; Hole holes[MAXH]; struct Diagonal { int startrow; int endrow; int startcol; int endcol; }; Diagonal leftdiagonal[MAXD]; Diagonal rightdiagonal[MAXD]; int gridsize; int county; //to check for the hole closed void init(int N) { gridsize = N; for (int i = 0; i < MAXN; i++) { rowmap[i].clear(); colmap[i].clear(); } for (int i = 0; i < MAXD; i++) { leftdiag[i].clear(); rightdiag[i].clear(); } for (int i = 0; i < MAXH; i++) { holes[i].row = -1; holes[i].col = -1; holes[i].isavailable = false; holes[i].count = 0; } for (int i = 0; i < MAXD; i++) { leftdiagonal[i].startrow = -1; leftdiagonal[i].startcol = -1; leftdiagonal[i].endrow = -1; leftdiagonal[i].endcol = -1; rightdiagonal[i].startrow = -1; rightdiagonal[i].startcol = -1; rightdiagonal[i].endrow = -1; rightdiagonal[i].endcol = -1; } county = 0; } void addDiagonal(int mARow, int mACol, int mBRow, int mBCol) { if (mACol + mARow == mBCol + mBRow) { if (mARow < mBRow) { leftdiagonal[mARow + mACol].startrow = mARow; leftdiagonal[mARow + mACol].startcol = mACol; leftdiagonal[mARow + mACol].endrow = mBRow; leftdiagonal[mARow + mACol].endcol = mBCol; } else { leftdiagonal[mARow + mACol].startrow = mBRow; leftdiagonal[mARow + mACol].startcol = mBCol; leftdiagonal[mARow + mACol].endrow = mARow; leftdiagonal[mARow + mACol].endcol = mACol; } } else { if (mARow < mBRow) { rightdiagonal[mARow - mACol + gridsize].startrow = mARow; rightdiagonal[mARow - mACol + gridsize].startcol = mACol; rightdiagonal[mARow - mACol + gridsize].endrow = mBRow; rightdiagonal[mARow - mACol + gridsize].endcol = mBCol; } else { rightdiagonal[mARow - mACol + gridsize].startrow = mBRow; rightdiagonal[mARow - mACol + gridsize].startcol = mBCol; rightdiagonal[mARow - mACol + gridsize].endrow = mARow; rightdiagonal[mARow - mACol + gridsize].endcol = mACol; } } } void addHole(int mRow, int mCol, int mID) { holes[mID].row = mRow; holes[mID].col = mCol; holes[mID].isavailable = true; rowmap[mRow].emplace(mID); colmap[mCol].emplace(mID); leftdiag[mRow + mCol].emplace(mID); rightdiag[mRow - mCol + gridsize].emplace(mID); } void eraseHole(int mRow, int mCol) { for (auto it : rowmap[mRow]) { if (holes[it].col == mCol) { holes[it].isavailable = false; break; } } } int hitMarble(int mRow, int mCol, int mK) { county++; int best = 0; while (mK--) { int dist = 100000; for (auto it : rowmap[mRow]) { if (abs(holes[it].col - mCol) * 10 <= dist && (holes[it].isavailable == true) && (holes[it].count != county)) { if (abs(holes[it].col - mCol) * 10 < dist || holes[it].col < holes[best].col) { dist = abs(holes[it].col - mCol) * 10; best = it; } } } for (auto it : colmap[mCol]) { if (abs(holes[it].row - mRow) * 10 <= dist && (holes[it].isavailable == true) && (holes[it].count != county)) { if (abs(holes[it].row - mRow) * 10 < dist || holes[it].row < holes[best].row) { dist = abs(holes[it].row - mRow) * 10; best = it; } } } //for diagonal 1 if (mRow >= leftdiagonal[mRow + mCol].startrow && mRow <= leftdiagonal[mRow + mCol].endrow) { for (auto it : leftdiag[mRow + mCol]) { if (holes[it].row >= leftdiagonal[mRow + mCol].startrow && holes[it].row <= leftdiagonal[mRow + mCol].endrow) { if (abs(holes[it].row - mRow) * 14 <= dist && (holes[it].isavailable == true) && (holes[it].count != county)) { if (abs(holes[it].row - mRow) * 14 < dist || holes[it].row < holes[best].row || (holes[it].row == holes[best].row && holes[it].col < holes[best].col)) { dist = abs(holes[it].row - mRow) * 14; best = it; } } } } } //for diagonal 2 if(mRow >= rightdiagonal[mRow - mCol + gridsize].startrow && mRow <= rightdiagonal[mRow - mCol + gridsize].endrow) { for (auto it : rightdiag[mRow - mCol + gridsize]) { if (holes[it].row >= rightdiagonal[mRow - mCol + gridsize].startrow && holes[it].row <= rightdiagonal[mRow - mCol + gridsize].endrow) { if (abs(holes[it].row - mRow) * 14 <= dist && (holes[it].isavailable == true) && (holes[it].count != county)) { if (abs(holes[it].row - mRow) * 14 < dist || holes[it].row < holes[best].row || (holes[it].row == holes[best].row && holes[it].col < holes[best].col)) { dist = abs(holes[it].row - mRow) * 14; best = it; } } } } } if (dist == 100000) break; else { holes[best].count = county; mRow = holes[best].row; mCol = holes[best].col; } } if (best == 0) return -1; else return best; }
Leave a Comment