大模拟

The Game of Life

https://vjudge.net/problem/%E8%AE%A1%E8%92%9C%E5%AE%A2-A1537

给定初始点 问321次迭代中,最大的活点个数,最大的活点个数的次数,最后活点个数

总结反复操作的复杂度。

一开始用队列T了,改用set

#include<bits/stdc++.h>
#define inf 1e18
#define ull unsigned long long 
#define PI acos(-1.0)
#define PII pair<int,int>
using namespace std;
const int N = 1500 , M = 1e6+7;
const int mod = 1e9+7;
char g[N][N];
int cnt, maxx,maxx_pos,n,m;

int mp[750][750];
queue<PII>change;
set<PII>s; //记录活点和周围点 

int around(int x,int y){
    int num = 0;
    for(int dx=-1;dx<=1;++dx){
        for(int dy=-1;dy<=1;++dy){
            if(dx==0&&dy==0)continue;
            if(mp[x+dx][y+dy]==1)    num++;
        }
    }
    return num;
}
void insesrt_around(int x,int y){
    for(int dx=-1;dx<=1;++dx)
        for(int dy=-1;dy<=1;++dy)
            s.insert({x+dx,y+dy});
}
void solve(){
    s.clear();
    memset(mp,0,sizeof mp);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i)scanf("%s",g[i]+1);
    
    int now = 0,las;
    for(int i=1;i<=n;++i){
        for(int j=1;j<=m;++j){
            int fi = i + 350;
            int fj = j + 350;
            if(g[i][j]=='#'){
                insesrt_around(fi,fj);
                mp[fi][fj]=1;
                now++;
            }
        }
    }
    maxx = 0;maxx_pos = 0;
    for(int i=0;i<=321;++i){
        //cout<<"-------"<<i<<"\n";
        if(now>maxx){
            maxx=now;
            maxx_pos=i;
        }
        for(auto it:s){
            int x = it.first, y = it.second;
            int num = around(x,y);
            if(mp[x][y]==1){
            //    cout<<"x=="<<x-350<<"  "<<y-350<<"\n";
                if(num!=3&&num!=2)    change.push(it);
            //    cout<<"num=="<<num<<"\n";
            }
            else{
                if(num==3)    change.push(it);
            }
        }
        las = now;
        while(!change.empty()){
            PII u = change.front();change.pop();
            int x = u.first, y = u.second;
            if(mp[x][y]==0){
                now++;
                mp[x][y]=1;
                insesrt_around(x,y);
            }
            else{
                now--;
                mp[x][y]=0;
            }
        }
    }
    printf("%d %d %d\n",maxx_pos,maxx,las);
}
signed main(){
    int t=1;
    scanf("%d",&t);
    while(t--){
        solve();
    }
    return 0;
}

 

posted @ 2021-10-18 09:10  PdrEam  阅读(20)  评论(0编辑  收藏  举报