zoukankan      html  css  js  c++  java
  • bfs+规律——cf1350E

    没啥思维难度。。就是找到一个格子第一次开始变色的时间点f[i][j],再往后这个格子必定会每个时间改变一次颜色

    处理出第一次就变颜色的格子,然后用bfs进行扩展,找周围没被扩展过的,且初始颜色不同的格子

    #include<bits/stdc++.h>
    using namespace std;
    #define N 1005
    #define ll long long
    
    int n,m,t;
    char mp[N][N];
    
    int f[N][N];//每个格子第一次改变颜色时的时间 
    void prework(){ 
        queue<pair<int,int> >q;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++){
                if(i+1<=n && mp[i][j]==mp[i+1][j]){
                    q.push(make_pair(i,j));
                    f[i][j]=1;
                }
                else if(i-1>=1 && mp[i][j]==mp[i-1][j]){
                    q.push(make_pair(i,j));
                    f[i][j]=1;
                }
                else if(j+1<=m && mp[i][j]==mp[i][j+1]){
                    q.push(make_pair(i,j));
                    f[i][j]=1;
                }
                else if(j-1>=1 && mp[i][j]==mp[i][j-1]){
                    q.push(make_pair(i,j));
                    f[i][j]=1;
                }
            }
        while(q.size()){
            auto p=q.front();q.pop();
            int i=p.first,j=p.second;//把周围同色的加入queue     
            if(i+1<=n && mp[i][j]!=mp[i+1][j] && !f[i+1][j]){
                q.push(make_pair(i+1,j));
                f[i+1][j]=f[i][j]+1;
            }
            if(i-1>=1 && mp[i][j]!=mp[i-1][j] && !f[i-1][j]){
                q.push(make_pair(i-1,j));
                f[i-1][j]=f[i][j]+1;
            }
            if(j+1<=m && mp[i][j]!=mp[i][j+1] && !f[i][j+1]){
                q.push(make_pair(i,j+1));
                f[i][j+1]=f[i][j]+1;
            }
            if(j-1>=1 && mp[i][j]!=mp[i][j-1] && !f[i][j-1]){
                q.push(make_pair(i,j-1));
                f[i][j-1]=f[i][j]+1;
            }
        }
    }
    
    int main(){
        cin>>n>>m>>t;
        for(int i=1;i<=n;i++)
            scanf("%s",mp[i]+1);
        prework();
        
        while(t--){
            int i,j;ll p;
            scanf("%d%d%lld",&i,&j,&p);
            if(n==1 && m==1){
                cout<<mp[i][j]<<'
    ';
                continue;
            }
            if(f[i][j]==0){
                cout<<mp[i][j]<<'
    ';
            }
            else if(p<=f[i][j]-1){
                cout<<mp[i][j]<<'
    ';
            }else {
                p-=f[i][j]-1;
                p%=2;
                int ans=mp[i][j]-'0';
                if(p)cout<<(ans^1)<<'
    ';
                else cout<<ans<<'
    ';
            }
        }
    }
  • 相关阅读:
    Unity The Method Signature Matching Rule
    Unity The Property Matching Rule
    Unity The Type Matching Rule
    Unity The Custom Attribute Matching Rule
    Unity The Member Name Matching Rule
    Unity No Policies
    Unity The Return Type Matching Rule
    Unity The Parameter Type Matching Rule
    Unity The Namespace Matching Rule
    关于TSQL递归查询的(转)
  • 原文地址:https://www.cnblogs.com/zsben991126/p/12894601.html
Copyright © 2011-2022 走看看