Untitled
unknown
plain_text
20 days ago
2.3 kB
2
Indexable
Never
#include <iostream> #include <queue> #include <vector> using namespace std; #define MAX_HEIGHT 1001 #define MAX_WIDTH 200001 #define MAX_BOX 10001 #define INF 99999999 struct Space { int id, row; bool haveExit; }; Space space[MAX_WIDTH]; vector<int> graph[MAX_BOX]; int visit[MAX_BOX]; void init(int mH, int mW) { for(int i=0; i<mW; i++) { space[i].id = -1; space[i].row = mH; space[i].haveExit = 0; } for(int i=0; i<MAX_BOX; i++) { graph[i].clear(); visit[i] = 0; } } int dropBox(int mId, int mLen, int mExitA, int mExitB, int mCol) { int rowTarget = INF; for(int i=mCol; i<mCol+mLen; i++) { rowTarget = min(rowTarget, space[i].row); } rowTarget--; if(space[mCol+mExitA].row = rowTarget + 1 && space[mCol+mExitA].id !=-1) { //Check exitA of above box(mId) graph[mId].push_back(space[mCol+mExitA].id); //graph[space[mCol + mExitA].id].push_back(mId); } if(space[mCol+mExitB].row = rowTarget + 1 && space[mCol + mExitB].id != -1) { //Check exitB of above box(mId) graph[mId].push_back(space[mCol + mExitB].id); //graph[space[mCol + mExitB].id].push_back(mId); } if(mCol != 0 && space[mCol - 1].row == rowTarget) {//Check left box(mID) have or not have graph[mId].push_back(space[mCol - 1].id); graph[space[mCol - 1].id].push_back(mId); } if(space[mCol + mLen].row == rowTarget) { //Check right box(mId) have or not have other box graph[mId].push_back(space[mCol + mLen].id); graph[space[mCol + mLen].id].push_back(mId); } for(int i=mCol; i<mCol+mLen; i++) { if(space[i].row == rowTarget + 1 && space[i].haveExit) { graph[space[i].id].push_back(mId); } //Update id, rowMax, haveExit at all position of box(mId) in space space[i].id = mId; space[i].row = rowTarget; if(i == mCol + mExitA || i == mCol + mExitB) { space[i].haveExit = 1; } else { space[i].haveExit = 0; } } return rowTarget; } int explore(int mIdA, int mIdB) { queue<int, int>pq; pq.push(make_pair(1, mIdA)); visit[mIdA] = 1; while(!pq.empty()) { pair<int, int> temp = pq.front(); pq.pop(); int cnt = temp.first; int boxCur = temp.second; if(boxCur == mIdB) return cnt; for(int boxId : graph[boxCur]) { pq.push(make_pair(cnt + 1, boxCur)); } } return -1; }
Leave a Comment