求助万能的谷民(违规紫衫)
  • 板块灌水区
  • 楼主LSR120218_juruo
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/8/4 16:33
  • 上次更新2024/8/4 18:07:50
查看原帖
求助万能的谷民(违规紫衫)
971101
LSR120218_juruo楼主2024/8/4 16:33

给为帮我看一下为什么总是运行出错?

#include<bits/stdc++.h>
using namespace std;

int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};

int main() {
	// ...(变量声明和输入部分)
	int n, m, start_x, start_y, end_x, end_y, q[10010][3], h = 1, t = 1, b[200][200] = {0};
	char a[200][200];
	
	cin >> n >> m >> start_x >> start_y >> end_x >> end_y;
	
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> a[i][j];
		}
	}
	// 初始化队列,设置入口坐标
	q[1][0] = start_x;
	q[1][1] = start_y;
	q[1][2] = 0; // 起点的步数为0
	b[start_x][start_y] = 1; // 标记起点已访问
	
	// 开始广度优先搜索
	while (h <= t) {
		// 打印当前队列头部的坐标
		cout << "处理队列头部: (" << q[h][0] << ", " << q[h][1] << ")" << endl;
		
		// 遍历四个可能的移动方向
		for (int i = 0; i < 4; i++) {
			int nr = q[h][0] + dx[i], nc = q[h][1] + dy[i];
			
			// 打印尝试扩展的坐标
			cout << "尝试扩展到: (" << nr << ", " << nc << ")" << endl;
			
			// 检查新位置是否在地图内、是否可走以及是否未访问过
			if (nr >= 0 && nr <= n && nc >= 0 && nc <= m && a[nr][nc] == '.'&&b[nr][nc]==0) {
				t++;
				q[t][0] = nr;
				q[t][1] = nc;
				q[t][2] = q[h][2] + 1;
				b[nr][nc] = 1; // 更新步数
				
				// 打印添加到队列的坐标和步数
				cout << "添加到队列: (" << nr << ", " << nc << ") 步数: " << q[t][2] << endl;
			} else {
				// 打印不可扩展的坐标
				cout << "无法扩展到: (" << nr << ", " << nc << ")" << endl;
			}
		}
		h++; // 移动头指针,处理下一个队列元素
	}
	
	// 如果没有找到路径,打印队列的状态
	cout << "队列耗尽,未找到路径。" << endl;
	for (int i = 1; i <= t; i++) {
		cout << "队列元素 " << i << ": (" << q[i][0] << ", " << q[i][1] << ", " << q[i][2] << ")" << endl;
	}
	
	cout << -1 << endl;
	return 0;
}
//10 10
//1 1
//9 10
//##########
//#..#######
//##..######
//###..#####
//####.#####
//####.#####
//####..####
//#####...##
//#######...
//##########

thx

2024/8/4 16:33
加载中...