题意:给了一个含有'.'和'X'的nxm的田野,'.'代表草坪,'X'代表石头,在田野的不同区域里会有燃着的草,不同地方的草燃着的时间不同,燃着的草每一秒可以向它相邻的区域蔓延,直到燃着的时间为0,求燃着的草有多少(石头不能被烧着)
分析:DFS。从燃着的位置开始沿四个方向DFS,记录所到位置之后还能燃着的时间,燃着的时间为0或者其他火苗也扩散到该点后,燃着的时间比当前能燃着的时间短则返回。
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 550;
char s[maxn][maxn];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int vis[maxn][maxn];
int n,m,k;
struct node
{
int x,y,t;
};
void dfs(node x)
{
if(x.t <= vis[x.x][x.y])return ;
vis[x.x][x.y] = x.t;
if(x.t == 0)return ;
for(int i = 0; i < 4; i++)
{
int xx = x.x + dir[i][0];
int yy = x.y + dir[i][1];
if(xx >= 0 && xx < n && yy >= 0 && yy < m && s[xx][yy] == '.')
{
node tmp;
tmp.x = xx;
tmp.y = yy;
tmp.t = x.t - 1;
dfs(tmp);
}
}
}
int main()
{
cin>>n>>m>>k;
for(int i = 0; i < n; i++)
{
scanf("%s",s[i]);
}
memset(vis,-1,sizeof(vis));
while(k--)
{
node ans;
scanf("%d%d%d",&ans.x,&ans.y,&ans.t);
ans.x--;ans.y--;
dfs(ans);
}
int cnt = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
{
if(s[i][j] == '.' && vis[i][j] >= 0)
{
cnt++;
// printf("i:%d j:%d\n",i,j);
}
}
cout<<cnt<<endl;
return 0;
}