#32789: 為甚麼這樣會RE?


s010013@stmail.hhsh.chc.edu.tw (Smith)

學校 : 國立溪湖高級中學
編號 : 174920
來源 : [27.53.32.54]
最後登入時間 :
2022-10-10 09:31:00
d793. 00929 - Number Maze -- UVa929 | From: [27.53.64.84] | 發表日期 : 2022-11-06 23:55

如題所述,我丟到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
    }
}

 
#33116: Re: 為甚麼這樣會RE?


luray0601@gmail.com (QWERTYPIG)

學校 : 臺北市私立復興實驗高級中學
編號 : 139334
來源 : [36.226.26.217]
最後登入時間 :
2023-03-24 20:42:11
d793. 00929 - Number Maze -- UVa929 | From: [1.169.126.231] | 發表日期 : 2022-12-02 22:30

如題所述,我丟到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不知道如何對他排序?

另外我在本機測也跑不動

 
ZeroJudge Forum