Test instructions: Given the matrix of the N*m, a robot from a position, start to walk, if encountered * or boundary, clockwise rotation, and then go, ask you the last robot can pass the maximum number of squares.
Analysis: The problem is mainly test instructions reading is not good, WA several times, the first is in the * or border to turn, followed by the place can walk, note that two points, you can AC, you can use DFS, can also use BFS,
I used the DFS.
The code is as follows:
#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <cstdio> #include <string> #include < cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include < queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include < Stack>using namespace std; typedef long Long Ll;typedef pair<int, int> p;const int INF = 0x3f3f3f3f;const double inf = 0x3f3f3f3f3f3f;const Double PI = ACOs ( -1.0); const double EPS = 1e-8;const int maxn = ten + 5;const int mod = 1e9 + 7; const char *mark = "+-*"; const int dr[] = {-1, 0, 1, 0};const int dc[] = {0, 1, 0, -1};int N, m;inline bool is_in (int R, I NT c) {return R >= 0 && r < n && C >= 0 && C < m;} Char s[15][15];int vis[15][15];int cnt;void dfs (int r, int c, int ok) {if (s[r][c] = = ' * ' | | cnt >) return; ++cnt; for (int i = 0; i < 4; ++i) {int x = R + dr[(I+ok)%4]; int y = c + dc[(I+ok)%4]; if (is_in (x, y) && s[x][y]! = ' * ') {vis[x][y] = 1; DFS (x, y, (ok+i)%4); return; }} return; int main () {while (scanf ("%d%d", &n, &m) = = 2) {cnt = 0; memset (s, 0, sizeof (s)); memset (Vis, 0, sizeof (VIS)); for (int i = 0; i < n; ++i) scanf ("%s", S[i]); int SX, SY; for (int i = 0, i < n; ++i) for (int j = 0; j < m; ++j) if (s[i][j] = = ' U ' | | s[i][j] = = ' D ' || S[I][J] = = ' L ' | | S[I][J] = = ' R ') sx = i, sy = j; Vis[sx][sy] = 1; if (s[sx][sy] = = ' U ') DFS (SX, SY, 0); else if (s[sx][sy] = = ' R ') DFS (SX, SY, 1); else if (s[sx][sy] = = ' D ') DFS (SX, SY, 2); else if (s[sx][sy] = = ' L ') DFS (SX, SY, 3); int ans = 0; for (int i = 0, i < n; ++i) for (int j = 0; j < m; ++j) ans + = vis[i][j]; printf ("%d\n", ans); } return 0;}
Codeforces 589J Cleaner Robot (DFS, or BFS)