PS:好久没有更新博客,由于各种原因吧,主要还是懒,从今天开始重新更新啦。


题目描述

You are given a grid of squares with H horizontal rows and W vertical columns, where each square is painted white or black. HW characters from A11 to AHW represent the colors of the squares. Aij is # if the square at the i-th row from the top and the j-th column from the left is black, and Aij is . if that square is white.
We will repeatedly perform the following operation until all the squares are black:
Every white square that shares a side with a black square, becomes black.
Find the number of operations that will be performed. The initial grid has at least one black square.
Constraints
·1≤H,W≤1000
·Aij is # or ..
·The given grid has at least one black square.

 

输入

Input is given from Standard Input in the following format:
H W
A11A12...A1W
:
AH1AH2...AHW

 

 

输出

Print the number of operations that will be performed.


题目大意:给你一个n*m的方格,有'#'与'.'组成,#每次可以呈十字型向外扩散,问最少需要扩散几次,使得整个方格全部都是#。

 

这是一个搜索问题 ,用到BFS,用一个例子来说明题意与 BFS 对应这道题的妙处。

样例:(黑代表#,白代表.)

看一下 (3,2)这个位置,如果用(1,2)位置的黑去更新需要两步,但是如果用(3,3)则需要一步,所以我们所求的最小的最大值有可能会因为我们过程中 更新判断错误而出错。而如果用BFS,我们首先将 黑  的两个位置 放入队列,然后进行BFS,BFS的条件即为判断这个点是否已被更新,我们可以开标记数组VIS也可以 判断是否为'.',如果为'.'就将其赋值为'#',并放入队列。这样时间复杂度也有所优化。

具体过程还需要看代码:

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
typedef long long ll ;
const ll INF=1e9+5;
ll n,m;
struct node{
    int x,y;
    int step;
};
char mp[1005][1005];
int ans=-INF;
queue<node>q;
bool judge(int x,int y)
{
    if(x<1||x>n||y<1||y>m)
        return false;
    return true;
}
int dx[5]={1,-1,0,0};
int dy[5]={0,0,-1,1};
void bfs()
{
    while(!q.empty())
    {
        node pre=q.front();q.pop();
        ans=max(ans,pre.step);
        for(int i=0;i<4;i++)
        {
            int mx=dx[i]+pre.x,my=dy[i]+pre.y;
            if(judge(mx,my)&&mp[mx][my]=='.')
            {
                mp[mx][my]='#';
                q.push(node{mx,my,pre.step+1});
            }
        }
    }
}
int main()
{
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%s",mp[i]+1);
    for(int i=1;i<=n;i++)
    {
        for(int k=1;k<=m;k++)
            if(mp[i][k]=='#')
            {
               node s=node{i,k,0};
               q.push(s);
            }
    }
    bfs();
    printf("%d\n",ans);
    return 0;
}