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
| #include <bits/stdc++.h>
#define INF 0x3f3f3f3f #define EPS 1e-8 #define FOR(i, l, r) for (ll(i) = (l); (i) <= (r); ++(i)) #define log printf #define IOS \ ios::sync_with_stdio(false); \ cin.tie(nullptr); \ cout.tie(nullptr);
using namespace std;
typedef __int128 i128; typedef long long ll; typedef pair<ll, ll> PII;
const int N = 510, M = 310; int m, n, t, x, a[N][N], dp[N][N][M], ans; string s;
template <typename T>
inline T read() { T sum = 0, fl = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1; for (; isdigit(ch); ch = getchar()) sum = sum * 10 + ch - '0'; return sum * fl; }
template <typename T>
inline void write(T x) { if (x < 0) { putchar('-'), write<T>(-x); return; } static T sta[35]; ll top = 0; do { sta[top++] = x % 10, x /= 10; } while (x); while (top) putchar(sta[--top] + 48); }
int main() { IOS; cin >> t; while (t--) { cin >> n >> m >> x; FOR(i, 1, n) { cin >> s; for (int j = 0;j < m;++j) { if (s[j] == '?') a[i][j + 1] = -INF; else a[i][j + 1] = s[j] - '0'; } } FOR(i, 1, n) FOR(j, 1, m) FOR(k, 0, x) { switch(a[i][j]) { case -INF: if (k > 0) dp[i][j][k] = max(dp[i][j - 1][k - 1], dp[i - 1][j][k - 1]) + 1; else dp[i][j][k] = max(dp[i][j - 1][k], dp[i - 1][j][k]); break; case 1: dp[i][j][k] = max(dp[i][j - 1][k], dp[i - 1][j][k]) + 1; break; case 0: dp[i][j][k] = max(dp[i][j - 1][k], dp[i - 1][j][k]); break; } } FOR(i, 0, x) ans = max(ans, dp[n][m][i]); cout << ans << endl; memset(dp, 0, sizeof(dp)); memset(a, 0, sizeof(a)); ans = -INF; } return 0; }
|