只有40分求助

P2298 Mzc和男家丁的游戏

@[HiAI](/user/462173) 这里推荐把你的bfs写在函数里 然后可以用一个结构体存x和y, 然后用一个queue就行! ```cpp #include <iostream> #include <queue> using namespace std; const int N = 2010; int n, m; bool vis[N][N], flag; int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; char c[N][N]; struct node { int x, y; int step; } st; bool check(int x, int y) { return x >= 1 && x <= n && y >= 1 && y <= m && !vis[x][y] && c[x][y] != '#'; } void bfs(node st) { vis[st.x][st.y] = true; queue<node> q; q.push(st); while (!q.empty()) { node top = q.front(); q.pop(); for (int i = 0; i < 4; i ++) { int x = top.x + dir[i][0]; int y = top.y + dir[i][1]; if (c[top.x][top.y] == 'd') { cout << top.step; flag = true; return ; } if (check(x, y)) { q.push({x, y, top.step + 1}); vis[x][y] = true; } } } } int main() { // freopen(".in", "r", stdin); // freopen(".out", "w", stdout); scanf ("%d %d", &n, &m); for (int i = 1; i <= n; i ++) { for (int j = 1; j <= m; j ++) { cin >> c[i][j]; if (c[i][j] == 'm') st = {i, j, 0}; } } bfs(st); if (!flag) cout << "No Way!"; return 0; } ```
by zhuzl009 @ 2023-03-29 21:11:02


还有这里不要用x1和y1会和c++里的变量冲突
by zhuzl009 @ 2023-03-29 21:15:20


|