题解(欢迎指正)
2025-08-20 18:15:09
发布于:浙江
13阅读
0回复
0点赞
这道题的数据量不大,可以通过while (n--)套while (k--)的方式解决,在通过定义两个方向数组,一个标记函数来标记访问过的坐标来防止重复计算,以及一个检查函数检查下个坐标是否合法来完成程序。
以下是我的代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 5; // 最大1000,开1005防止数组炸了
int dx[9] = {0, 1, 0, -1};
int dy[9] = {1, 0, -1, 0}; // 方向数组
char mp[maxn][maxn]; // 地图
int t, n, m, k, cnt = 0, x, y, d;
bool check (int xx, int yy) { // 检查坐标是否合法
if (xx >= 1 && xx <= n && yy >= 1 && yy <= m && mp[xx][yy] != 'x') {
return true;
}
return false;
}
int main () {
cin >> t;
while (t--) {
cnt = 0;
bool vis[maxn][maxn] = {false}; // 标记,标记过的坐标不再记录
cin >> n >> m >> k >> x >> y >> d;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> mp[i][j];
}
}
cnt++;
vis[x][y] = true;
while (k--) {
if (check(x + dx[d], y + dy[d])) {
x = x + dx[d]; // 以后我再多打int我就是史
y = y + dy[d]; // 可恶的int,害我找了1个小时的bug,找来找去都是对的。。。
if (vis[x][y] == false) {
cnt++;
vis[x][y] = true;
}
}
else {
d = (d + 1) % 4;
}
}
cout << cnt << "\n";
}
return 0;
}
全部评论 1
666,盐都不盐了
2025-08-20 来自 浙江
0那咋了
2025-08-20 来自 浙江
0
有帮助,赞一个