题目描述Who am I What am I Why am I These are all difficult questions that have kept philosophers reliably busy over the past millennia But when it comes to Where am I then well modern smartphones and GPS
思路:
- 首先,我们需要遍历所有可能的起始位置。
- 对于每个起始位置,我们按照顺时针螺旋的方式探索网格,直到找到一个标记点。在探索过程中,我们记录经过的步数。
- 对于每个起始位置,我们重复步骤2多次,然后求平均步数,即为期望步数。
- 我们还需要记录最大步数和对应的起始位置。
代码如下:
#include
int main() {
int dx, dy;
cin >> dx >> dy;
vector
int maxSteps = 0;
vector<pair<int, int>> maxStartPos;
int totalSteps = 0;
int numTrials = 1000; // 进行1000次试验
for (int trial = 0; trial < numTrials; trial++) {
int steps = 0;
for (int x = 0; x < dx; x++) {
for (int y = 0; y < dy; y++) {
int curX = x, curY = y;
bool found = false;
while (!found) {
if (grid[curY][curX] == 'X') {
found = true;
} else {
steps++;
if (curX % 2 == 0) {
if (curY == dy - 1 || grid[curY + 1][curX] != '.') {
curX++;
} else {
curY++;
}
} else {
if (curY == 0 || grid[curY - 1][curX] != '.') {
curX++;
} else {
curY--;
}
}
}
}
}
}
totalSteps += steps;
if (steps > maxSteps) {
maxSteps = steps;
maxStartPos.clear();
maxStartPos.push_back(make_pair(x, y));
} else if (steps == maxSteps) {
maxStartPos.push_back(make_pair(x, y));
}
}
double expectedSteps = totalSteps / (double) numTrials;
cout << expectedSteps << endl;
cout << maxSteps << endl;
sort(maxStartPos.begin(), maxStartPos.end());
for (auto startPos : maxStartPos) {
cout << startPos.first << " " << startPos.second << endl;
}
return 0;
AI 优化版
原文地址:https://www.cveoy.top/t/topic/hS1i 著作权归作者所有。请勿转载和采集!