题意:就是上一道formula的基础上限定某些格子只能竖着通过,某些只能横着通过。
还是括号表示。这个转移的时候特判一下就好了,具体实现基本和上一题一样。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cassert>
#include<iostream>
using namespace std;
#define LL long long
#define clr(a) memset(a,0,sizeof a)
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define erp(i,a,b) for(int i=a;i>=b;--i)
#define getbit(x,y) (((x) >> ((y)<<1)) & 3)
#define bit(x,y) ((x)<<((y)<<1))
#define clrbit(x,i,j) ((x) & (~(3<<((i)<<1))) & (~(3<<((j)<<1))))
const int MAXS = 1000000, mo = 100003;
int n, m, ex, ey;
char a[15][15];
struct Node
{
int s, nxt;
LL val;
};
struct Hash
{
Node e[MAXS];
int adj[mo], ec;
void init()
{
memset(adj, -1, sizeof adj);
ec = 0;
}
void push(int s, LL v)
{
int ha = s%mo;
for (int i = adj[ha]; ~i; i=e[i].nxt)
if (e[i].s == s)
{
e[i].val += v;
return;
}
e[ec].val = v, e[ec].s = s;
e[ec].nxt = adj[ha];
adj[ha] = ec++;
}
} dp[2];
inline int FindL(int st, int x)
{
int cnt = 1, s;
erp(i, x-1, 0)
{
s = (st >> (i<<1)) & 3;
if (s == 2) cnt++;
else if (s == 1) cnt--;
if (!cnt) return i;
}
return -1;
}
inline int FindR(int st, int x)
{
int cnt = 1, s;
rep(i, x+1, m)
{
s = (st >> (i<<1)) & 3;
if (s == 1) cnt++;
else if (s == 2) cnt--;
if (!cnt) return i;
}
return -1;
}
void work1(int i, int j, int cur) // '.'
{
dp[cur].init();
rep(k, 0, dp[cur^1].ec-1)
{
int L = getbit(dp[cur^1].e[k].s, j-1);
int U = getbit(dp[cur^1].e[k].s, j);
int s = clrbit(dp[cur^1].e[k].s, j-1, j);
LL las = dp[cur^1].e[k].val;
if (!L && !U)
{
if (i<n && j<m)
dp[cur].push(s|bit(1, j-1)|bit(2, j), las);
}
else if (!L)
{
if (i < n) dp[cur].push(s|bit(U, j-1), las);
if (j < m) dp[cur].push(s|bit(U, j), las);
}
else if (!U)
{
if (i < n) dp[cur].push(s|bit(L, j-1), las);
if (j < m) dp[cur].push(s|bit(L, j), las);
}
else if (L==1 && U==1)
dp[cur].push(s^bit(3, FindR(s, j)), las);
else if (L==2 && U==2)
dp[cur].push(s^bit(3, FindL(s, j-1)), las);
else if (L==2 && U==1)
dp[cur].push(s, las);
else if (i==ex && j==ey)
dp[cur].push(s, las);
}
}
void work2(int i, int j, int cur)
{
dp[cur].init();
rep(k, 0, dp[cur^1].ec-1)
{
int L = getbit(dp[cur^1].e[k].s, j-1);
int U = getbit(dp[cur^1].e[k].s, j);
int s = clrbit(dp[cur^1].e[k].s, j-1, j);
LL las = dp[cur^1].e[k].val;
if (!L && !U) dp[cur].push(s, las);
}
}
void worklr(int i, int j, int cur)
{
dp[cur].init();
rep(k, 0, dp[cur^1].ec-1)
{
int L = getbit(dp[cur^1].e[k].s, j-1);
int U = getbit(dp[cur^1].e[k].s, j);
int s = clrbit(dp[cur^1].e[k].s, j-1, j);
LL las = dp[cur^1].e[k].val;
if (!U && L)
{
if (j < m) dp[cur].push(s|bit(L, j), las);
}
}
}
void workud(int i, int j, int cur)
{
dp[cur].init();
rep(k, 0, dp[cur^1].ec-1)
{
int L = getbit(dp[cur^1].e[k].s, j-1);
int U = getbit(dp[cur^1].e[k].s, j);
int s = clrbit(dp[cur^1].e[k].s, j-1, j);
LL las = dp[cur^1].e[k].val;
if (!L && U)
{
if (i < n) dp[cur].push(s|bit(U, j-1), las);
}
}
}
LL solve()
{
dp[0].init(), dp[0].push(0, 1);
int cur = 0;
rep(i, 1, n)
{
rep(k, 0, dp[cur].ec-1)
dp[cur].e[k].s <<= 2;
rep(j, 1, m)
{
cur ^= 1;
if (a[i][j]=='.') work1(i, j, cur);
else if (a[i][j]=='#') work2(i, j, cur);
else if (a[i][j]=='-') worklr(i, j, cur);
else workud(i, j, cur);
}
}
rep(k, 0, dp[cur].ec-1)
if (!dp[cur].e[k].s) return dp[cur].e[k].val;
return 0;
}
int main()
{
scanf("%d%d", &n, &m);
rep(i, 1, n)
{
scanf("%s", a[i]+1);
rep(j, 1, m)
{
if (a[i][j]=='.') ex = i, ey = j;
if (a[i][j]=='-'&&(j==1||j==m)) { puts("0"); return 0; }
if (a[i][j]=='|'&&(i==1||i==n)) { puts("0"); return 0; }
}
}
cout << solve() << '\n';
return 0;
}