ZOJ 3804 YY's Minions(模拟)

97 篇文章 0 订阅

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 eightdirections 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 Fminutes. 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 NMFKK means the number of leaving messages. 1<=NM<=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 TiXiYi, They mean the minion who is located in (XiYi) 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

题解:

题意:

有一个矩阵的宠物,0代表睡着,1代表醒着,如果周围八个方向上有3个醒着的,睡的会吵醒,如果周围醒着的个数小于2或者大于3,醒着的会睡,然后每一行t,x,y表示在ts结束时位于x,y位置的宠物会离开,让你输出一定时间后的变化情况,x表示离开了

思路:

模拟个全过程就好了

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<deque>
#define M (t[k].l+t[k].r)/2
#define lson k*2
#define rson k*2+1
using namespace std;
int dirx[8]={0,0,-1,1,-1,1,-1,1};//方向数组
int diry[8]={-1,1,0,0,-1,1,1,-1};
int p1[1005][1005];//存前1s的情况
int p2[1005][1005];//存当前情况
struct node
{
    int x,y;
    int time;
}t[2505];//存要离开的宠物
int cmp(node a,node b)//按宠物的离开时间从小到大排序
{
    return a.time<b.time;
}
int main()
{
    int i,j,k,z,x,y,m,n,f,test,q,ans,tt,xx,yy;
    scanf("%d",&test);
    while(test--)
    {
        scanf("%d%d%d%d",&n,&m,&f,&q);
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                scanf("%1d",&p1[i][j]);
            }
        }
        for(i=0;i<q;i++)
        {
            scanf("%d%d%d",&t[i].time,&t[i].x,&t[i].y);
        }
        sort(t,t+q,cmp);//排序
        ans=0;
        while(ans<q&&t[ans].time==0)//处理0s的情况
        {
            p1[t[ans].x][t[ans].y]=-1;//-1表示离开了
            ans++;
        }
        tt=1;
        while(tt<=f)//tt表示当前时间
        {
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=m;j++)
                {
                    if(p1[i][j]==-1)//如果为离开直接赋值
                    {
                        p2[i][j]=-1;
                        continue;
                    }
                    int num=0;
                    for(k=0;k<8;k++)
                    {
                        xx=i+dirx[k];
                        yy=j+diry[k];
                        if(xx>0&&yy>0&&xx<=n&&yy<=m&&p1[xx][yy]==1)//查找八个方向上醒着的个数
                        {
                            num++;
                        }
                    }
                    if(p1[i][j]==0)
                    {
                        if(num==3)//睡了会醒的情况
                            p2[i][j]=1;
                        else
                            p2[i][j]=0;
                    }
                    else
                    {
                        if(num<2||num>3)//醒了会睡的情况
                            p2[i][j]=0;
                        else
                            p2[i][j]=1;
                    }
                }
            }
            while(ans<q&&t[ans].time<=tt)//更新离开情况
            {
                p2[t[ans].x][t[ans].y]=-1;
                ans++;
            }
            tt++;
            for(i=1;i<=n;i++)//复制一遍当前情况
            {
                for(j=1;j<=m;j++)
                {
                    p1[i][j]=p2[i][j];
                }
            }
        }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                if(p1[i][j]==-1)
                    printf("X");
                else
                    printf("%d",p1[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值