ZOJ-3804---YY's Minions (模拟)


YY's Minions

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Despite YY's so much homework, she would like to take some time to play with her minions first.

YY lines her minions up to an N*M matrix. Every minion has two statuses: awake or asleep. We use 0(the digit) to represent that it is asleep, and 1 for awake.Also, we define the minions who are around a minion closest in one of the eight directions its neighbors.And every minute every minion will change its status by the following specific rules:

  • If this minion is awake, and the number of its neighbors who are awake is less than 2, this minion will feel lonely and turn to asleep.
  • If this minion is awake, and the number of its neighbors who are awake is more than 3, this minion will turn to asleep for it will feel too crowded.
  • If this minion is awake, and the number of its neighbors who are awake is exactly 2 or 3, this minion will keep being awake and feel very happy.
  • If this minion is asleep, and the number of its neighbors who are awake is exactly 3, this minion will wake up because of the noise.Note that all changes take place at the same time at the beginning of a specific minute.

    Also, some minions will get bored and leave this silly game. We use 'X's to describe them.We suppose that a minion would leave after T minutes. It will leave at the end of the Tth minute. Its status is considered during the change at the beginning of the Tth minute, and should be ignored after that.Of course, one minion will not leave twice!

    YY is a girl full of curiosity and wants to know every minion's status after F minutes. But you know she is weak and lazy! Please help this cute girl to solve this problem :)

    Input

    There are multiple test cases.

    The first line contains the number of test cases Q. 1<=Q<=100.
    For each case, there are several lines:
    The first line contains four integers N, M, F, K. K means the number of leaving messages. 1<=N, M<=50, 1<=F<=1000, 1<=K<=N*M.
    Next N lines are the matrix which shows the initial status of each minion. Each line contains M chars. We guarantee that 'X' wouldn't appear in initial status matrix.
    And next K lines are the leaving messages. Each line contains three integers Ti, Xi, Yi, They mean the minion who is located in (Xi, Yi) will leave the game at the end of the Tith minutes. 1<=Ti<= F, 1<=Xi<=N, 1<=Yi<=M.

    Output

    For each case, output N lines as a matrix which shows the status of each minion after F minutes.

    Sample Input
    2
    3 3 2 1
    101
    110
    001
    1 2 2
    5 5 6 3
    10111
    01000
    00000
    01100
    10000
    2 3 3
    2 4 1
    5 1 5
    
    Sample Output
    010
    1X0
    010
    0000X
    11000
    00X00
    X0000
    00000
    
    Hint
    For case 1:
    
    T=0, the game starts
    101
    110
    001
    ---------------
    at the beginning of T=1, a change took place
    100
    101
    010
    ---------------
    at the end of T=1 (the minion in (2,2) left)
    100
    1X1
    010
    ---------------
    at the beginning of T=2, a change took place
    010
    1X0
    010
    ---------------
    at the end of T=2 (nothing changed for no minion left at T=2)
    010
    1X0
    010
    

题意:有个n*m的矩阵,每个代表一个人,1代表醒着,0代表睡着了,X代表离开了;输入n,m,f,k,f代表总时间,k代表要离开的人数,矩阵下面的输入代表在第几秒结束哪个位置的人会离开。
思路:这题就是简单的模拟,每一秒把每个人周围的人的8个方向的醒着的人算出来,然后按题意把当前位置的人的状态改变,注意不能直接在原数组上修改,可以用另一个数组暂时保存,然后把值赋给原数组,详细看代码中的解释:
AC代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m,f,k;
char a[55][55],b[55][55];
int next[8][2]={-1,-1,-1,0,-1,1,1,0,1,-1,1,1,0,1,0,-1};//方向数组
int Count(int x,int y)//计算当前点周围醒着的人数
{
        int cnt=0;
        for(int i=0;i<8;i++)
        {
            int tx=x+next[i][0];
            int ty=y+next[i][1];
            if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&a[tx][ty]=='1')
                cnt++;
        }
        return cnt;
}
int main()
{
    int v;
    scanf("%d",&v);
    while(v--)
    {
        scanf("%d%d%d%d",&n,&m,&f,&k);
        for(int i=1;i<=n;i++)
            scanf("%s",a[i]+1);
        int t[2505],x[2505],y[2505];
        for(int i=1;i<=k;i++)
            scanf("%d%d%d",&t[i],&x[i],&y[i]);
        for(int tt=1;tt<=f;tt++){//循环每一秒
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    if(a[i][j]=='X')b[i][j]='X';//如果当前人已经离开了则不需要处理
                    else{
                        int cnt=Count(i,j);//按题意把b[i][j]变成相应的状态
                        if(a[i][j]=='1'){
                            if(cnt<2||cnt>3)b[i][j]='0';
                            else b[i][j]=a[i][j];
                        }
                        else if(a[i][j]=='0'){
                            if(cnt==3) b[i][j]='1';
                            else b[i][j]='0';
                        }
                    }
                }
            }
            for(int i=1;i<=k;i++){//判断当前时间是否有人离开。注意这里没处理过的话一定要遍历所有的点,不然会wrong answer
                if(t[i]==tt)
                    b[x[i]][y[i]]='X';
            }
            for(int i=1;i<=n;i++){//把b赋给a
                for(int j=1;j<=m;j++){
                    a[i][j]=b[i][j];
                }
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                printf("%c",a[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值