草,WA了
原题链接:4862.坠入二维虚空!2023-08-15 17:11:34
发布于:河北
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;
// 多维坐标点结构体
struct Coordinate {
vector<int> coord;
};
// 定义运动方向
const vector<vector<int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
// 判断坐标是否越界
bool isOutOfBound(const vector<int>& coord, const vector<int>& size) {
for (int i = 0; i < coord.size(); i++) {
if (coord[i] < 0 || coord[i] >= size[i]) {
return true;
}
}
return false;
}
// 计算从起点到终点的最短路径长度
int shortestPath(const vector<vector<vector<int>>>& A, const vector<int>& size) {
queue<Coordinate> q;
vector<vector<vector<int>>> dist(size, vector<vector<int>>(size, vector<int>(size, INT_MAX)));
// 起点为0
Coordinate start;
start.coord = vector<int>(size.size(), 0);
dist[0][0][0] = 0;
q.push(start);
while (!q.empty()) {
Coordinate cur = q.front();
q.pop();
for (const vector<int>& dir : directions) {
Coordinate next = cur;
for (int i = 0; i < cur.coord.size(); i++) {
next.coord[i] += dir[i];
}
if (!isOutOfBound(next.coord, size)) {
int x = next.coord[0], y = next.coord[1], z = next.coord[2];
if (dist[x][y][z] == INT_MAX) {
dist[x][y][z] = dist[cur.coord[0]][cur.coord[1]][cur.coord[2]] + A[x][y][z];
q.push(next);
}
}
}
}
return dist[size[0] - 1][size[1] - 1][size[2] - 1];
}
int main() {
int n;
cin >> n;
vector<int> size(n);
for (int i = 0; i < n; i++) {
cin >> size[i];
}
vector<vector<vector<int>>> A(size[0], vector<vector<int>>(size[1], vector<int>(size[2])));
for (int i = 0; i < size[0]; i++) {
for (int j = 0; j < size[1]; j++) {
for (int k = 0; k < size[2]; k++) {
cin >> A[i][j][k];
}
}
}
int shortest = shortestPath(A, size);
cout << shortest << endl;
return 0;
}
这里空空如也
有帮助,赞一个