这是我对此问题陈述的解决方案: http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1842
我遇到运行时错误,但是当我使用下面的调试语句时,程序会给出正确的输出。
while(!q.empty()){
cn=0;
while(q.front().first<=ct && cn <=n && q.front().second==cpos){
allCarT.push_back(ct+t);
for(i=0; i< sz(allCarT); i++) cout << allCarT[i] << "\n"; //debugging statement give correct answers
q.pop(); //error occurs here in the last loop
cn++;
}
if(cn==0 && q.front().first > ct){
ct = q.front().first;
}else{
ct+=t;
if(cpos=="left") cpos="right";
else cpos="left";
}
}
完整解决方案:https://ideone.com/BCU3UT
答案 0 :(得分:0)
在该代码中,您不会检查该队列是否为空:
while(q.front().first<=ct && cn <=n && q.front().second==cpos){
allCarT.push_back(ct+t);
q.pop(); //error occurs here in the last loop
// queue can be empty now, so you have out of bound access with q.front()
cn++;
}