当前位置: 首页> 健康> 母婴 > 【C++学习第17天】深度有限搜索

【C++学习第17天】深度有限搜索

时间:2025/9/6 17:38:00来源:https://blog.csdn.net/qq_52726763/article/details/140878845 浏览次数:0次

一、题目

AcWing 845. 八数码 - AcWing

二、代码

来自acwing

#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>using namespace std;typedef pair<int, int> PII;const int N = 110;int n, m;
int g[N][N];
int d[N][N];
PII q[N * N];int bfs()
{int hh = 0, tt = 0;q[0] = {0, 0};memset(d, -1, sizeof d);d[0][0] = 0;int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};while(hh <= tt){auto t = q[hh++];for(int i = 0; i < 4; i++){int x = t.first + dx[i], y = t.second + dy[i];if(x >= 0 && x < n && y >= 0 && y < m && g[x][y] == 0 && d[x][y] == -1){d[x][y] = d[t.first][t.second] + 1;q[++tt] = {x, y};}}}return d[n - 1][m - 1];
}int main()
{cin >> n >> m;for(int i = 0; i < n; i++)for(int j = 0; j < m; j++)cin >> g[i][j];cout << bfs() << endl;return 0;
}

关键字:【C++学习第17天】深度有限搜索

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: