题目:
代码如下:
#include<bits/stdc++.h>
using namespace std;
#define MAX 105
int n,m,L,sum[MAX][MAX],tt[MAX][MAX],dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
struct Point {int x,y,pos,t;}point;
void bfs(int x,int y)
{
queue<Point> q;
sum[x][y]++;
if(sum[x][y] > L){
for(int i = 0;i < 4;i++){
point.x = x;
point.y = y;
point.t = 0;
point.pos = i;
q.push(point);
}
sum[x][y] = 0;
tt[x][y] = 0;
}
while(!q.empty()){
point = q.front();
q.pop();
int xx = point.x + dir[point.pos][0];
int yy = point.y + dir[point.pos][1];
int ttm = point.t;
if(xx < 1 || xx > n || yy < 1 || yy > m) continue;
if(tt[xx][yy] == ttm + 1) continue;
else if(sum[xx][yy]){
sum[xx][yy]++;
if(sum[xx][yy] > L){
sum[xx][yy] = 0;
tt[xx][yy] = ttm + 1;
for(int i = 0;i < 4;i++){
Point temp;
temp.x = xx;
temp.y = yy;
temp.pos = i;
temp.t = ttm + 1;
q.push(temp);
}
}
}
else{
Point temp;
temp.x = xx;
temp.y = yy;
temp.pos = point.pos;
temp.t = ttm + 1;
q.push(temp);
}
}
}
int main()
{
int x,y,k,q;
cin >> n >> m >> L;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= m;j++)
cin >> k,sum[i][j] = k;
cin >> q;
while(q--){
cin >> x >> y;
bfs(x,y);
}
int s = 0;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= m;j++)
s += sum[i][j];
if(!s) cout << "YES" << endl;
else{
cout << "NO" << endl;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
if(j > 1) cout << " " << sum[i][j];
else cout << sum[i][j];
}
cout << endl;
}
}
return 0;
}
每次在注水的地方进行一次bfs操作,当水滴超过L是开始向四周走,最后每个空都是0就输出YES,否则就输出NO,随后将所有地方的水滴个数打印出来。