Darker and Darker
题目大意:给你一个
这是一个搜索问题,容易想到广搜,用广搜妙在哪里?
样例:(黑代表#,白代表.)
. # .
. . .
. . #
看一下
而如果用广搜,我们首先将黑色的两个位置放入队列,然后进行广搜,广搜的条件即为判断这个点是否已被更新,我们可以开标记数组
AC Code:
#include <bits/stdc++.h>
using namespace std;
#define mod 1e9+7
typedef long long ll;
const ll INF=1e9+5;
long long n,m;
char mp[1005][1005];
int ans = -INF;
int dx[5]={1,-1,0,0};
int dy[5]={0,0,-1,1};
struct node {
int x,y,step;
};
queue<node> q;
bool judge(int x,int y) // 判断格子合法性
{
if(x<1 || x>n || y<1 || y>m)
return false;
return true;
}
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;
int 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;
}
审核大大,求过~