打水滴(BFS)

打水滴(BFS)

在一个n行m列的网格中,某些位置存在一些水滴。嘟嘟进行q次打水滴操作,每次嘟嘟在某一个网格当中添加一个水滴,当某一网格中的水滴数量超过L时,该网格中的水滴变为四个水滴,并分别向上下左右四个方向飞出,每个飞出的水滴如果遇到一个包含水滴的网格时,则该水滴停在该网格当中,如果一直没有遇到,则该水滴将飞出网格。所有水滴移动的速度都相同,每秒移动一格。如果最终时刻所有网格中都不包含水滴,则嘟嘟取得了胜利。

注意:如果同一秒有多个水滴同时进入一个网格,那么应该等所有水滴都进入以后再会破裂。q次打水滴,嘟嘟都会等到不在有水滴移动了以后才会滴下一滴水滴。每次打水滴操作后,直到所有水滴都不再移动,才会进行下一次的打水滴操作。

输入格式

第一行包含三个整数n,m(1≤n,m≤100),L(5≤L≤10)。

接下来n行每行包含m个整数,每个整数在区间[0,L]范围内,表示每个网格中的水滴数量。

接下来一行包含一个整数q(q≤2500)。

接下来q行每行包含两个整数x,y(1≤x≤n,1≤y≤m),表示嘟嘟每次添加水滴的位置。

输出格式

对于每组测试数据,如果所有水滴都消失,则输出"YES",否则第一行输出"NO",接下来n行每行输出m个整数,表示每个网格中的水滴数量。

 

输入样例1

2 2 2
2 0
0 0
1
1 1

输出样例1

YES

输入样例2

2 2 2
2 0
0 0
2
1 2
1 1

输出样例2

NO
0 2
0 0

 

 

 

 

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <iostream>
  4 #include <string>
  5 #include <math.h>
  6 #include <algorithm>
  7 #include <vector>
  8 #include <stack>
  9 #include <queue>
 10 #include <set>
 11 #include <map>
 12 #include <sstream>
 13 const int INF=0x3f3f3f3f;
 14 typedef long long LL;
 15 const int mod=1e9+7;
 16 const double PI = acos(-1);
 17 const double eps =1e-8;
 18 #define Bug cout<<"---------------------"<<endl
 19 const int maxn=1e7+10;
 20 using namespace std;
 21 
 22 int n,m,L;
 23 int G[110][110];
 24 int tm[110][110];//记录每个位置水滴破开的时间 
 25 struct node
 26 {
 27     int x,y;//坐标 
 28     int pos;//记录移动方向 
 29     int time;//记录时间 
 30 };
 31 int NT[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
 32 
 33 void BFS(int x,int y)
 34 {
 35     node now,to;
 36     queue<node> qe;
 37     G[x][y]++;
 38     if(G[x][y]>L)//如果会破开才会BFS 
 39     {
 40         for(int i=0;i<4;i++)//4个方向都要有 
 41         {
 42             now.x=x;
 43             now.y=y;
 44             now.pos=i;
 45             now.time=0;
 46             qe.push(now);
 47         }
 48         G[x][y]=0;//归0 
 49         tm[x][y]=0;//开始时间初始为0 
 50     }
 51     while(!qe.empty())
 52     {
 53         now=qe.front();
 54         qe.pop();
 55         int xx=now.x+NT[now.pos][0];
 56         int yy=now.y+NT[now.pos][1];
 57         if(xx>0&&xx<=n&&yy>0&&yy<=m)
 58         {
 59             if(now.time+1==tm[xx][yy]) continue;//处理同一秒多个水滴进入同一个格子 
 60             else if(G[xx][yy])
 61             {
 62                 G[xx][yy]++;
 63                 if(G[xx][yy]>L)
 64                 {
 65                     for(int i=0;i<4;i++)
 66                     {
 67                         to.x=xx;
 68                         to.y=yy;
 69                         to.pos=i;
 70                         to.time=now.time+1;
 71                         qe.push(to);
 72                     }
 73                     G[xx][yy]=0;
 74                     tm[xx][yy]=now.time+1;//记录时间 
 75                 }
 76             }
 77             else//该位置没水滴会向更远处传递 
 78             {
 79                 to.x=xx;
 80                 to.y=yy;
 81                 to.pos=now.pos;
 82                 to.time=now.time+1;//记录时间 
 83                 qe.push(to);
 84             }
 85         }
 86     }
 87 }
 88 
 89 int main()
 90 {
 91     #ifdef DEBUG
 92     freopen("sample.txt","r",stdin);
 93     #endif
 94     
 95     scanf("%d %d %d",&n,&m,&L);
 96     for(int i=1;i<=n;i++)
 97     {
 98         for(int j=1;j<=m;j++)
 99             scanf("%d",&G[i][j]);
100     }
101     int q;
102     scanf("%d",&q);
103     for(int i=1;i<=q;i++)
104     {
105         memset(tm,-1,sizeof(tm));
106         int x,y;
107         scanf("%d %d",&x,&y);
108         BFS(x,y);
109     }
110     int sum=0;
111     for(int i=1;i<=n;i++)
112     {
113         for(int j=1;j<=m;j++)
114             sum+=G[i][j];
115     }
116     if(sum)
117     {
118         printf("NO\n");
119         for(int i=1;i<=n;i++)
120         {
121             for(int j=1;j<=m;j++)
122                 printf(j==m?"%d\n":"%d ",G[i][j]);
123         }
124     }
125     else printf("YES\n");
126     
127     return 0;
128 }

 

 

 

 

 

 

-

posted @ 2020-01-13 16:22  jiamian22  阅读(337)  评论(0编辑  收藏  举报