测评会员优惠活动进行中 · 开通 VIP,有效期内测评不限次 VIP 优惠中 · 测评不限次 立即查看

A49989. 流感传染

填空题 中等

题目描述

流感传染

题目描述

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

输入

第一行一个数字n,n不超过100,表示有n*n的宿舍房间。

接下来的n行,每行n个字符,’.’表示第一天该房间住着健康的人,’#’表示该房间着,

’@’表示第一天该房间住着得流感的人。

接下来的一行是一个整数m,m不超过100.

输出

输出第m天,得流感的人数

样例输入

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

样例输出

16

参考答案

#include<bits/stdc++.h> using namespace std; char room[105][105]; int n,day,ans = 0; void change(int x,int y){ if(room[x-1][y] == '.'&&(x-1)>=0){ room[x-1][y] = '*'; } if(room[x][y-1] == '.'&&(y-1)>=0){ room[x][y-1] = '*'; } if(room[x+1][y] == '.'&&(x+1)<n){ room[x+1][y] = '*'; } if(room[x][y+1] == '.'&&(y+1)<n){ room[x][y+1] = '*'; } } int main(){ cin>>n; getchar(); for(int i = 0;i<n;i++){ for(int j = 0;j<n;j++){ char k = getchar(); room[i][j] = k; } getchar(); } cin>>day; for(int k = 1;k<day;k++){ for(int i = 0;i<n;i++){ for(int j = 0;j<n;j++){ if(room[i][j] == '@'){ change(i,j); } } } for(int i = 0;i<n;i++){ for(int j = 0;j<n;j++){ if(room[i][j] == '*'){ room[i][j] = '@'; } } } } for(int i = 0;i<n;i++){ for(int j = 0;j<n;j++){ if(room[i][j] == '@'){ ans++; } } } cout<<ans; }
上一题 下一题