[CSP-J 2024] 地图探险
2024年12月25日小于 1 分钟
#include <bits/stdc++.h>
using namespace std;
int T;
int n, m, k;
char g[1005][1005];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
bool vis[1005][1005];
void dfs(int x, int y, int d, int step)
{
vis[x][y] = true;
if (step == k)
return;
int xx = x + dx[d];
int yy = y + dy[d];
if (1 <= xx && xx <= n && 1 <= yy && yy <= m &&
g[xx][yy] != 'x')
dfs(xx, yy, d, step + 1);
else
{
d = (d + 1) % 4;
dfs(x, y, d, step + 1);
}
}
int main()
{
cin >> T;
while (T--)
{
int x, y, d;
cin >> n >> m >> k;
cin >> x >> y >> d;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
{
cin >> g[i][j];
vis[i][j] = false;
}
dfs(x, y, d, 0);
int ans = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
ans += vis[i][j];
cout << ans << "\n";
}
return 0;
}