为啥用stl的queue会卡runtime error?
查看原帖
为啥用stl的queue会卡runtime error?
1106997
guuuuyo楼主2024/8/9 22:02
#include<bits/stdc++.h>
using namespace std;

const int N=309;
int n, m[N][N], x, y, t, dx[]={0, 0, 1, -1}, dy[]={1, -1, 0, 0}, mp[N][N];
struct node {
    int x, y;
};

int cnt=0;
void bfs() {
    queue<node> q;
    q.push({0,0});
    while (!q.empty() && cnt<1000) {
        int x=q.front().x, y=q.front().y;
        
        for (int i=0; i<4; i++) {
            int xx=x+dx[i], yy=y+dy[i];
            if (xx < 0 || xx > 307 || y < 0 || y > 307) continue;
            if ((mp[yy][xx] > m[y][x]+1) && m[yy][xx]>=0x3f3f3f3f) {
                m[yy][xx]=m[y][x]+1;
                q.push({xx, yy});
                if (mp[yy][xx]>=0x3f3f3f3f) {
                    cout << m[yy][xx];
                    return;
                }
            }
        }
        q.pop();
        cnt++;
    }
    
    cout << -1;
}

int main() {
    for (int i=0; i<N; i++) {
        for (int j=0; j<N; j++) {
            m[i][j]=mp[i][j]=0x3f3f3f3f;
        }
    }

	cin >> n;
	for (int i=0; i<n; i++) {
	    cin >> x >> y >> t;
	    mp[y][x]=min(t, mp[y][x]);
	    for (int j=0; j<4; j++) {
	        int xx=x+dx[j], yy=y+dy[j];
	        if (xx < 0 || xx > 307 || y < 0 || y > 307) continue;
	        mp[yy][xx]=min(t, mp[yy][xx]);
	    }
	}

	m[0][0]=0;
	bfs();
	
// 	for (int i=0; i<n; i++) {
//         for (int j=0; j<n; j++) {
//             cout << m[i][j] << ' ';
//         } cout << endl;
//     } cout << endl;
//     for (int i=0; i<n; i++) {
//         for (int j=0; j<n; j++) {
//             cout << mp[i][j] << ' ';
//         } cout << endl;
//     }
	return 0;
}
2024/8/9 22:02
加载中...