【C++学习第17天】深度有限搜索
创始人
2024-11-14 21:06:58
0

一、题目

AcWing 845. 八数码 - AcWing

二、代码

来自acwing

#include  #include  #include  #include   using namespace std;  typedef pair 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; }

相关内容

热门资讯

黑科技模拟器!微扑克ai辅助工... 黑科技模拟器!微扑克ai辅助工具(ai辅助)太离谱了总是真的有挂(2025已更新)(哔哩哔哩)1、全...
透视挂“wepoker轻量版有... 透视挂“wepoker轻量版有透视吗”详细透视辅助开挂详细教程-一贯是有挂;wepoker轻量版有透...
分享开挂内幕“免费广东雀神智能... 分享开挂内幕“免费广东雀神智能插件安装”太坑了透视辅助开挂科技教程-确实真的是有挂;1、下载好免费广...
黑科技有挂!wpkai辅助实战... 黑科技有挂!wpkai辅助实战效果(透视)太嚣张了好像是有挂(2020已更新)(哔哩哔哩)黑科技有挂...
透视挂“hhpoker必备开挂... 透视挂“hhpoker必备开挂”详细透视辅助开挂AI教程-果然真的是有挂;hhpoker必备开挂软件...
一分钟带你了解“福建兄弟水挂”... 一分钟带你了解“福建兄弟水挂”太嚣张了透视辅助开挂细节揭秘-竟然真的是有挂;1、首先打开福建兄弟水挂...
黑科技辅助!云扑克是否有外挂(... 黑科技辅助!云扑克是否有外挂(透明挂黑科技)太无语了竟然真的有挂(2026已更新)(哔哩哔哩)云扑克...
透视智能ai“cloudpok... 透视智能ai“cloudpoker透视挂”详细透视辅助开挂wpk教程-切实真的有挂1、完成cloud...
交流学习经验“丽水茶苑辅助下载... 交流学习经验“丽水茶苑辅助下载app”太嚣张了透视辅助开挂解密教程-其实真的有挂1、起透看视 丽水茶...
黑科技脚本!Wepoke辅助从... 黑科技脚本!Wepoke辅助从哪里下载(辅助挂)太夸张了确实有挂(2025已更新)(哔哩哔哩)1、游...