#include#include#include#includeusing namespace std;int main(){int n;cin>>n;int i, j;char maze[n][n];for(i=0;istring s;cin>>s;for(j=0;jmaze[i][j]=s[j];}}//印出地圖// for(i=0;i// for(j=0;j// cout<// } cout<// }int x,y,temp;queue x_queue,y_queue;queue ans;x_queue.push(1);y_queue.push(1);ans.push(1);while(true){x=x_queue.front();y=y_queue.front();x_queue.pop();y_queue.pop();temp=ans.front();temp++;ans.pop();// cout<// maze[x][y]='t';if(x==n-2 && y==n-2){cout<break;}if(maze[x+1][y]=='.'){x_queue.push(x+1);y_queue.push(y);ans.push(temp);}if(maze[x][y+1]=='.'){x_queue.push(x);y_queue.push(y+1);ans.push(temp);}if(maze[x-1][y]=='.'){x_queue.push(x-1);y_queue.push(y);ans.push(temp);}if(maze[x][y-1]=='.'){x_queue.push(x);y_queue.push(y-1);ans.push(temp);}if(x_queue.empty()){break;}// cout<}// for(i=0;i// for(j=0;j// cout<// }// cout<// }}有沒有大神能教我一下,我用三個queue一個紀錄x座標,一個紀錄y座標, 嘗試利用BFS的方式,最後第三個queue去紀錄走到第幾步,為甚麼這樣會25% RE,看起來錯誤好像是跟記憶體有關的,請問有人知道為甚麼嗎
喔沒事了感謝,我找到問題了
#include#include#include#includeusing namespace std;int main(){int n;cin>>n;int i, j;char maze[n][n];for(i=0;istring s;cin>>s;for(j=0;jmaze[i][j]=s[j];}}//印出地圖// for(i=0;i// for(j=0;j// cout<// } cout<// }int x,y,temp;queue x_queue,y_queue;queue ans;x_queue.push(1);y_queue.push(1);ans.push(1);while(true){x=x_queue.front();y=y_queue.front();x_queue.pop();y_queue.pop();temp=ans.front();temp++;ans.pop();// cout<// maze[x][y]='t';if(x==n-2 && y==n-2){cout<break;}if(maze[x+1][y]=='.'){x_queue.push(x+1);y_queue.push(y);ans.push(temp);}if(maze[x][y+1]=='.'){x_queue.push(x);y_queue.push(y+1);ans.push(temp);}if(maze[x-1][y]=='.'){x_queue.push(x-1);y_queue.push(y);ans.push(temp);}if(maze[x][y-1]=='.'){x_queue.push(x);y_queue.push(y-1);ans.push(temp);}if(x_queue.empty()){break;}// cout<}// for(i=0;i// for(j=0;j// cout<// }// cout<// }}有沒有大神能教我一下,我用三個queue一個紀錄x座標,一個紀錄y座標, 嘗試利用BFS的方式,最後第三個queue去紀錄走到第幾步,為甚麼這樣會25% RE,看起來錯誤好像是跟記憶體有關的,請問有人知道為甚麼嗎
如果到不了終點,你的程式會無限迴圈,記憶體越吃越多