#36023: 為什麼會RE


zoosewu (lion)

學校 : 臺北市立中正高級中學
編號 : 114560
來源 : [36.229.231.77]
最後登入時間 :
2024-03-28 23:32:26
a982. 迷宮問題#1 | From: [123.194.85.227] | 發表日期 : 2023-06-30 19:47

#include<iostream>
#include<queue>
#include<stack>
#include <utility>
using namespace std;
int main(){
int n;
cin>>n;
int i, j; 
char maze[n][n];
for(i=0;i<n;i++){
string s;
cin>>s;
for(j=0;j<n;j++){
maze[i][j]=s[j];
}
 
}
//印出地圖 
 
// for(i=0;i<n;i++){
// for(j=0;j<n;j++){
// cout<<maze[i][j];
// } cout<<endl;
// }
 
int x,y,temp;
queue<int> x_queue,y_queue;
queue<int> 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<<x<<" "<<y<<" ";
// maze[x][y]='t';
if(x==n-2 && y==n-2){
cout<<temp-1;
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<<ans.front()<<endl;
}
// for(i=0;i<n;i++){
// for(j=0;j<n;j++){
// cout<<maze[i][j];
// }
// cout<<endl;
// }
 
有沒有大神能教我一下,我用三個queue一個紀錄x座標,一個紀錄y座標, 嘗試利用BFS的方式,最後第三個queue去紀錄走到第幾步,為甚麼這樣會25% RE,看起來錯誤好像是跟記憶體有關的,請問有人知道為甚麼嗎
 
#36026: Re: 為什麼會RE


zoosewu (lion)

學校 : 臺北市立中正高級中學
編號 : 114560
來源 : [36.229.231.77]
最後登入時間 :
2024-03-28 23:32:26
a982. 迷宮問題#1 | From: [123.194.85.227] | 發表日期 : 2023-06-30 22:04

#include
#include
#include
#include
using namespace std;
int main(){
int n;
cin>>n;
int i, j; 
char maze[n][n];
for(i=0;i
string s;
cin>>s;
for(j=0;j
maze[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,看起來錯誤好像是跟記憶體有關的,請問有人知道為甚麼嗎


喔沒事了感謝,我找到問題了

 
#36027: Re: 為什麼會RE


cges30901 (cges30901)

學校 : 不指定學校
編號 : 30877
來源 : [101.136.203.77]
最後登入時間 :
2024-04-07 15:34:14
a982. 迷宮問題#1 | From: [59.115.44.241] | 發表日期 : 2023-06-30 22:10

#include
#include
#include
#include
using namespace std;
int main(){
int n;
cin>>n;
int i, j; 
char maze[n][n];
for(i=0;i
string s;
cin>>s;
for(j=0;j
maze[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,看起來錯誤好像是跟記憶體有關的,請問有人知道為甚麼嗎


如果到不了終點,你的程式會無限迴圈,記憶體越吃越多

 
ZeroJudge Forum