题解:P11228 [CSP-J 2024] 地图探险

· · 题解

题意

给一个起始点和初始方向,行动 k 次。如果前方在地图内且为空地,往前走;否则右转(方向++)。问总共走了多少个格子?

PS:右转算一次行动。

思路

# 代码 ```cpp line-numbers #include <bits/stdc++.h> using namespace std; const int N = 1e3 + 5; int dx[] = {0,1,0,-1},dy[] = {1,0,-1,0}; bool a[N][N]; set<pair<int,int>> f; int main(int argc, char **argv){ // freopen("explore.in","r",stdin); // freopen("explore.out","w",stdout); int t; cin >> t; while (t--){ memset(a,0,sizeof a); f.clear(); int n,m,k; int x,y,d; cin >> n >> m >> k; cin >> x >> y >> d; f.insert({x,y}); for (int i = 1;i <= n;i++){ string s; cin >> s; for (int j = 1;j <= m;j++){ a[i][j] = s[j - 1] == '.'?1:0; } } while (k--){ int xi = x + dx[d],yi = y + dy[d]; if (xi > 0 && xi <= n && yi > 0 && yi <= m && a[xi][yi]) x = xi,y = yi,f.insert({x,y}); else d++,d %= 4; } printf("%d\n",f.size()); } return 0; } ```