P11228 [CSP-J 2024] 地图探险(民间数据)
C++#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e3 + 5;
char c[N][N];
ll vis[N][N];
ll dx[4] = {0, 1, 0, -1},dy[4] = {1, 0, -1, 0};
void solve()
{
ll n, m, k, x, y, d, ans = 1;
cin >> n >> m >> k;
cin >> x >> y >> d;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> c[i][j];
memset(vis,0,sizeof vis) ;//注意事项1:vis数组每次需要重置
vis[x][y] = 1;//防止机器人绕一圈回到起点以为没来过
while (k--)
{
ll nx = x + dx[d], ny = y + dy[d];
if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && c[nx][ny] == '.')//
{
if (vis[nx][ny] == 0)//如果能走,判断是否来过
{
ans++;
vis[nx][ny] = 1;
}
x = nx,y = ny;
}
else
d = (d + 1) % 4; //如果不能走,向右转
}
cout << ans << '\n';
}
int main()
{
ll t;
cin >> t;
while(t--) solve();
return 0;
}
好难的题