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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| #include <bits/stdc++.h> #define rep(i, x, y) for (int i = x; i <= y; i++) using namespace std;
typedef long long ll; const int mod = 590027, N = 6e5 + 10; ll n, m, ans, ex, ey, pre, cur = 1; ll bits[30], tots[2], lnk[N], nxt[N], to[N], cnt, sta[2][N], dp[2][N]; bool mp[15][15]; char s[15][15];
void hsh(ll state, ll val) { ll x = state % mod; for (ll i = lnk[x]; i; i = nxt[i]) if (sta[cur][to[i]] == state) { dp[cur][to[i]] += val; return; } tots[cur]++; sta[cur][tots[cur]] = state; dp[cur][tots[cur]] = val;
to[++cnt] = tots[cur], nxt[cnt] = lnk[x], lnk[x] = cnt; }
void DP() { ll cursta, curans; int is_d, is_r; dp[cur][tots[cur] = 1] = 1, sta[cur][1] = 0; rep(i, 1, n) { rep(j, 1, tots[cur]) sta[cur][j] <<= 2; rep(j, 1, m) { swap(pre, cur); memset(lnk, 0, sizeof(lnk)); tots[cur] = cnt = 0; rep(k, 1, tots[pre]) { cursta = sta[pre][k], curans = dp[pre][k]; is_r = (cursta >> bits[j - 1]) % 4, is_d = (cursta >> bits[j]) % 4;
if (!mp[i][j]) { if (!is_r && !is_d) hsh(cursta, curans); } else if (!is_r && !is_d) { if (mp[i + 1][j] && mp[i][j + 1]) hsh(cursta + (1 << bits[j - 1]) + 2 * (1 << bits[j]), curans); } else if (is_r && !is_d) { if (mp[i][j + 1]) hsh(cursta - is_r * (1 << bits[j - 1]) + is_r * (1 << bits[j]), curans); if (mp[i + 1][j]) hsh(cursta, curans); } else if (!is_r && is_d) { if (mp[i][j + 1]) hsh(cursta, curans); if (mp[i + 1][j]) hsh(cursta + is_d * (1 << bits[j - 1]) - is_d * (1 << bits[j]), curans); } else if (is_r == 1 && is_d == 1) { int cnt = 1; rep(l, j + 1, m) { int t = (cursta >> bits[l]) % 4; if (t == 1) ++cnt; if (t == 2) --cnt; if (!cnt) { hsh(cursta - (1 << bits[j - 1]) - (1 << bits[j]) - (1 << bits[l]), curans); break; } } } else if (is_r == 2 && is_d == 2) { int cnt = -1; for (int l = j - 2; l; --l) { int t = (cursta >> bits[l]) % 4; if (t == 1) ++cnt; if (t == 2) --cnt; if (!cnt) { hsh(cursta - 2 * (1 << bits[j - 1]) - 2 * (1 << bits[j]) + (1 << bits[l]), curans); break; } } } else if (is_r == 2 && is_d == 1) { hsh(cursta - 2 * (1 << bits[j - 1]) - (1 << bits[j]), curans); } else if (is_r == 1 && is_d == 2) { if (i == ex && j == ey) ans += curans; } } } } }
int main() { cin >> n >> m; rep(i, 1, n) { scanf("%s", s[i] + 1); rep(j, 1, m) if (s[i][j] == '.') ex = i, ey = j, mp[i][j] = 1; else mp[i][j] = 0; } rep(i, 1, 25) bits[i] = (i << 1); DP(); printf("%lld\n", ans); return 0; }
|