|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目如图,代码如下
立体迷宫的bfs代码有几个点没过去,求助一下看下哪个点漏了。
- #include<bits/stdc++.h>
- using namespace std;
- int n, m, k;
- char a[15][15][15];
- int vis[15][15][15];
- int dz[] = { 0,0,0,0,1,-1 }; int dy[] = { 0,0,1,-1,0,0 }; int dx[] = { 1,-1,0,0,0,0 };
- int ans = 1;
- typedef struct node {
- int x, y, z;
- }node;
- queue<node> q;
- void bfs(int x, int y, int z) {
- node t;
- t.x = x; t.y = y; t.z = z;
- q.push(t);
- vis[x][y][z] = 1;
- while (!q.empty()) {
- node t = q.front();
- q.pop();
- for (int i = 0; i < 6; i++) {
- int tx = t.x + dx[i];
- int ty = t.y + dy[i];
- int tz = t.z + dz[i];
- if (tx < 1 || tx > k || ty < 1 || ty > n || tz < 1 || tz > m) continue;
- if (!vis[tx][ty][tz] && a[tx][ty][tz] != '#') {
- node t1;
- t1.x = tx; t1.y = ty; t1.z = tz;
- ans++;
- q.push(t1);
- vis[tx][ty][tz] = 1;
- }
- }
- }
- }
- int main() {
- cin >> n >> m >> k;
- int im, in;//起点
- for (int i = 1; i <= k; i++)
- for (int j = 1; j <= n; j++)
- for (int k = 1; k <= m; k++)
- {
- cin >> a[i][j][k];
- }
- cin >> im >> in;
- a[1][in][im] = '.';//泄露实验室一定为'.'
- bfs(1, in, im);
- cout << ans;
- }
复制代码
谢谢大佬回复。
你的代码中有一个明显的错误。在你的bfs函数中,你使用了一个名为k的变量作为循环变量,但是这个变量与外部的k变量重名,导致在循环过程中无法正确访问外部的k变量。为了解决这个问题,你需要修改内部的循环变量名,例如将其改为j。修改后的代码如下:
- int main() {
- cin >> n >> m >> k;
- int im, in; // 起点
- for (int i = 1; i <= k; i++)
- for (int j = 1; j <= n; j++)
- for (int l = 1; l <= m; l++) {
- cin >> a[ i][j][l];
- }
- cin >> im >> in;
- a[1][in][im] = '.'; // 泄露实验室一定为'.'
- bfs(1, in, im);
- cout << ans;
- }
复制代码
请注意,我将内部的循环变量名从k改为了l,以避免与外部的k变量重名。
|
-
|