ZOJ 3804 YY's Minions(模拟)

曾经终败给现在 2022-06-10 00:46 211阅读 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 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

  1. 2
  2. 3 3 2 1
  3. 101
  4. 110
  5. 001
  6. 1 2 2
  7. 5 5 6 3
  8. 10111
  9. 01000
  10. 00000
  11. 01100
  12. 10000
  13. 2 3 3
  14. 2 4 1
  15. 5 1 5

Sample Output

  1. 010
  2. 1X0
  3. 010
  4. 0000X
  5. 11000
  6. 00X00
  7. X0000
  8. 00000

Hint

  1. For case 1:
  2. T=0, the game starts
  3. 101
  4. 110
  5. 001
  6. ---------------
  7. at the beginning of T=1, a change took place
  8. 100
  9. 101
  10. 010
  11. ---------------
  12. at the end of T=1 (the minion in (2,2) left)
  13. 100
  14. 1X1
  15. 010
  16. ---------------
  17. at the beginning of T=2, a change took place
  18. 010
  19. 1X0
  20. 010
  21. ---------------
  22. at the end of T=2 (nothing changed for no minion left at T=2)
  23. 010
  24. 1X0
  25. 010

题解:

题意:

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

思路:

模拟个全过程就好了

代码:

  1. #include<algorithm>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<stdio.h>
  5. #include<math.h>
  6. #include<string>
  7. #include<stdio.h>
  8. #include<queue>
  9. #include<stack>
  10. #include<map>
  11. #include<deque>
  12. #define M (t[k].l+t[k].r)/2
  13. #define lson k*2
  14. #define rson k*2+1
  15. using namespace std;
  16. int dirx[8]={0,0,-1,1,-1,1,-1,1};//方向数组
  17. int diry[8]={-1,1,0,0,-1,1,1,-1};
  18. int p1[1005][1005];//存前1s的情况
  19. int p2[1005][1005];//存当前情况
  20. struct node
  21. {
  22. int x,y;
  23. int time;
  24. }t[2505];//存要离开的宠物
  25. int cmp(node a,node b)//按宠物的离开时间从小到大排序
  26. {
  27. return a.time<b.time;
  28. }
  29. int main()
  30. {
  31. int i,j,k,z,x,y,m,n,f,test,q,ans,tt,xx,yy;
  32. scanf("%d",&test);
  33. while(test--)
  34. {
  35. scanf("%d%d%d%d",&n,&m,&f,&q);
  36. for(i=1;i<=n;i++)
  37. {
  38. for(j=1;j<=m;j++)
  39. {
  40. scanf("%1d",&p1[i][j]);
  41. }
  42. }
  43. for(i=0;i<q;i++)
  44. {
  45. scanf("%d%d%d",&t[i].time,&t[i].x,&t[i].y);
  46. }
  47. sort(t,t+q,cmp);//排序
  48. ans=0;
  49. while(ans<q&&t[ans].time==0)//处理0s的情况
  50. {
  51. p1[t[ans].x][t[ans].y]=-1;//-1表示离开了
  52. ans++;
  53. }
  54. tt=1;
  55. while(tt<=f)//tt表示当前时间
  56. {
  57. for(i=1;i<=n;i++)
  58. {
  59. for(j=1;j<=m;j++)
  60. {
  61. if(p1[i][j]==-1)//如果为离开直接赋值
  62. {
  63. p2[i][j]=-1;
  64. continue;
  65. }
  66. int num=0;
  67. for(k=0;k<8;k++)
  68. {
  69. xx=i+dirx[k];
  70. yy=j+diry[k];
  71. if(xx>0&&yy>0&&xx<=n&&yy<=m&&p1[xx][yy]==1)//查找八个方向上醒着的个数
  72. {
  73. num++;
  74. }
  75. }
  76. if(p1[i][j]==0)
  77. {
  78. if(num==3)//睡了会醒的情况
  79. p2[i][j]=1;
  80. else
  81. p2[i][j]=0;
  82. }
  83. else
  84. {
  85. if(num<2||num>3)//醒了会睡的情况
  86. p2[i][j]=0;
  87. else
  88. p2[i][j]=1;
  89. }
  90. }
  91. }
  92. while(ans<q&&t[ans].time<=tt)//更新离开情况
  93. {
  94. p2[t[ans].x][t[ans].y]=-1;
  95. ans++;
  96. }
  97. tt++;
  98. for(i=1;i<=n;i++)//复制一遍当前情况
  99. {
  100. for(j=1;j<=m;j++)
  101. {
  102. p1[i][j]=p2[i][j];
  103. }
  104. }
  105. }
  106. for(i=1;i<=n;i++)
  107. {
  108. for(j=1;j<=m;j++)
  109. {
  110. if(p1[i][j]==-1)
  111. printf("X");
  112. else
  113. printf("%d",p1[i][j]);
  114. }
  115. printf("\n");
  116. }
  117. }
  118. return 0;
  119. }

发表评论

表情:
评论列表 (有 0 条评论,211人围观)

还没有评论,来说两句吧...

相关阅读