跳到正文

个人记录

2026寒假编程jw老师BFS专题错题总结

XieMuzhu约 6 分钟
原文

这是 2026年1月26日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年1月26日
收录
2026年1月26日
更新
2026年11月7日
篇幅
约 6 分钟

版本

  1. v2当前
  2. v1

今日的BFS专题,满分500分,AC了3道,得到335分,在丢失的165分中,有很多细节是值得注意的。错题总结如下:

T1:T725354 仙岛求药

错因:

题中说到“输入有多组测试数据.”因为作业原题中有一道和这个相同但输入方式不同,一个是单组数据,一个是多组数据,没有看清楚就直接写,导致了题目的错误。

注意:

多测记得初始化+换行!

代码:```cpp

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 25;
int m, n, sx, sy, ex, ey;
char a[N][N];
bool f[N][N];
int dis[N][N];
int dx[5] = {0, 0, -1, 1};
int dy[5] = {-1, 1, 0, 0};
struct node{
	int x, y;
};
int bfs(){
	queue <node> q;
	q.push({sx, sy});
	f[sx][sy] = true;
	while(!q.empty()){
		int tx = q.front().x;
		int ty = q.front().y;
		q.pop();
		if(tx == ex && ty == ey) return dis[tx][ty];
		for(int i = 0; i < 4; i++){
			int nx = tx + dx[i];
			int ny = ty + dy[i];
			if(nx < 1 || nx > m || ny < 1 || ny > n) continue;
			if(f[nx][ny] == true || a[nx][ny] == '#') continue;
			f[nx][ny] = true;
			dis[nx][ny] = dis[tx][ty] + 1;
			q.push({nx, ny});
		}
	}
	return -1;
}
int main(){
    while(true){
        cin >> m >> n;
        memset(f, 0, sizeof(f));
        memset(dis, 0, sizeof(dis));
        memset(a, 0, sizeof(a));//多测记得要初始化
        if(m == 0 && n == 0) return 0;
    	for(int i = 1; i <= m; i++)
    		for(int j = 1; j <= n; j++){
    			cin >> a[i][j];
    			if(a[i][j] == '@') sx = i, sy = j;
    			if(a[i][j] == '*') ex = i, ey = j;
    		}
        cout << bfs() << '\n';//记得换行
    }
	return 0;
} 

T5: T725462 马与象

错因:

考场上没有注意到“每一方都可以连续走任意步”,导致思路错误,再加上时间关系,只能“骗分”了。

思路:

双BFS,一个BFS记录马走到任意一格的步数,另一个记录象走到任意一格的步数,最后取ans,将马的步数和象的步数加起来,就可以得到最终结果了~

注意:

得把dsih和dise都初始化为极大值,方便更新ans。

代码:

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 55, MAX = 1e9;
int n, hx, hy, ex, ey, ans = MAX;//这里需要注意ans不能初始化为INT_MAX,不然后面就更新不了了
int dxh[10] = {1, 1, 2, 2, -1, -1, -2, -2};
int dyh[10] = {-2, 2, -1, 1, -2, 2, -1, 1};
int dxe[5] = {-2, -2, 2, 2};
int dye[5] = {-2, 2, -2, 2};
int dish[N][N], dise[N][N];
bool f[N][N];
struct node{
	int x, y;
};
void bfs1(){//记录马走到任意一格的步数
	memset(f, false, sizeof(f));
	queue <node> q;
	q.push({hx, hy});
	f[hx][hy] = true;
	dish[hx][hy] = 0;
	while(!q.empty()){
		int tx = q.front().x;
		int ty = q.front().y;
		q.pop();
		for(int i = 0; i < 8; i++){
			int nx = tx + dxh[i];
			int ny = ty + dyh[i];
			if(nx < 0 || nx > n || ny < 0 || ny > n) continue;
			if(f[nx][ny] == true) continue;
			f[nx][ny] = true;
			dish[nx][ny] = dish[tx][ty] + 1;
			q.push({nx, ny});
		}
	}
	return;
}
void bfs2(){//记录象走到任意一格的步数
	queue <node> q;
	q.push({ex, ey});
	memset(f, false, sizeof(f));
	f[ex][ey] = true;
	dise[ex][ey] = 0;
	while(!q.empty()){
		int tx = q.front().x;
		int ty = q.front().y;
		q.pop();
		for(int i = 0; i < 4; i++){
			int nx = tx + dxe[i];
			int ny = ty + dye[i];
			if(nx < 0 || nx > n || ny < 0 || ny > n) continue;
			if(f[nx][ny] == true) continue;
			f[nx][ny] = true;
			dise[nx][ny] = dise[tx][ty] + 1;
			q.push({nx, ny});
		}
	}
	return;
}
signed main(){
	memset(dish, 0x3f, sizeof(dish));
	memset(dise, 0x3f, sizeof(dise));//记得初始化~
	cin >> n >> hx >> hy >> ex >> ey;
	bfs1();
	bfs2();
	for(int i = 0; i < n; i++)
		for(int j = 0; j < n; j++)
			ans = min(ans, dish[i][j] + dise[i][j]);
	if(ans == MAX) cout << -1;//特判
	else cout << ans;
	return 0;
}

这就是来追梦2026寒假第二讲XieMuyang的错题总结,请jw老师查收~