Description
SDUT 4783 2019-ncov的突然出现扰乱了人们的日常生活,它具有极强的传染性,可以快速的在人群中扩散,现在研究人员正在模拟其在人群中的扩散情况. 在一个n*m矩阵所示的人群中,*为普通人,#为佩戴口罩的人,@为病毒携带者, 已知每秒每位病毒携带者会将病毒传染给相邻八个方向的未戴口罩的普通人。 请问 x 秒后会有多少名传染者(初始为第0秒)?
输入格式
第一行输入空格分隔的三个数n,m,x代表n行,m列的空间,x秒(n,m<=1000)。 接下来n行每行m人如上述所示
输出格式
一个数字,代表最终被传染的人数。
输入样例
4 4 2 **** *@** **## **#*
输出样例
12
提示
多起点广搜
思路记录:典型的广度优先搜索题目,简单题,就是写起来比较麻烦,多练点吧
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
char a[1005][1005],b[1005][1005];
int qx[100000],fx=0,rx=0;
int qy[100000],fy=0,ry=0;
int px[8]={-1,-1,-1,0,1,1,1,0};
int py[8]={-1,0,1,1,1,0,-1,-1};
int nex,ney,pod=0,x,n,m;;
int bfs(int x1){
while(x1--){
int len=rx-fx;//cout<<len<<endl;
while(len--){
for(int i=0;i<8;i++){
nex=qx[fx]+px[i];
ney=qy[fy]+py[i];
if(nex>=0&&nex<n&&ney>=0&&ney<m&&a[nex][ney]=='*'){
a[nex][ney]='@';
pod++;
qx[rx++]=nex;
qy[ry++]=ney;
}
}
fx++;
fy++;
}
}
return pod;
}
int main()
{
cin>>n>>m;
cin>>x;
int pod1=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
cin>>a[i][j];
if(a[i][j]=='@')
qx[rx++]=i,qy[ry++]=j,pod1++;//进队列,把患病人员进队
}
cout<<bfs(x)+pod1;
return 0;
}