如題所述,我丟到virtual judge上拿了Accepted,結果丟在這就變成RE。但是我看界線沒設錯,也有把一些該初始化的東西初始化,不知道為甚麼還會遇到RE。有大神能指點我一下嗎?
我的程式碼如下
#include <iostream>
#include <climits>
#include <queue>
#include <vector>
#define INF INT_MAX / 2
using namespace std;
struct spot{
int X;//column
int Y;//row
int dis;//distance
bool operator < (spot other) const
{
return this->dis > other.dis;
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
priority_queue<spot> pq;
int N; cin >> N;
while(N--){
int n, m, tmp;
cin >> n >> m;
bool visited[n][m] = {};
spot sp;
int x[] = {1,0,-1,0};
int y[] = {0,1,0,-1};
int graph[n][m];//graph[y][x] = weight(x,y)
int d[n][m];//d[y][x] = distance((0,0) -> (x,y))
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> tmp;
graph[i][j] = tmp;
}
}
for(auto& a : d)
for(auto& b : a)
b = INF;
d[0][0] = graph[0][0];
sp = {0,0,d[0][0]};
pq.push(sp);
while(!pq.empty()){
spot mid = pq.top();//set the middle node
pq.pop();
for(int i = 0; i < 4; i++){
int x2 = mid.X + x[i];//x(new spot)
int y2 = mid.Y + y[i];//y(new spot)
if(x2 >= 0 and x2 < m and y2 >= 0 and y2 < n){
if(!visited[y2][x2]){
visited[y2][x2] = true;
d[y2][x2] = d[mid.Y][mid.X] + graph[y2][x2];
sp = {x2,y2,d[y2][x2]};
pq.push(sp);
}
}
}
if(visited[n-1][m-1]){//check whether the end spot has been visited
cout << d[n-1][m-1] << '\n';
break;
}
}
pq = priority_queue<spot>();//clear the pq
}
}
如題所述,我丟到virtual judge上拿了Accepted,結果丟在這就變成RE。但是我看界線沒設錯,也有把一些該初始化的東西初始化,不知道為甚麼還會遇到RE。有大神能指點我一下嗎?
我的程式碼如下
#include
#include
#include
#include
#define INF INT_MAX / 2
using namespace std;
struct spot{
int X;//column
int Y;//row
int dis;//distance
bool operator < (spot other) const
{
return this->dis > other.dis;
}
};int main(){
ios::sync_with_stdio(false);
cin.tie(0);
priority_queue pq;
int N; cin >> N;
while(N--){
int n, m, tmp;
cin >> n >> m;
bool visited[n][m] = {};
spot sp;
int x[] = {1,0,-1,0};
int y[] = {0,1,0,-1};
int graph[n][m];//graph[y][x] = weight(x,y)
int d[n][m];//d[y][x] = distance((0,0) -> (x,y))
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> tmp;
graph[i][j] = tmp;
}
}
for(auto& a : d)
for(auto& b : a)
b = INF;
d[0][0] = graph[0][0];
sp = {0,0,d[0][0]};
pq.push(sp);
while(!pq.empty()){
spot mid = pq.top();//set the middle node
pq.pop();
for(int i = 0; i < 4; i++){
int x2 = mid.X + x[i];//x(new spot)
int y2 = mid.Y + y[i];//y(new spot)
if(x2 >= 0 and x2 < m and y2 >= 0 and y2 < n){
if(!visited[y2][x2]){
visited[y2][x2] = true;
d[y2][x2] = d[mid.Y][mid.X] + graph[y2][x2];
sp = {x2,y2,d[y2][x2]};
pq.push(sp);
}
}
}
if(visited[n-1][m-1]){//check whether the end spot has been visited
cout << d[n-1][m-1] << '\n';
break;
}
}
pq = priority_queue();//clear the pq
}
}
會不會是priority_queue初始化的問題?spot是自定義的,或許priority_queue不知道如何對他排序?
另外我在本機測也跑不動