[C++]流感传染

6262:流感传染 

Oj Url:http://noi.openjudge.cn/ch0203/6262/

总时间限制: 1000ms

内存限制: 65536kB

 

描述

有一批易感人群住在网格状的宿舍区内,宿舍区为\boldsymbol{n*n}的矩阵,每个格点为一个房间,房间里可能住人,也可能空着。在第一天,有些房间里的人得了流感,以后每天,得流感的人会使其邻居传染上流感,(已经得病的不变),空房间不会传染。请输出第\boldsymbol{m}天得流感的人数。

 

输入

第一行一个数字\boldsymbol{n}\boldsymbol{n}不超过100,表示有\boldsymbol{n*n}的宿舍房间。
接下来的\boldsymbol{n}行,每行\boldsymbol{n}个字符,‘\boldsymbol{.}’表示第一天该房间住着健康的人,’\boldsymbol{\#}’表示该房间空着,’\boldsymbol{@}’表示第一天该房间住着得流感的人。
接下来的一行是一个整数\boldsymbol{m}\boldsymbol{m}不超过100.

输出

输出第\boldsymbol{m}天,得流感的人数

 

样例输入

5
....#
.#.@.
.#@..
#....
.....
4

样例输出

16

程序代码

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
#define elif else if
#define for1(i,l,r) for(int i=l;i<=r;++i)
#define for2(i,l,r) for(int i=l;i>=r;--i)
#define mst(a) memset(a,0,sizeof a)
typedef long long ll;
const int MAX_SIZE = 111;
int a[MAX_SIZE][MAX_SIZE] = {};
queue <int> q1;
queue <int> q2;
int main(int argc, char *argv[]) {
	ios::sync_with_stdio(false);
	mst(a); int n, m, ans = 0; cin >> n;
	for1 (i, 1, n)
		for1 (j, 1, n) {
			char ch; cin >> ch;
			if (ch == '#') a[i][j] = -1;
			elif (ch == '@') a[i][j] = 1;
		};
	cin >> m;
	for1 (k, 2, m) {
		for1 (i, 1, n)
			for1 (j, 1, n)
				if (a[i][j] == 1) { //save the positions of the latent patients
					if (a[i - 1][j] == 0) {q1.push(i - 1); q2.push(j);}; 
					if (a[i][j - 1] == 0) {q1.push(i); q2.push(j - 1);}; 
					if (a[i + 1][j] == 0) {q1.push(i + 1); q2.push(j);};
					if (a[i][j + 1] == 0) {q1.push(i); q2.push(j + 1);};
				};
		while (!q1.empty()) { //get the positions from the queues
			int x = q1.front(), y = q2.front();
			a[x][y] = 1; //replace the map
			q1.pop(); q2.pop();	
		};	
	};
	for1 (i, 1, n) for1 (j, 1, n) if (a[i][j] == 1) ++ans;
	cout << ans << endl; //print the answer 
	return 0; 	
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值