#26506: RE (SIGABRT)


qawl987 (中央地板好滑)

學校 : 不指定學校
編號 : 149523
來源 : [140.115.50.44]
最後登入時間 :
2021-10-05 20:06:04
a982. 迷宮問題#1 | From: [1.161.80.123] | 發表日期 : 2021-08-10 16:40

#1: 25% RE (SIGABRT)

系統呼叫了 abort 函式!
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted (core dumped)
上網查了之後有看到說queue如果為空的話pop()會有問題,但是改掉之後還是一樣求解,感謝
以下是我的程式碼
#include <iostream> #include <queue> using namespace std; struct pos { int x,y,step; }; int main() { int n,dir[4][2]= {{0,-1},{1,0},{0,1},{-1,0}},fi_step=0; bool maze[100][100]; cin>>n; char c; for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { cin>>c; if(c=='#')maze[i][j] =false; else maze[i][j] = true; } } queue<pos> q; pos start = {1,1,1}; q.push(start); bool flag =false; while(q.size()>0 && !flag) { int xx=0,yy=0,steep=0; for(int i=0; i<4; i++) { xx = q.front().x + dir[i][0]; yy = q.front().y + dir[i][1]; steep = q.front().step; if(xx==n-2 && yy==n-2) { flag =true; steep++; fi_step = steep; break; } else if(maze[xx][yy]==true) { q.push({xx,yy,steep+1}); } } if(!flag&& q.size()>0) { q.pop(); } } if(!flag)cout<<"No solution!"<<'\n'; else cout<<fi_step<<'\n'; }

 

 
#26792: Re:RE (SIGABRT)


cges30901 (cges30901)

學校 : 不指定學校
編號 : 30877
來源 : [101.136.203.77]
最後登入時間 :
2024-04-07 15:34:14
a982. 迷宮問題#1 | From: [110.28.135.31] | 發表日期 : 2021-08-25 09:11

#1: 25% RE (SIGABRT)

系統呼叫了 abort 函式!
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted (core dumped)
上網查了之後有看到說queue如果為空的話pop()會有問題,但是改掉之後還是一樣求解,感謝
以下是我的程式碼
#include #include using namespace std; struct pos { int x,y,step; }; int main() { int n,dir[4][2]= {{0,-1},{1,0},{0,1},{-1,0}},fi_step=0; bool maze[100][100]; cin>>n; char c; for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { cin>>c; if(c=='#')maze[i][j] =false; else maze[i][j] = true; } } queue q; pos start = {1,1,1}; q.push(start); bool flag =false; while(q.size()>0 && !flag) { int xx=0,yy=0,steep=0; for(int i=0; i<4; i++) { xx = q.front().x + dir[i][0]; yy = q.front().y + dir[i][1]; steep = q.front().step; if(xx==n-2 && yy==n-2) { flag =true; steep++; fi_step = steep; break; } else if(maze[xx][yy]==true) { q.push({xx,yy,steep+1}); } } if(!flag&& q.size()>0) { q.pop(); } } if(!flag)cout<<"No solution!"<<'\n'; else cout<<fi_step<<'\n'; }

 

大概是因為會出現無限迴圈,記憶體被吃光了...

你可以用這個測試看看:

9
#########
#.......#
#.#####.#
#.......#
#########
#..#.#..#
#.##.##.#
#.......#
#########
 
ZeroJudge Forum