CodeForces-1350E Orac and Game of Life


传送门:CodeForces-1350E

思路分析

每一个好的点,不会变坏,坏的点变好之后也不会变坏了
所以用bfs预处理出每一个点变好的时间,然后根据时间差来判断就行了

样例输入

3 3 3
000
111
000
1 1 1
2 2 2
3 3 3

样例输出

1
1
1

AC代码

#include <bits/stdc++.h>
#define ls x<<1
#define rs x<<1|1
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define int long long 
namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
    bool IOerror=0;
    inline char nc() {
        static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
        if(p1==pend) {
            p1=buf;
            pend=buf+fread(buf,1,BUF_SIZE,stdin);
            if(pend==p1) {
                IOerror=1;
                return -1;
            }
        }
        return *p1++;
    }
    inline bool blank(char ch) {
        return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
    }
    template<class T> inline bool read(T &x) {
        bool sign=0;
        char ch=nc();
        x=0;
        for(; blank(ch); ch=nc());
        if(IOerror)return false;
        if(ch=='-')sign=1,ch=nc();
        for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
        if(sign)x=-x;
        return true;
    }
    template<class T,class... U>bool read(T& h,U&... t) {
        return read(h)&&read(t...);
    }
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 0x3f3f3f3f;
const int N = 1010;

int n,m,k;
int a[N][N],dis[N][N],vis[N][N];
int dx[4]= {0,0,1,-1},dy[4]= {1,-1,0,0};

bool judge(int x,int y) {
    for(int i=0; i<4; i++) {
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(nx<1||ny<1||nx>n||ny>m) continue;
        if(a[nx][ny]==a[x][y]) return true;
    }
    return false;
}

signed main() {

#ifdef xiaofan
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
#endif

    cin>>n>>m>>k;
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=m; j++) {
            char x;
            cin>>x;
            a[i][j]=x-'0';
        }
    }
    queue<pair<int,int>>q;
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=m; j++) {
            if(judge(i,j)) {
                q.push(mp(i,j));
                dis[i][j]=0;
                vis[i][j]=1;
            }
        }
    }
    while(!q.empty()) {
        auto u=q.front();
        q.pop();
        int x=u.fi;
        int y=u.se;
        for(int i=0; i<4; i++) {
            int nx=x+dx[i];
            int ny=y+dy[i];
            if(nx<1||ny<1||nx>n||ny>m||vis[nx][ny]) continue;
            dis[nx][ny]=dis[x][y]+1;
            vis[nx][ny]=1;
            q.push(mp(nx,ny));
        }
    }
    while(k--){
        int x,y,p;
        cin>>x>>y>>p;
        if(vis[x][y]){
            if(p<=dis[x][y] || (p-dis[x][y])%2==0) cout<<a[x][y]<<endl;
            else cout<<(a[x][y]^1)<<endl;
        }else{
            cout<<a[x][y]<<endl;
        }
    }





    return 0;
}
&nbsp;


文章作者: 小凡
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 小凡 !
评论
  目录
隐藏
{% if theme.sakura.enable %}{% endif %}