有点问题

#include<iostream>
#include<queue>
using namespace std;

const int q = 50;
char a[q][q];
int m, n;
int dx[4] = { 0,0,1,-1 };
int dy[4] = { 1,-1,0,0 };
bool vis[q][q] = { false };
struct Edge {
int x, y, w;
};
int check(int xx,int yy) {
if (xx > 0 && xx <= m && yy > 0 && yy <= n && a[xx][yy] == '.')
return true;
return false;
}
int bfs() {
queue<Edge>que;
que.push({ 1,1,0 });
while (!que.empty()) {
Edge e = que.front();
que.pop();
if (e.x == m && e.y == n) {
return e.w + 1;
}
if (vis[e.x][e.y]) continue;
vis[e.x][e.y] = true;
for (int i = 0; i < 4; i++) {
int xx = e.x + dx[i];
int yy = e.y + dy[i];
if (check(xx,yy)) {
que.push({ xx,yy,e.w + 1 });
}
}
}
}
int main() {
cin >> m >> n;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[m][n];
}
}
cout << bfs() << endl;
return 0;
}

1 comments

  • @ 2024-2-29 10:12:48

    对不起,,,我二维数组输入的时候搞错了,已经OK了,越简单的问题越不好找

    • 1

    Information

    ID
    769
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    7
    Tags
    # Submissions
    444
    Accepted
    87
    Uploaded By