有点实力啊,全PE了,礼堂钉针救命
2023-08-20 11:50:12
发布于:广东
13阅读
0回复
0点赞
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, x, y, dir;
cin >> n >> x >> y >> dir;
vector<vector<int>> matrix(n, vector<int>(n, 0));
vector<pair<int, int>> directions;
if (dir == 1) {
directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
} else {
directions = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
}
int num = 1; // 当前填入的数值
int curX = x - 1, curY = y - 1;
int directionIndex = 0;
while (true) {
matrix[curX][curY] = num;
int nextX = curX + directions[directionIndex].first;
int nextY = curY + directions[directionIndex].second;
if (nextX >= 0 && nextX < n && nextY >= 0 && nextY < n && matrix[nextX][nextY] == 0) {
curX = nextX;
curY = nextY;
num++;
} else {
// 转向到下一个方向
directionIndex = (directionIndex + 1) % 4;
nextX = curX + directions[directionIndex].first;
nextY = curY + directions[directionIndex].second;
if (!(nextX >= 0 && nextX < n && nextY >= 0 && nextY < n && matrix[nextX][nextY] == 0)) {
break;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout.width(3);
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
全部评论 2
666
2023-08-23 来自 广东
1你这个题测试点可能出问题了
2023-08-20 来自 广东
1
有帮助,赞一个