zoukankan      html  css  js  c++  java
  • Orac and Game of Life

    C. Orac and Game of Life

    参考:Codeforces Round #641 Editorial

    要注意到可能会有多块区域会同时发生变化,那么这个时候就不能用 dfs 来写了,考虑到它的同时性,那么就可以用 bfs 来写

    将思维转化为代码的能力。

    // Created by CAD on 2020/5/16.
    #include <bits/stdc++.h>
    
    #define fi first
    #define se second
    #define pii pair<int,int>
    #define ll long long
    using namespace std;
    
    const int maxn=1e3+5;
    int g[maxn][maxn],f[maxn][maxn];
    int dx[]={0,0,1,-1};
    int dy[]={1,-1,0,0};
    int n,m;
    
    bool check(int _x,int _y){
        for(int i=0;i<4;++i){
            int x=_x+dx[i],y=_y+dy[i];
            if(x<1||x>n||y<1||y>m) continue;
            if(g[x][y]==g[_x][_y]) return 1;
        }
        return 0;
    }
    void bfs(){
        queue<pii> q;
        for(int i=1;i<=n;++i)
            for(int j=1;j<=m;++j)
                if(check(i,j))
                    q.push({i,j}),f[i][j]=1;
        while(!q.empty()){
            pii now=q.front();q.pop();
            int _x=now.fi,_y=now.se;
            for(int i=0;i<4;++i){
                int x=_x+dx[i],y=_y+dy[i];
                if(x<1||x>n||y<1||y>m||f[x][y]) continue;
                f[x][y]=f[_x][_y]+1;
                q.push({x,y});
            }
        }
    }
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(0);
        int t;cin>>n>>m>>t;
        for(int i=1;i<=n;++i){
            string s;cin>>s;
            for(int j=0;j<m;++j)
                g[i][j+1]=s[j]-'0';
        }
        bfs();
        while(t--){
            ll x,y,p;cin>>x>>y>>p;
            p++;
            if(f[x][y])
                cout << (p<=f[x][y]?g[x][y]:g[x][y]^(p-f[x][y]&1)) << "
    ";
            else cout<<g[x][y]<<"
    ";
        }
        return 0;
    }
    
    CAD加油!欢迎跟我一起讨论学习算法,QQ:1401650042
  • 相关阅读:
    多线程02
    多线程01
    CSS
    Mybatis CRUD中万能Map的用法及优势
    Tomcat配置
    Node.js+Vue+Webpack
    Java的几种常见排序算法
    maven插件 mybatis逆向工程
    ssm依赖
    mybatis spring整合依赖配置
  • 原文地址:https://www.cnblogs.com/CADCADCAD/p/12900380.html
Copyright © 2011-2022 走看看