打水滴

题目:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
代码如下:

#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,随后将所有地方的水滴个数打印出来。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值