HDU 5480 Conturbatio(并查集或前缀和)

题目大意:给出一个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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值