首页 > 代码库 > ZOJ Problem Set - 3804 YY's Minions
ZOJ Problem Set - 3804 YY's Minions
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
23 3 2 11011100011 2 25 5 6 310111010000000001100100002 3 32 4 15 1 5
Sample Output
0101X00100000X1100000X00X000000000
Hint
For case 1:
T=0, the game starts101110001---------------at the beginning of T=1, a change took place100101010---------------at the end of T=1 (the minion in (2,2) left)1001X1010---------------at the beginning of T=2, a change took place0101X0010---------------at the end of T=2 (nothing changed for no minion left at T=2)0101X0010#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>using namespace std;char str[55][55];int a[100][100],aa[100][100];int t[2550],x[2550],y[2550];int d[8][2]= {{1,0},{1,1},{1,-1},{0,1},{0,-1},{-1,0},{-1,1},{-1,-1}};int main(){ int q,n,m,f,k,r,e; int i,j,cnt,c; scanf("%d",&q); while(q--) { memset(a,0,sizeof(a)); memset(t,0,sizeof(t)); memset(x,0,sizeof(x)); memset(y,0,sizeof(y)); memset(str,0,sizeof(str)); scanf("%d%d%d%d",&n,&m,&f,&k); getchar(); for(i=0; i<n; i++) { for(j=0; j<m; j++) scanf("%c",&str[i][j]); getchar(); } for(i=0; i<n; i++) { for(j=0; j<m; j++) { if(str[i][j]==‘1‘) a[i][j]=1; if(str[i][j]==‘0‘) a[i][j]=0; } } for(i=0; i<k; i++) scanf("%d%d%d",&t[i],&x[i],&y[i]); for(r=1; r<=f; r++) { memset(aa,0,sizeof(aa)); for(i=0; i<n; i++) { for(j=0; j<m; j++) { if(a[i][j]==1) { for(e=0;e<8;e++) { int xx=i+d[e][0]; int yy=j+d[e][1]; if(xx>=0&&yy>=0&&xx<n&&yy<m) aa[xx][yy]++; } } } } for(i=0; i<n; i++) { for(j=0; j<m; j++) { if(a[i][j]==2) continue; if(a[i][j]==1) { if(aa[i][j]<2||aa[i][j]>3) a[i][j]=0; } else if(a[i][j]==0) { if(aa[i][j]==3) a[i][j]=1; } } } for(i=0;i<k;i++) { if(r==t[i]) a[x[i]-1][y[i]-1]=2; } } for(i=0; i<n; i++) { for(j=0; j<m; j++) { if(a[i][j]==2) printf("X"); else printf("%d",a[i][j]); } printf("\n"); } } return 0;}/*35 5 6 510111010000000001100100002 3 32 4 15 1 54 3 31 5 5*/
ZOJ Problem Set - 3804 YY's Minions