http://noi.openjudge.cn/ch0205/2753/ 实在不知道怎么做了
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#define maxn 41
using namespace std;
struct node {
int x;
int y;
};
queue<node> q;
char a[maxn][maxn];
int book[maxn][maxn];
int dis[maxn][maxn];
int r, c;
int mi = 1000;
int _next[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {-1, 0}};
int bfs() {
while (!q.empty()) {
node z = q.front();
if (book[z.x][z.y] == 2) {
return dis[z.x][z.y];
}
book[z.x][z.y] = 1;
for (int i = 0 ; i < 4 ; i++) {
int tx = z.x + _next[i][0];
int ty = z.y + _next[i][1];
if (book[tx][ty] != 1) {
dis[tx][ty] = dis[z.x][z.y] + 1;
q.push((node) {
tx, ty
});
}
}
q.pop();
}
return 0;
}
int main() {
cin >> r >> c;
for (int i = 0; i <= r + 1; i++) {
for (int j = 0 ; j <= c + 1; j++) {
a[i][j] = '#';
dis[i][j] = 0;
book[i][j] = 1;
}
}
for (int i = 1 ; i <= r; i++) {
for (int j = 1; j <= c ; j++) {
cin >> a[i][j];
if (a[i][j] == '.') {
book[i][j] = 0;
}
}
}
q.push((node) {
1, 1
});
book[r][c] = 2;
// for (int i = 1; i <= r; i++) {
// for (int j = 1; j <= c; j++) {
// cout << book[i][j] << " ";
// }
// cout << endl;
// }
dis[1][1] = 1;
cout << bfs() << endl;
return 0;
}