1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| #include <cstdio> using namespace std;
const int H = 1000, W = 1000; const int dr[] = {0,1,0,-1}, dc[] = {1,0,-1,0}; char a[H][W+1]; int tmp[4], h, w;
void DFS(int r, int c) { if (a[r][c] != '.' || !r || r == h-1 || !c || c == w-1) return; int cntA = 0; for (int d = 0; d < 4; ++d) if (a[r+dr[d]][c+dc[d]] == 'A') tmp[cntA++] = d; switch (cntA) { case 2: if ((tmp[0]+tmp[1])%2 == 0 || a[r-dr[tmp[0]]-dr[tmp[1]]][c-dc[tmp[0]]-dc[tmp[1]]] != '.') break; case 3: case 4: a[r][c] = 'A'; for (int d = 0; d < 4; ++d) DFS(r+dr[d], c+dc[d]); } }
inline bool check(int r, int c) { return 0 <= r && r < h && 0 <= c && c < w && a[r][c] == '.'; }
int main() { scanf("%d%d", &h, &w); for (int r = 0; r < h; ++r) scanf("%s", a[r]); for (int r = 1; r < h-1; ++r) for (int c = 1; c < w-1; ++c) DFS(r, c);
int r, c, rr, cc, d = 0, len = 0; for (r = 0; r < h; ++r) for (c = 0; c < w; ++c) if (a[r][c] == 'A') { --r; goto L1; } L1: rr = r; cc = c; do { int rrr = r+dr[d+1&3], ccc = c+dc[d+1&3]; if (check(rrr, ccc)) { r = rrr; c = ccc; d = d+1 & 3; } else if (rrr = r+dr[d], ccc = c+dc[d], check(rrr, ccc)) { r = rrr; c = ccc; } else if (rrr = r+dr[d+3&3], ccc = c+dc[d+3&3], check(rrr, ccc)) { r = rrr; c = ccc; d = d+3 & 3; } ++len; }while (r != rr || c != cc); printf("%d\n", len); }
|