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 108 109 110 111 112 113 114 115 116 117 118 119
| #include <bits/stdc++.h> using namespace std;
#ifdef Fread char buf[1 << 21], *iS, *iT; #define gc() (iS == iT ? (iT = (iS = buf) + fread (buf, 1, 1 << 21, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++) #define getchar gc #endif
template <typename T> void r1(T &x) { x = 0; char c(getchar()); int f(1); for(; c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1; for(; '0' <= c && c <= '9';c = getchar()) x = (x * 10) + (c ^ 48); x *= f; }
#ifdef Getmod const int mod = 1e9 + 7; template <int mod> struct typemod { int z; typemod(int a = 0) : z(a) {} inline int inc(int a,int b) const {return a += b - mod, a + ((a >> 31) & mod);} inline int dec(int a,int b) const {return a -= b, a + ((a >> 31) & mod);} inline int mul(int a,int b) const {return 1ll * a * b % mod;} typemod<mod> operator + (const typemod<mod> &x) const {return typemod(inc(z, x.z));} typemod<mod> operator - (const typemod<mod> &x) const {return typemod(dec(z, x.z));} typemod<mod> operator * (const typemod<mod> &x) const {return typemod(mul(z, x.z));} typemod<mod>& operator += (const typemod<mod> &x) {*this = *this + x; return *this;} typemod<mod>& operator -= (const typemod<mod> &x) {*this = *this - x; return *this;} typemod<mod>& operator *= (const typemod<mod> &x) {*this = *this * x; return *this;} int operator == (const typemod<mod> &x) const {return x.z == z;} int operator != (const typemod<mod> &x) const {return x.z != z;} }; typedef typemod<mod> Tm; #endif
template <typename T,typename... Args> inline void r1(T& t, Args&... args) { r1(t); r1(args...); }
const int maxn = 4e5 + 5; const int maxm = maxn << 1;
int n, m;
int a[maxn];
#define id(i, j) ((i - 1) * m + j)
char s[maxn];
int L[maxn], R[maxn];
void dfs1(int x,int y,int c) { if(L[id(x, y)] != -1) return ; L[id(x, y)] = c; if(x < n) dfs1(x + 1, y, c); if(x > 1 && s[id(x - 1, y)] == '#') dfs1(x - 1, y, c); if(y > 1 && s[id(x, y - 1)] == '#') dfs1(x, y - 1, c); if(y < m && s[id(x, y + 1)] == '#') dfs1(x, y + 1, c); }
void dfs2(int x,int y,int c) { if(R[id(x, y)] != -1) return ; R[id(x, y)] = c; if(x < n) dfs2(x + 1, y, c); if(x > 1 && s[id(x - 1, y)] == '#') dfs2(x - 1, y, c); if(y > 1 && s[id(x, y - 1)] == '#') dfs2(x, y - 1, c); if(y < m && s[id(x, y + 1)] == '#') dfs2(x, y + 1, c); }
int f[maxn];
signed main() {
int i, j; memset(L, -1, sizeof(L)), memset(R, -1, sizeof(R)); r1(n, m); for(i = 1; i <= n; ++ i) { for(j = 1; j <= m; ++ j) { cin >> s[id(i, j)]; } }
for(i = 1; i <= m; ++ i) r1(a[i]);
for(j = 1; j <= m; ++ j) for(i = 1; i <= n; ++ i) if(s[id(i, j)] == '#') dfs1(i, j, j); for(j = m; j >= 1; -- j) for(i = 1; i <= n; ++ i) if(s[id(i, j)] == '#') dfs2(i, j, j);
for(i = 1; i <= m + 1; ++ i) f[i] = m + 2; for(j = 1; j <= m; ++ j) if(a[j]) { for(i = n; i; -- i) if(s[id(i, j)] == '#') { -- a[j]; if(a[j] == 0) { f[L[id(i, j)]] = min(f[L[id(i, j)]], R[id(i, j)] + 1); } } }
int ans(0), pos(1); for(i = m; i; -- i) f[i] = min(f[i], f[i + 1]); while(pos <= m + 1) ++ ans, pos = f[pos]; printf("%d\n", ans - 1);
return 0; }
|