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 Tthminute. 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 Tithminutes. 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
思路:没什么好说的,就是搜索模拟。
AC代码如下:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n,m,k,num[60][60][2],gun,dx[8]={-1,-1,0,1,1,1,0,-1},dy[8]={0,1,1,1,0,-1,-1,-1}; char s[60]; struct node { int x,y,f; }box[2510]; bool cmp(node a,node b) { return a.f<b.f; } void solve(int T,int a,int b) { int i,j,p,ret; for(i=1;i<=n;i++) for(j=1;j<=m;j++) { if(num[i][j][a]==-1) { num[i][j][b]=-1; continue; } ret=0; for(p=0;p<8;p++) if(num[i+dx[p]][j+dy[p]][a]==1) ret++; if(num[i][j][a]==1) { if(ret<2) num[i][j][b]=0; else if(ret>3) num[i][j][b]=0; else num[i][j][b]=1; } else { if(ret==3) num[i][j][b]=1; else num[i][j][b]=0; } } while(gun<=k) { if(box[gun].f==T) { num[box[gun].x][box[gun].y][b]=-1; gun++; } else break; } } int main() { int t,i,j,f,a,b,xi,xj; scanf("%d",&t); while(t--) { scanf("%d%d%d%d",&n,&m,&f,&k); for(i=1;i<=n;i++) { scanf("%s",s+1); for(j=1;j<=m;j++) num[i][j][0]=s[j]-'0'; } for(i=0;i<=m;i++) num[0][i][0]=num[n+1][i][0]=num[0][i][1]=num[n+1][i][1]=0; for(i=0;i<=n;i++) num[i][0][0]=num[i][m+1][0]=num[i][0][1]=num[i][m+1][1]=0; for(i=1;i<=k;i++) scanf("%d%d%d",&box[i].f,&box[i].x,&box[i].y); sort(box+1,box+1+k,cmp); gun=1; for(i=1;i<=f;i++) { if(i&1) solve(i,0,1); else solve(i,1,0); } if(f&1) a=1; else a=0; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) if(num[i][j][a]!=-1) printf("%d",num[i][j][a]); else printf("X"); printf("\n"); } } }