#include <cstdio>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <stack>
#define INF 0x3f3f3f3f
#define IMAX 2147483646
#define LINF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define c unsigned long long
#define uint unsigned int
using namespace std;
int x, y, mx, my, d[111][111];
char s[111][111];
bool v[111][111];
int dx[8] = { -1,-1,-1,1,1,1,0,0 };
int dy[8] = { -1,0,1,-1,0,1,-1,1 };
queue<pair<int, int> > q;
bool pd() {
for (int i = 1; i <= y; i++)
for (int j = 1; j <= x; j++)
if (!v[i][j]) return 0;
return 1;
}
int main() {
scanf("%d%d%d%d", &x, &y, &mx, &my);
q.push(make_pair(y + 1 - my, mx));
for (int i = 1; i <= y; i++)
scanf("%s", s[i] + 1);
memset(v, 0, sizeof(v));
for (int i = 1; i <= y; i++)
for (int j = 1; j <= x; j++)
if (s[i][j] == '*') v[i][j] = 1;
v[y + 1 - my][mx] = 1;
memset(d, 0, sizeof(d));
while (q.size()) {
pair<int, int> now = q.front();
q.pop();
for (int i = 0; i < 8; i++) {
pair<int, int> nxt;
nxt.first = now.first + dx[i];
nxt.second = now.second + dy[i];
if (nxt.first < 1 || nxt.first > y || nxt.second < 1 || nxt.second > x)
continue;
if (v[nxt.first][nxt.second])
continue;
v[nxt.first][nxt.second] = 1;
d[nxt.first][nxt.second] = d[now.first][now.second] + 1;
q.push(nxt);
}
if (pd()) {
printf("%d\n", d[now.first][now.second] + 1);
return 0;
}
}
return 0;
}
ch 2907宽搜
最新推荐文章于 2022-05-21 17:42:43 发布