题目大意:给出一个N*M的棋盘,棋盘上面有K只车,车能攻击所在行和列
现在给你一个矩阵,问这个矩阵内的所有棋子是否都能被攻击到
解题思路:这题我用的是并查集,把连续的能攻击的行和列并起来,查找的时候就比较简单了
矩阵的所有棋子要被攻击到,要么矩阵内所有行都有车,要么所有列都有车,要么两者都成立
还有另一种思路,就是用前缀和,直接两个位置的前缀和相减判断是否矩阵内的行和列都能被攻击到
这里只给出并查集的做法
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 100010;
int row[N], col[N];
bool visRow[N], visCol[N];
int n, m, k, q;
int findRow(int x) {
return row[x] == x ? x : row[x] = findRow(row[x]);
}
int findCol(int x) {
return col[x] == x ? x : col[x] = findCol(col[x]);
}
void solve() {
scanf("%d%d%d%d", &n, &m, &k, &q);
memset(visRow, 0, sizeof(visRow));
memset(visCol, 0, sizeof(visCol));
for (int i = 1; i <= n; i++)
row[i] = i;
for (int i = 1; i <= m; i++)
col[i] = i;
int r, c;
int x1, x2, y1, y2;
while (k--) {
scanf("%d%d", &r, &c);
if (!visRow[r]) {
if (visRow[r - 1] && r >= 2)
row[r - 1] = r;
if (visRow[r + 1] && r < n)
row[r] = findRow(r + 1);
}
if (!visCol[c]) {
if (visCol[c - 1] && c >= 2)
col[c - 1] = c;
if (visCol[c + 1] && c < m)
col[c] = findCol(c + 1);
}
visRow[r] = visCol[c] = true;
}
int t1, t2;
while (q--) {
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
if (visRow[x1])
t1 = findRow(x1);
else
t1 = 0;
if (visCol[y1])
t2 = findCol(y1);
else
t2 = 0;
if (t1 >= x2 || t2 >= y2)
printf("Yes\n");
else
printf("No\n");
}
}
int main() {
int test;
scanf("%d", &test);
while (test--) solve();
return 0;
}