【HDU】5254 棋盘占领(BFS)

这题给的不明不白的,一开始不清楚有公共点是几个意思

10 11

01   可以变成 11

直接BFS就可以了

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 555;
int n,m,ans;
int vis[maxn][maxn];
int mat[maxn][maxn];
int state[maxn][maxn][4];// 0 1 2 3
const int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
queue<int>qx,qy;
bool judge(int x,int y){
    if(state[x][y][0] && state[x][y][2]) return true;
    if(state[x][y][0] && state[x][y][3]) return true;
    if(state[x][y][1] && state[x][y][2]) return true;
    if(state[x][y][1] && state[x][y][3]) return true;
    return false;
}
void bfs(){
    while(!qx.empty()){
        int x = qx.front(),y = qy.front();
        qx.pop(); qy.pop();
        for(int i = 0; i < 4; i++){
            int xx = x + dir[i][0];
            int yy = y + dir[i][1];
            if(xx >=  1 && xx <= n && yy >= 1 && yy <= m && !vis[xx][yy]){
                state[xx][yy][i] = 1;
                if(judge(xx,yy)){
                    vis[xx][yy] = 1;
                    qx.push(xx); qy.push(yy);
                    ans ++;
                }
            }
        }
    }
    printf("%d\n",ans);
}
int main(){
    int T,Case = 1;
    scanf("%d",&T);
    while(T--){
        int t;
        memset(vis,0,sizeof(vis));
        memset(state,0,sizeof(state));
        scanf("%d%d",&n,&m);
        scanf("%d",&t);
        ans = 0;
        for(int i = 0; i < t; i++){
            int x,y;
            scanf("%d%d",&x,&y);
            if(!vis[x][y]){
                vis[x][y] = 1;
                qx.push(x); qy.push(y);
                ans ++;
            }
        }
        printf("Case #%d:\n",Case++);
        bfs();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值