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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
#include <bits/stdc++.h> #define FIO cin.tie(0); ios::sync_with_stdio(false) #define all(x) (x).begin(), (x).end() #define fi first #define se second #define TEST #define TESTS int t = 1; cin >> t; while (t--)
#if 1 #define int i64 #define inf 0x3f3f3f3f3f3f3f3fLL #else #define inf 0x3f3f3f3f #endif
#ifndef ONLINE_JUDGE #define debug(...) std::cerr << #__VA_ARGS__ << " = "; dbg1(__VA_ARGS__) void dbg2() { std::cerr << "\n"; } template<typename T, typename... T2> void dbg2(T x, T2... args) { std::cerr << ", " << x; dbg2(args...); } template<typename T, typename... T2> void dbg1(T x, T2... args) { std::cerr << x; dbg2(args...); } #else #define debug(...) #endif
using namespace std; using i64 = long long; using u32 = unsigned; using u64 = unsigned long long; using pii = std::pair<int, int>;
inline char nc() { static char buf[1 << 20], *p1, *p2; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++; } #ifndef ONLINE_JUDGE #define nc getchar #endif void read() {} template<typename T, typename... T2> inline void read(T &x, T2 &... oth) { x = 0; char c = nc(), up = c; while(!isdigit(c)) up = c, c = nc(); while(isdigit(c)) x = x * 10 + c - '0', c = nc(); up == '-' ? x = -x : 0; read(oth...); }
constexpr int N = 2e5 + 10; constexpr int MOD = 998244353;
int dx[]{1, -1, 0}; int dy[]{0, 0, 1};
int dx2[]{1, -1, 0, 0}; int dy2[]{0, 0, 1, -1};
void solve() { int n, m, k; cin >> n >> m >> k; vector<int> mn(n * m + 1, 1e9), mx(n * m + 1); vector<vector<char>> s(n + 1, vector<char>(m + 1)); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { cin >> s[i][j]; } } vector vis(n + 1, vector(m + 1, false)); vector<vector<vector<tuple<int, int>>>> pre(n + 1, vector<vector<tuple<int, int>>>(m + 1)); vector ok(n + 1, vector(m + 1, false)); { queue<tuple<int, int>> q; q.emplace(1, 1); while (q.size()) { auto [x, y] = q.front(); q.pop(); if (vis[x][y]) continue; vis[x][y] = 1; for (int i = 0; i < 3; ++i) { int nx = x + dx[i], ny = y + dy[i]; if (nx <= 0 || nx > n || ny <= 0 || ny > m || s[nx][ny] == '1') continue; q.emplace(nx, ny); pre[nx][ny].emplace_back(x, y); } } q.emplace(1, m); while (q.size()) { auto [x, y] = q.front(); q.pop(); if (ok[x][y]) continue; ok[x][y] = 1; for (auto [_x, _y] : pre[x][y]) { q.emplace(_x, _y); } } } vector vis2(n + 1, vector(m + 1, false)); auto bfs = [&](int f, int sx, int sy) { queue<tuple<int, int>> q; q.emplace(sx, sy); while (q.size()) { auto [x, y] = q.front(); q.pop(); if (vis2[x][y]) continue; vis2[x][y] = 1; for (int i = 0; i < 3; ++i) { int nx = x + dx[i], ny = y + dy[i]; if (nx <= 0 || nx > n || ny <= 0 || ny > m || !(!ok[nx][ny] && vis[nx][ny])) continue; mn[f] = min(mn[f], ny); mx[f] = max(mx[f], ny); q.emplace(nx, ny); } } }; for (int j = 1; j <= m; ++j) { for (int i = 1; i <= n; ++i) { if (!ok[i][j] && vis[i][j] && !vis2[i][j]) { int id = m * (i - 1) + j; mn[id] = mx[id] = j; bfs(id, i, j); } } } int flag = 1; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { int id = m * (i - 1) + j; int t = mx[id] - mn[id] + 1; if (t >= k) { flag = 0; break; } } } if (!ok[1][1]) flag = 0; assert(ok[1][1]); cout << (!flag ? "Yes" : "No") << "\n"; }
signed main() { FIO; TESTS { solve(); }
return 0; }
|