void enqueue_worklist_item(const worklist_item & item) { worklist.push(item); }
void ofURLFileLoaderImpl::clear(){ Poco::ScopedLock<ofMutex> lock(mutex); requests.clear(); while(!responses.empty()) responses.pop(); }
void push(U &&u) { boost::lock_guard<boost::mutex> l(m); q.push( boost::forward<U>(u) ); c.notify_one(); }
// Return whether the stack is empty. bool empty() { return left.empty() && right.empty(); }
void* listen_t(void *ptr) { sem_init(&mutexqueue,0,1); int n; socklen_t len; char *method, buff[1024]; len=sizeof(current_socket.client); time_t it; incoming_request.type=0; while(1) { char RequestPath[100]; FILE *tmp;struct stat attr; incoming_request.connection=accept(current_socket.socket_id,(struct sockaddr *)& (current_socket.client),&len); n=read(incoming_request.connection,buff,1024); char *z = buff; incoming_request.buffers = z; method=strtok(buff," "); incoming_request.path=strtok(NULL," "); incoming_request.request_method=strtok(NULL,"\n"); if(strcmp(method,"GET")==0) incoming_request.type=1; else if(strcmp(method,"HEAD")==0) incoming_request.type=2; else incoming_request.type=3; incoming_request.ip= (¤t_socket.client)->sin_addr.s_addr; for (int i = 0; i < (incoming_request.path).length(); i++) { if (incoming_request.path[i] == '\n' || incoming_request.path[i] == '\r' ) incoming_request.path[i] = '\0'; } strcpy(RequestPath,RootPath); const char* e = (incoming_request.path).c_str(); strcat(RequestPath,e); tmp = fopen(RequestPath,"r"); if(tmp != NULL) { fseek(tmp,0,SEEK_END); incoming_request.filesize=ftell(tmp); fclose(tmp); incoming_request.status = 200; } else { incoming_request.status = 404;incoming_request.filesize=0; } if(strcmp(method,"HEAD")==0) { incoming_request.filesize=0; } time(&it); stat(RequestPath, &attr); incoming_request.LastModifiedTime=asctime(gmtime(&attr.st_mtime)); incoming_request.IncomingTime=asctime(gmtime(&it)); if(debug==1) { debug_request = incoming_request; debugging_request(); } else { sem_wait(&mutexqueue); if (sched == 0) { readyQueue.push(incoming_request); } else { readyQueueP.push(incoming_request); } sem_post(&mutexqueue); } } }
int main(int argc, char *argv[]) { ifstream ifs(argv[1]); while(1) { ifs >> column; ifs >> low; if (low ==0 && column ==0) break; // init for(int i=0; i<low; ++i) { for(int j=0; j<column; ++j) { filed[i][j] = 1000; } } // mark own area int existing_num; ifs >> existing_num; point p; for(int i=0; i<existing_num; ++i) { int l,c; ifs >> c >> l; p.push_back(l-1); p.push_back(c-1); p.push_back(0); que.push(p); while(que.size() != 0) { setQue(que.front()); que.pop(); } p.clear(); } // 初期化のためにコピー c_array(); //debug(); // culc plan int plan_num; ifs >> plan_num; int ans=0; for(int i=0; i<plan_num; ++i) { int l,c; area = 0; ifs >> c >> l; p.push_back(l-1); p.push_back(c-1); p.push_back(0); que.push(p); while(que.size() != 0) { setQue(que.front()); que.pop(); } p.clear(); if (ans < area) { ans = area; } // 予定地分もメモに記憶させているので初期化 rec_array(); } cout << ans << endl; } }
// Removes the element on top of the stack. void pop() { left.empty() ? right.pop() : left.pop(); }
void enqueueCharacter(char ch){ q.push(ch); }
void gPushTouch( queue<TouchHolder> &touchQueue, TouchHolder touchHolder ) { touchQueue.push(touchHolder); }
int main() { int n; cin>>n; int cntR=0,cntl=0; m.clear(); while(!q.empty())q.pop(); string s,s1,s2; for(int i=0;i<n;i++) { cin>>s; int cr=0; for(int i=0;i<s.length();i++) { s[i]=tolower(s[i]); if (s[i]=='r') cr++; } //cout<<s<<cr<<endl; q.push(MP(s,cr)); } cin>>n; for(int i=0;i<n;i++) { cin>>s1>>s2; int cr1=0; for(int i=0;i<s1.length();i++) { s1[i]=tolower(s1[i]); if (s1[i]=='r') cr1++; } int cr2=0; for(int i=0;i<s2.length();i++) { s2[i]=tolower(s2[i]); if (s2[i]=='r') cr2++; } //cout<<s1<<cr1<<s2<<cr2<<endl; m.insert(pss(MP(s1,cr1),MP(s2,cr2))); } while(!q.empty()) { psi tmp=q.front(); q.pop(); if(m[tmp].first=="") { cntl+=tmp.first.length(); cntR+=tmp.second; } else { int cntr1=tmp.second; int cntr2=m[tmp].second; int len1=tmp.first.length(); int len2=m[tmp].first.length(); if(cntr2<cntr1) cntR+=cntr2,cntl+=len2; else if(cntr2==cntr1) cntR+=cntr2,cntl+=(len1>len2?len2:len1); else cntR+=cntr1,cntl+=len1; //cout<<cntr1<<cntr2<<cntR<<endl; //cout<<len1<<len2<<cntl<<endl; } } cout<<cntR<<" "<<cntl<<endl; return 0; }
char dequeueCharacter(){ char c = q.front(); q.pop(); return c; }
int main() { ifstream fin; string FileName; float SimulationTime; int QuantumSize; int NumProcesses; int Lines; float TotalLength = 0; float AveJobLength; float TotalTurnaroundTime = 0; float AveTurnaroundTime; float TotalWaitTime = 0; float AveWaitTime; srand(time(NULL)); cout << "Please enter the file name: " << endl; // cin >> FileName; FileName = "input.txt"; fin.open(FileName.c_str()); if(fin.is_open()) { int Throughput = 0; int JobsInSystem = 0; int CurrentSystemTime = 0; // int JobsSkipped = 0; cout << "Please enter the desired simulation time (in seconds): "; //cin >> SimulationTime; SimulationTime = 2.1; SimulationTime = SimulationTime * 1000; cout << endl; cout << "Please enter the desired quantum size (in milliseconds): "; //cin >> QuantumSize; QuantumSize = 50; cout << endl; cout << "Please enter the number of processes allowed in the system: "; //cin >> NumProcesses; NumProcesses = 4; cout << endl; jobs NextJob; fin >> Lines; for(int i=0; i<Lines; i++) { fin >> NextJob.StartTime >> NextJob.PID >> NextJob.ProbIORequest >> NextJob.Length; NextJob.TurnaroundTime = 0; NextJob.WorkTime = 0; NextJob.TotalTime = 0; incoming.push(NextJob); // cout << NextJob.StartTime << "\t" // << NextJob.PID << "\t" // << NextJob.ProbIORequest << "\t" // << NextJob.Length << "\n"; fout << NextJob.StartTime << "\t" << NextJob.PID << "\t" << NextJob.ProbIORequest << "\t" << NextJob.Length << "\n"; } for(int j=0; j<NumProcesses; j++) { ready.push(incoming.front()); // incoming.front().TotalTime = time(NULL); TotalLength = TotalLength + incoming.front().Length; // cout << "TotalLength is " << TotalLength << "\n"; incoming.pop(); } while(CurrentSystemTime <= SimulationTime && (!ready.empty() || !IO.empty())) { // cout << " CurrentSystemTime is : " << CurrentSystemTime << "\n"; fout << " CurrentSystemTime is : " << CurrentSystemTime << "\n"; CurrentSystemTime = CPU(QuantumSize, ready, IO, Throughput, JobsInSystem, CurrentSystemTime, TotalLength, TotalTurnaroundTime, TotalWaitTime); IO_Process(QuantumSize, ready, IO, Throughput, JobsInSystem, TotalTurnaroundTime); } // JobsSkipped = TotalJobsSkip(incoming); JobsInSystem = JobsStillInSystem(ready, IO); AveJobLength = TotalLength/float(Throughput + JobsInSystem); AveTurnaroundTime = TotalTurnaroundTime/float(Throughput + JobsInSystem); AveWaitTime = TotalWaitTime/float(Throughput + JobsInSystem); // ----------------------------------------------------------------------------------------- // cout << "Throughput (number of jobs completed during the simulation):\t" << Throughput << "\n" // << "Number of jobs still in system:\t" << JobsInSystem << "\n" // // << "Number of jobs skipped:\t" << JobsSkipped << "\n" // << "Average job length excluding I/O time:\t" << AveJobLength << " (ms)" << "\n" // << "Average turnaround time:\t" << AveTurnaroundTime << " (ms)" << "\n" // // << "Average waiting time per process:\t" << AveWaitTime << " (ms)" << "\n"; // // << "CPU utilization (percentage of time CPU is busy):\t" << Throughput << '%' << "\n"; fout << "Throughput (number of jobs completed during the simulation):\t" << Throughput << "\n" << "Number of jobs still in system:\t" << JobsInSystem << "\n" // << "Number of jobs skipped:\t" << JobsSkipped << "\n" << "Average job length excluding I/O time:\t" << AveJobLength << " (ms)" << "\n" << "Average turnaround time:\t" << AveTurnaroundTime << " (ms)" << "\n" << "Average waiting time per process:\t" << AveWaitTime << " (ms)" << "\n"; // << "CPU utilization (percentage of time CPU is busy):\t" << Throughput << '%' << "\n"; }
void solve ( ) { input(); w[ex][ey] = 0; ac p,q; p = a[ex][ey]; p.step = 0; while ( !que.empty() ) que.pop(); que.push(p); while ( !que.empty() ) { p = que.front(); que.pop(); if ( w[p.x][p.y] < p.step ) continue; if ( p.up != 1 && p.x > 0 ) { q = a[p.x-1][p.y]; if ( p.up == 0 ) q.step = p.step; else q.step = p.step+1; if ( w[q.x][q.y] > q.step ) { w[q.x][q.y] = q.step; que.push(q); } } if ( p.down != 1 && p.x < 200 ) { q = a[p.x+1][p.y]; if ( p.down == 0 ) q.step = p.step; else q.step = p.step+1; if ( w[q.x][q.y] > q.step ) { w[q.x][q.y] = q.step; que.push(q); } } if ( p.left != 1 && p.y > 0 ) { q = a[p.x][p.y-1]; if ( p.left == 0 ) q.step = p.step; else q.step = p.step+1; if ( w[q.x][q.y] > q.step ) { w[q.x][q.y] = q.step; que.push(q); } } if ( p.right != 1 && p.y < 200 ) { q = a[p.x][p.y+1]; if ( p.right == 0 ) q.step = p.step; else q.step = p.step+1; if ( w[q.x][q.y] > q.step ) { w[q.x][q.y] = q.step; que.push(q); } } } if ( w[0][0] == inf ) printf("-1\n"); else printf("%d\n",w[0][0]); }
worklist_item next_worklist_item() { worklist_item foo = worklist.top(); worklist.pop(); return foo; }
int main() { int K,M; scanf("%d%d",&K,&M); build(1,0,M); insert(1,0,M,0,0,0,0,-1); while (q.size()){ int L=q.front(); //printf("%d: %d\n",L,dist[L]); q.pop(); insert(1,0,M,(L+K)&1,L+K-2*min(L,K),min(L+K-2*max(L+K-M,0),M),dist[L]+1,L); } if (dist[M]==inf){ puts("-1"); }else{ printf("%d\n",dist[M]); if (dist[M]<10){ int len=0,cur=M,path[20]; while (cur!=0){ path[len++]=cur; cur=pre[cur]; } vector<int> one,zero; for (int i=1;i<=M;++i){ zero.push_back(i); } while (cur!=M){ int next=path[--len]; int overlap = (cur+K-next)/2; vector<int> out; for (int i=0;i<overlap;++i){ out.push_back(one.back()); one.pop_back(); } for (int i=overlap;i<K;++i){ out.push_back(zero.back()); zero.pop_back(); } for (int i=0;i<overlap;++i){ zero.push_back(out[i]); } for (int i=overlap;i<K;++i){ one.push_back(out[i]); } sort(out.begin(),out.end()); for (int i=0;i<out.size();++i){ if (i) printf(" "); printf("%d",out[i]); } puts(""); cur=next; } } } return 0; }
void gPushKey( queue<KeyHolder> &keyQueue, KeyHolder keyHolder ) { keyQueue.push(keyHolder); }
/* Call to render the next GL frame */ void Java_cc_openframeworks_OFAndroid_render( JNIEnv* env, jclass thiz ) { if(paused || surfaceDestroyed) return; if(!threadedTouchEvents){ mutex.lock(); queue<ofTouchEventArgs> events = touchEventArgsQueue; while(!touchEventArgsQueue.empty()) touchEventArgsQueue.pop(); mutex.unlock(); while(!events.empty()){ switch(events.front().type){ case ofTouchEventArgs::down: ofNotifyMousePressed(events.front().x,events.front().y,0); ofNotifyEvent(ofEvents().touchDown,events.front()); break; case ofTouchEventArgs::up: ofNotifyMouseReleased(events.front().x,events.front().y,0); ofNotifyEvent(ofEvents().touchUp,events.front()); break; case ofTouchEventArgs::move: ofNotifyMouseMoved(events.front().x,events.front().y); ofNotifyMouseDragged(events.front().x,events.front().y,0); ofNotifyEvent(ofEvents().touchMoved,events.front()); break; case ofTouchEventArgs::doubleTap: ofNotifyEvent(ofEvents().touchDoubleTap,events.front()); break; case ofTouchEventArgs::cancel: ofNotifyEvent(ofEvents().touchCancelled,events.front()); break; } events.pop(); } } ofNotifyUpdate(); if(ofGetGLProgrammableRenderer()){ ofGetGLProgrammableRenderer()->startRender(); } int width, height; width = sWindowWidth; height = sWindowHeight; height = height > 0 ? height : 1; // set viewport, clear the screen //glViewport( 0, 0, width, height ); ofViewport(0, 0, width, height, false); // used to be glViewport( 0, 0, width, height ); float * bgPtr = ofBgColorPtr(); bool bClearAuto = ofbClearBg(); if ( bClearAuto == true || ofGetFrameNum() < 3){ ofClear(bgPtr[0]*255,bgPtr[1]*255,bgPtr[2]*255, bgPtr[3]*255); } if(bSetupScreen) ofSetupScreen(); ofNotifyDraw(); if(ofGetGLProgrammableRenderer()){ ofGetGLProgrammableRenderer()->finishRender(); } }
int reach(int a, int b) { while(!q.empty()) q.pop(); while(!x.empty()) x.pop(); q.push(a); x.push(0); memset(vis, 0, sizeof(vis)); vis[a] = true; while (!q.empty()) { int now = q.front(); q.pop(); int val = x.front(); x.pop(); for (int i = 0; i < g[now].size(); ++ i) { int next = g[now][i]; if (vis[next]) continue; vis[next] = true; int nv = val; if (next & 1) nv += w[now][i]; else nv -= w[now][i]; if (next == b) return nv; q.push(next); x.push(nv); } } return -1; }
// Push element x onto stack. void push(int x) { if (left.empty()) { left.push(x); while (!right.empty()) { left.push(right.front()); right.pop(); } } else { right.push(x); while (!left.empty()) { right.push(left.front()); left.pop(); } } }
void nextO(vector<vector<char>> &board, queue<pair<int, int>> &q, int i, int j, int m, int n) { if (i >= 0 && i < m && j >= 0 && j < n && board[i][j] == 'O') { board[i][j] = OUTER_O(); q.push(pair<int, int>(i, j)); } }
// Get the top element. int top() { return left.empty() ? right.front() : left.front(); }
// if user only inputs test, thats true, because // we add the -e, in bash thats true...? // so after we have detected the flag, char s will be // everything after that until connector or end void Test::run(queue<string>& q, bool& worked, bool brack) { // if no arguments after flag, then true // so, check it end of queue // or if a connector been detected // bool there; if (brack) { there = true; } // when ONLY "test" is entered or test and connector if (q.size() == 0 || isCon(q.front())) { cout << "(False)" << endl; worked = false; return; } if (q.front() == "]") { q.pop(); cout << "(False)" << endl; worked = false; return; } if (q.front() == "-e") { q.pop(); if (q.size() == 0 && brack) { cout << "(False)" << endl; worked = false; return; } // removed additional true if for isCon if (q.size() == 0 || isCon(q.front())) { cout << "(True)" << endl; worked = true; return; } if (q.front() == "]") { q.pop(); cout << "(True)" << endl; worked = true; return; } // keep checking string dir; while (q.size() != 0 && !(isCon(q.front()))) { // brack = false; if (q.front() == "]") { q.pop(); brack = true; } else if (q.front() == ")") { q.pop(); } else { brack = false; dir += q.front(); q.pop(); } } if (!brack && there) { cout << "(False)" << endl; worked = false; return; } // const char* s = q.front().c_str(); const char* s = dir.c_str(); if (exists(s)) { cout << "(True)" << endl; worked = true; } else { cout << "(False)" << endl; worked = false; } // q.pop(); } else if (q.front() == "-f") { q.pop(); if (q.size() == 0 && brack) { cout << "(False)" << endl; worked = false; return; } if (q.size() == 0 || isCon(q.front())) { cout << "(True)" << endl; worked = true; return; } if (q.front() == "]") { q.pop(); cout << "(True)" << endl; worked = true; return; } // keep checking string dir; while (q.size() != 0 && !(isCon(q.front()))) { // brack = false; if (q.front() == "]") { q.pop(); brack = true; } else if (q.front() == ")") { q.pop(); } else { brack = false; dir += q.front(); q.pop(); } } if (!brack && there) { cout << "(False)" << endl; worked = false; return; } const char* s = dir.c_str(); if (exists(s)) { if (isReg(s)) { cout << "(True)" << endl; worked = true; } else { cout << "(False)" << endl; worked = false; } } else { cout << "(False)" << endl; worked = false; } // q.pop(); } else if (q.front() == "-d") { q.pop(); if (q.size() == 0 && brack) { cout << "(False)" << endl; worked = false; return; } if (q.size() == 0 || isCon(q.front())) { cout << "(True)" << endl; worked = true; return; } if (q.front() == "]") { cout << "(True)" << endl; worked = true; return; } // keep checking string dir; while (q.size() != 0 && !(isCon(q.front()))) { // brack = false; if (q.front() == "]") { q.pop(); brack = true; } else if (q.front() == ")") { q.pop(); } else { brack = false; dir += q.front(); q.pop(); } } if (!brack && there) { cout << "(False)" << endl; worked = false; return; } const char* s = dir.c_str(); if (exists(s)) { if (isDir(s)) { cout << "(True)" << endl; worked = true; } else { cout << "(False)" << endl; worked = false; } } else { cout << "(False)" << endl; worked = false; } // q.pop(); } // pops til end or sees "]" else { cout << "(False)" << endl; worked = false; while (q.size() != 0 && q.front() != "]") { q.pop(); } if (q.size() != 0 && q.front() == "]") { q.pop(); } } }
void solve() { int i; toado t,tmp; long long val; for(i=0;i<nhang;i++) { gt[i][ncot-1] = a[i][ncot-1]; t.x = i; t.y = ncot-1; trc[i][ncot-1] = i; q.push(t); } while(!q.empty()) { t = q.front(); q.pop(); d[t.x][t.y] = false; if(t.y == 0) continue; for(i=3;i<6;i++) { tmp.x = t.x + h[i].x; tmp.y = t.y + h[i].y; if(tmp.x == nhang) tmp.x = 0; if(tmp.x < 0) tmp.x = nhang - 1; if(gt[tmp.x][tmp.y] > gt[t.x][t.y] + a[tmp.x][tmp.y]) { gt[tmp.x][tmp.y] = gt[t.x][t.y] + a[tmp.x][tmp.y]; trc[tmp.x][tmp.y] = t.x; if(d[tmp.x][tmp.y]) q.push(tmp); } else if(gt[tmp.x][tmp.y] == gt[t.x][t.y] + a[tmp.x][tmp.y]) if(trc[tmp.x][tmp.y] > t.x) trc[tmp.x][tmp.y] = t.x; } } val = maxgt; for(i=0;i<nhang;i++) val = gt[i][0] < val ? gt[i][0] : val; for(i=0;i<ncot;i++) kq[i] = nhang; for(i=0;i<nhang;i++) if(gt[i][0] == val) { luu[0] = i; dfs(i,0); break; } for(i=0;i<ncot;i++) { if(i) printf(" "); printf("%d",kq[i]+1); } printf("\n%lld\n",val); }
int main () { scanf("%d", &t); while (t--) { scanf("%d %d %d %d %d", &l, &h, &w, &n, &s); s /= 3; for (int k = 0; k < l; k++) { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { scanf(" %c", &tab[k][i][j]); dist[k][i][j] = INT_MAX; if (tab[k][i][j] == 'S') { aux.floor = k; aux.row = i; aux.col = j; q.push(make_pair(aux, 0)); } } } } for (int i = 0; i < n; i++) { scanf("%d %d %d %d", &aux.floor, &aux.row, &aux.col, &a); peep[i].first = aux; peep[i].second = a; } while (!q.empty()) { aux = q.front().first; a = q.front().second; q.pop(); if (aux.row >= h || aux.row < 0 || aux.col >= w || aux.col < 0 || dist[aux.floor][aux.row][aux.col] <= a || tab[aux.floor][aux.row][aux.col] == 'X') continue; dist[aux.floor][aux.row][aux.col] = a; for (int i = 0; i < 4; i++) { next = aux; if (i/2) next.row += mov[i%2]; else next.col += mov[i%2]; q.push(make_pair(next, a+1)); } if (tab[aux.floor][aux.row][aux.col] == '.' || !a) continue; next = aux; if (tab[aux.floor][aux.row][aux.col] == 'U') next.floor++; else if (tab[aux.floor][aux.row][aux.col] == 'D') next.floor--; q.push(make_pair(next, a+1)); } for (int i = 0; i < n; i++) for (int j = 0; j <= s; j++) memo[i][j] = -1; // for (int i = 0; i < n; i++) printf("(%d,%d) ", dist[peep[i].first.floor-1][peep[i].first.row-1][peep[i].first.col-1], peep[i].second); // printf("\n"); // for (int i = 0; i < n; i++) printf("(%d,%d) ", ks[i].first, ks[i].second); printf("%d\n", pd(n-1, s)); } }
int main(){ int startX, startY; //freopen("test/3442.txt","r",stdin); while(1){ scanf(" %d",&n); if(n == 0){ return 0; } for(int i=0; i<n; i++){ scanf("%d %d",&p1[i].j,&p1[i].i); p2[i].i = p1[i].i; p2[i].j = p1[i].j; } startX = p1[0].i; startY = p1[0].j; sort(p1, p1+n, comp1); sort(p2, p2+n, comp2); //x축과 평행한 직선 연결 for(int i=0; i<n/2; i++) { line[i].a = p1[2*i]; line[i].b = p1[2*i+1]; line[i].chk = false; } //y축과 평핸한 직선 연결 for(int i=0; i<n/2; i++) { line[i+n/2].a = p2[2*i]; line[i+n/2].b = p2[2*i+1]; line[i+n/2].chk = false; } int x1,y1,x2,y2,x3,y3; //출발점 찾기 char dir = 'N'; x1=p2[0].i; y1=p2[0].j; x2=p2[1].i; y2=p2[1].j; line[n/2].chk = true; q.push(Dir(x1,y1,dir)); while(!(p2[0].i==x2 && p2[0].j==y2)){ findLine(x2,y2,x3,y3); dir = getDir(dir,ccw(x1,y1,x2,y2,x3,y3)); q.push(Dir(x2,y2,dir)); x1=x2, y1=y2; x2=x3, y2=y3; } while(!(q.front().x==startX && q.front().y==startY)){ q.push(q.front()); q.pop(); } while(!q.empty()){ printf("%c",q.front().dir); q.pop(); } printf("\n"); } return 0; }
int main(int argc, char** argv) { for (int i = 0; i < 1002; i++) for (int j = 0; j < 1002; j++) map[i][j] = -1; cin >> C; cin >> R; for(int i=1; i<=R; i++) for(int j=1; j<=C; j++){ cin >> map[i][j]; if(map[i][j]==1) { Q.push(i*1001+j); //printf("첫 큐에추가 %d\n",i*100+j); } } /* for(int i=0; i<=R+1; i++){ for(int j=0; j<=C+1; j++){ printf("%d.",map[i][j]); } printf("\n" ); } */ //is ripe all? if(isRipeAll()) printf("0"); //탐색 int time; int row_cord[4] = {-1,1,0,0}; // 상수 변하면 안되는 값 int col_cord[4] = {0,0,-1,1}; while(!Q.empty()){ //printf("현재 위치는 %d\n",Q.front()); int curX = Q.front()/1001; int curY = Q.front()%1001; Q.pop(); //4방향 탐색 for( int d=0; d<4; d++){ int nx = curX + row_cord[d]; int ny = curY + col_cord[d]; if(map[nx][ny]==0){ Q.push(nx*1001+ny); map[nx][ny]=map[curX][curY]+1; time = map[nx][ny]; //printf("탐색후 큐에추가 %d\n",nx*1000+ny); } } } if(isRipeAll()) printf("%d",time-1); else printf("-1"); return 0; }
namespace MaxFlow { const int MAX_NODES = 1005; int SRC = 0, DEST = 1; int nodes = 2; int F[MAX_NODES][MAX_NODES], C[MAX_NODES][MAX_NODES]; int Parent[MAX_NODES]; queue<int> Q; vector<int> G[MAX_NODES]; void init(int n, int s = -1, int d = -1) { if(s == -1) s = ++n; if(d == -1) d = ++n; SRC = s; DEST = d; nodes = n; for(int i=1; i<=nodes; i++) { for(auto vec : G[i]) { C[i][vec] = F[i][vec] = 0; } G[i].clear(); } } void addEdge(int a, int b, int cap) { G[a].push_back(b); G[b].push_back(a); C[a][b] += cap; } bool bfs() { memset(Parent, 0, sizeof(Parent)); Parent[SRC] = -1; Q.push(SRC); while(!Q.empty()) { int node = Q.front(); Q.pop(); for(auto vec : G[node]) { if(!Parent[vec] && F[node][vec] < C[node][vec]) { Parent[vec] = node; Q.push(vec); } } } return Parent[DEST] != 0; } int maxFlow() { int flow = 0; while(bfs()) { for(auto x : G[DEST]) { if(Parent[x] == 0) continue; int M = C[x][DEST] - F[x][DEST]; for(int node = x; node != SRC; node = Parent[node]) { M = min(M, C[Parent[node]][node] - F[Parent[node]][node]); } F[x][DEST] += M; F[DEST][x] -= M; for(int node = x; node != SRC; node = Parent[node]) { F[Parent[node]][node] += M; F[node][Parent[node]] -= M; } flow += M; } } return flow; } };
void pop() //出栈: 在有元素的队列中,从头出队列,尾插入到空队列中,留最后一个元素pop { if (!q1.empty()) { while (!q1.empty()) { if (q1.size() > 1) { q2.push(q1.front()); //最后一个元素不在push } q1.pop(); } } else { while (!q2.empty()) { if (q2.size() > 1) { q1.push(q2.front()); //最后一个元素不在push } q2.pop(); } } }
void BFS() { while (!Q.empty()) Q.pop(); memset(visit,false,sizeof(visit)); Q.push(node(xa,ya,za,da,ra,"")); visit[xa][ya][za][da][ra] = true; while (!Q.empty()) { now = Q.front(); Q.pop(); if (mp[now.x][now.y][now.z] == true) continue; //cout << now.x << ' '<< now.y << ' ' << now.z << ' '<< now.r << ' '<< now.d << endl; if (now.x == xb && now.y == yb && now.z == zb && now.r == rb && now.d == db) { printf("%d\n",now.step.size()); printf("%s\n",now.step.c_str()); return; } updata = doF(now); updata.step = now.step+"F"; if (CheckIn(updata.x,updata.y,updata.z) == true) { if (visit[updata.x][updata.y][updata.z][updata.d][updata.r] == false) { visit[updata.x][updata.y][updata.z][updata.d][updata.r] = true; Q.push(updata); } } updata = doL(now); updata.step = now.step+"L"; if (CheckIn(updata.x,updata.y,updata.z) == true) { if (visit[updata.x][updata.y][updata.z][updata.d][updata.r] == false) { visit[updata.x][updata.y][updata.z][updata.d][updata.r] = true; Q.push(updata); } } updata = doL(doL(doL(now))); updata.step = now.step+"R"; if (CheckIn(updata.x,updata.y,updata.z) == true) { if (visit[updata.x][updata.y][updata.z][updata.d][updata.r] == false) { visit[updata.x][updata.y][updata.z][updata.d][updata.r] = true; Q.push(updata); } } updata = doU(now); updata.step = now.step+"U"; if (CheckIn(updata.x,updata.y,updata.z) == true) { if (visit[updata.x][updata.y][updata.z][updata.d][updata.r] == false) { visit[updata.x][updata.y][updata.z][updata.d][updata.r] = true; Q.push(updata); } } updata = doU(doU(doU(now))); updata.step = now.step+"D"; if (CheckIn(updata.x,updata.y,updata.z) == true) { if (visit[updata.x][updata.y][updata.z][updata.d][updata.r] == false) { visit[updata.x][updata.y][updata.z][updata.d][updata.r] = true; Q.push(updata); } } updata = doB(now); updata.step = now.step+"B"; if (CheckIn(updata.x,updata.y,updata.z) == true) { if (visit[updata.x][updata.y][updata.z][updata.d][updata.r] == false) { visit[updata.x][updata.y][updata.z][updata.d][updata.r] = true; Q.push(updata); } } } printf("Sorry, I can't get there.\n"); }
// Push element x onto stack. void push(int x) { q1.push(x); size++; }