题解 | 太阳之华 | 洪水

太阳之华

https://www.nowcoder.com/practice/be67461f82ee46f69c5ffec7b549bb7e

#include <bits/stdc++.h>
using namespace std;
const int N=2e3+5;
bitset<N> mat[N];
bool vis[N][N];
int cnt,tot;
int n,m;
array<int, 2> stk[N*N];
int tp;
int dir[5] = {1, 0, -1, 0, 1};

void clear(){
    while(tp) {
        auto [x, y] = stk[tp--];
        if(mat[x][y] == 0) {
            vis[x][y] = false;
            cnt--;
        }
    }
}

void dfs(int i, int j) {
    if(i < 1 || j < 1 || i > n || j > m) return;
    if(vis[i][j]) return;
    vis[i][j] = true;
    stk[++tp] = {i, j};
    if(mat[i][j] == 0) {
        cnt++;
        return;
    }
    for(int d = 0; d < 4; d++) {
        dfs(i + dir[d],j + dir[d + 1]);
    }
}

void solve(){
    bool ok = false;
    cin >> n >> m;
    cnt = tot = 0;
    for(int i = 1; i <= n ; i++ ){
        // mat[i].reset();
        for(int j = 1; j <= m ; j++ ){
            vis[i][j] = false;
            char c;
            cin >> c;
            if(c == '#') mat[i][j] = true, ok = true; 
            else mat[i][j] = false, tot++;
        }
    }
    if(!ok){
        cout << "Blue" << "\n";
        return;
    }
    ok=true;
    for(int i = 1 ; i <= n ; i++) {
        for(int j = 1; j <= m; j++) {
            if(mat[i][j] && !vis[i][j]) {
                dfs(i,j);
                ok &= cnt < tot;
                clear();
            }
        }
    }
    if(ok) {
        cout << "Draw" << "\n";
    } else {
        cout << "Red" << "\n";
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int T=1;
    cin>>T;
    while(T--)solve();    
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

明明就不饿:看不懂你到底会啥,什么岗位
点赞 评论 收藏
分享
03-01 21:45
中北大学 golang
孤蓝长空:请你说一下为什么你用websocket而不是http,请你说一下什么是rpc,为什么用rpc,你的rpc的传输协议是JSON,xml还是什么 请你描述一下你的鉴权流程(完整的) 我问的是第二个项目,随便问的哈哈哈
开工第一帖
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务