- 题解
B3625 迷宫寻路 只得40分自己也查不出错
- 2024-3-17 20:35:56 @
/*
3 5
.##.#
.#...
...#.
*/
#include <iostream>
#include <queue>
typedef struct
{
int x, y;
} Node;
const int wc = 4;
Node w[wc] {
{-1, 0},
{1, 0},
{0, -1},
{0, 1}
};
int main()
{
using namespace std;
int n, m;
cin >> n >> m;
char** a = new char*[n];
for (int i = 0; i < n; i++)
{
a[i] = new char[m];
for (int j = 0; j < m; j++)
{
//cin >> a[i][j];
do { cin >> a[i][j]; }
while (a[i][j] != '.' && a[i][j] != '#');
}
}
queue<Node> q;
q.push({0, 0});
try
{
while (!q.empty())
{
Node f = q.front();
if (a[f.x][f.y] == '#') throw false;
a[f.x][f.y] = '#';
for (int i = 0; i < wc; i++)
{
Node node = f;
node.x += w[i].x;
node.y += w[i].y;
if ((node.x < n && node.x >= 0) && (node.y < m && node.y >= 0)) if (a[node.x][node.y] == '.')
{
q.push(node);
if (node.x == n - 1 && node.y == m - 1) throw true;
}
}
q.pop();
}
throw false;
}
catch (bool& r)
{
if (r) cout << "Yes" << endl;
else cout << "No" << endl;
}
for (int i = 0; i < n; i++) delete [] a[i];
delete [] a;
return 0;
}
0 条评论
目前还没有评论...