6262:流感传染
Oj Url:http://noi.openjudge.cn/ch0203/6262/
总时间限制: 1000ms
内存限制: 65536kB
描述
有一批易感人群住在网格状的宿舍区内,宿舍区为
的矩阵,每个格点为一个房间,房间里可能住人,也可能空着。在第一天,有些房间里的人得了流感,以后每天,得流感的人会使其邻居传染上流感,(已经得病的不变),空房间不会传染。请输出第
天得流感的人数。
输入
第一行一个数字
,
不超过100,表示有
的宿舍房间。
接下来的行,每行
个字符,‘
’表示第一天该房间住着健康的人,’
’表示该房间空着,’
’表示第一天该房间住着得流感的人。
接下来的一行是一个整数,
不超过100.
输出
输出第
天,得流感的人数
样例输入
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;
};