向我77分的初赛致敬
2025-02-08 18:14:43
发布于:江苏
18阅读
0回复
0点赞
#include <iostream>
#include <vector>
#include <string>
#include <set>
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
bool isValid(int x, int y, int n, int m, const std::vector<std::string>& map) {
return x >= 1 && x <= n && y >= 1 && y <= m && map[x - 1][y - 1] == '.';
}
int bfs(int n, int m, int k, int x0, int y0, int d0, const std::vector<std::string>& map) {
int x = x0, y = y0, d = d0;
std::set<std::pair<int, int>> visited;
visited.insert({x, y});
for (int i = 0; i < k; ++i) {
int nx = x + dx[d];
int ny = y + dy[d];
if (isValid(nx, ny, n, m, map)) {
x = nx;
y = ny;
} else {
d = (d + 1) % 4;
}
visited.insert({x, y});
}
return visited.size();
}
int main() {
int T;
std::cin >> T;
while (T--) {
int n, m, k;
std::cin >> n >> m >> k;
int x0, y0, d0;
std::cin >> x0 >> y0 >> d0;
std::vector<std::string> map(n);
for (int i = 0; i < n; ++i) {
std::cin >> map[i];
}
int result = bfs(n, m, k, x0, y0, d0, map);
std::cout << result << std::endl;
}
return 0;
}
这里空空如也
有帮助,赞一个