#include <bits/stdc++.h>
using namespace std;
int n, m, zx, zy, nr, nc;
string a[1005];
int v[1005][1005];
int dr[] = {1, 0, -1, 0};
int dc[] = {0, 1, 0, -1};
const int bs[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
void dfs(int r, int c) {
a[r][c] = 'X';
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 4; j++) {
nr = r + i * dr[j];
nc = c + i * dc[j];
if (nr < 0 || nr > n - 1 ||
nc < 0 || nc > m - 1 ||
a[nr][nc] == 'X') {
continue;
}
v[nr][nc] = v[r][c] + 1;
//cout << nr << " " << nc << " " << v[nr][nc] << "\n";
if (v[zx][zy] > 0) {
return;
}
dfs(nr, nc);
}
}
}
int main() {
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
/*for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << a[i][j] << " ";
}
cout << "\n";
}*/
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] == '#') {
zx = i;
zy = j;
//cout << zx << " " << zy << "\n";
}
}
}
dfs(0, 0);
if (v[zx][zy] > 0) {
cout << v[zx][zy];
} else {
cout << -1;
}
return 0;
}
样例过了,但提交只对了一个......