跳到正文

个人记录

1-25bfs总结

Cabbage_Squirrel约 7 分钟
原文
文章信息与版本

文章信息

收录
2026年1月25日
更新
2026年1月26日
篇幅
约 7 分钟

版本

  1. v3当前
  2. v2
  3. v1

P1443 马的遍历

题意

有一个 n×mn \times m 的棋盘,在某个点 (x,y)(x, y) 上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步,不能到则输出 −1-1。

错因

没有用数组记录导致超时(nnm*m了,有大概256亿次,爆了),并且局部数组开太大还不清零。

Code

C++
#include<bits/stdc++.h>
using namespace std;
struct node{
	int x,y,k;
};
int dx[8] = {-1,-2,-2,-1,1,2,2,1};
int dy[8] = {2,1,-1,-2,2,1,-1,-2};
bool vis[1010][1010];
int dis[1010][1010],ex,ey,n,m;
queue<node>q;
void bfs() {
	q.push({ex,ey,1});
	vis[ex][ey] = 1;
	while(q.size()) {
		node t = q.front();
		q.pop();
		for(int i = 0;i < 8;i ++) {
			int tx = t.x + dx[i],ty = t.y + dy[i];
			if(vis[tx][ty] == 0 && tx > 0 && tx <= n && ty >= 1 && ty <= m) {
				node a;
				a.x = tx,a.y = ty,a.k = t.k + 1;
				q.push(a);
				dis[tx][ty] = t.k + 1;
				vis[tx][ty] = 1;
			}
		}
	}
}
int main() {
	cin >> n >> m;
	cin >> ex >> ey;
	bfs();
	for(int i = 1;i <= n;i ++) {
		for(int j = 1;j <= m;j ++) {
			if(i == ex && j == ey) {
			    cout << 0 << ' ';
			}else{
				cout << dis[i][j] - 1 << ' ';
			}
		}
		cout << endl;
	}
}

P1332 血色先锋队

题意

给定一个 nn 行 mm 列的矩阵,初始有 aa 个感染源,每个感染源会每小时向上下左右四个方向扩散,求 bb 个特领主最早被感染的时间。

错因

这里面有多个起点,应该在开始前将所有的起点先插入进去,可以把最开始存起点的部分封装成一个函数用多次。

Code

C++
#include<bits/stdc++.h>
using namespace std;
int n,m,a,b;
int sx[100005][2];
bool vis[505][505];
int dis[505][505];
int f[4][2] = {{0,-1},{0,1},{1,0},{-1,0}};
struct node{
	int x,y,st;
};
queue <node> q;
void p(int x,int y){
	node t;
	t.x = x;
	t.y = y;
	t.st = 0;
	q.push(t);
	vis[x][y] = true;
	return;
}

void bfs(){
	while(!q.empty()){
		node tmp;
		node t;
		for(int i = 0;i <= 3;i ++){
			t = q.front();
			int tx = t.x + f[i][0];
			int ty = t.y + f[i][1];
			if(tx >= 1 && tx <= n && ty >= 1 && ty <= m && !vis[tx][ty]) {
				vis[tx][ty] = true;
				tmp.x = tx;
				tmp.y = ty;
				tmp.st = t.st + 1;
				q.push(tmp);
			}
		}
		dis[t.x][t.y] = t.st;
		q.pop();
	}
}
int main(){
	memset(vis,false,sizeof(vis));
	cin >> n >> m >> a >> b;
	int x,y;
	for(int i = 1;i <= a;i ++) {
		cin >> x >> y;
		p(x,y);
	}
	for(int i = 1;i <= b;i ++){
		cin >> sx[i][1] >> sx[i][2];
	}
	bfs();
	for(int i = 1;i <= b;i ++){
		cout << dis[sx[i][1]][sx[i][2]] << endl;
	}
}

T725010 [USACO06OCT] Cows on Skates G

题意

给定一个 r×cr \times c 的网格,. 表示可通过,* 表示障碍。起点为 (1,1)(1,1),终点为 (r,c)(r,c),保证有解。每次可向相邻四格移动。要求输出任意一条从起点到终点的路径坐标序列,每行一个坐标。

错因

没有,我瞎写的,我自己都不知道在写啥。

解法

只要在 BFS 的时候记录走到的这个位置的上一步就行了。

Code

C++
#include<bits/stdc++.h>
using namespace std;
struct node {
    int x, y;
};
queue<node> q;
char c[120][80];
bool vis[120][80];
node pre[120][80];
int d[4][2] = {{0,1}, {1,0}, {-1,0}, {0,-1}};
int r, n;
void bfs() {
    node start;
    start.x = 1;
    start.y = 1;
    q.push(start);
    vis[1][1] = 1;
    pre[1][1] = {0, 0};
    while(q.size()) {
        node t = q.front();
        q.pop();
        if(t.x == r && t.y == n) {
            return;
        }
        for(int i = 0; i < 4; i++) {
            int tx = t.x + d[i][0];
            int ty = t.y + d[i][1];

            if(tx >= 1 && tx <= r && ty >= 1 && ty <= n &&
               c[tx][ty] == '.' && !vis[tx][ty]) {
                node a;
                a.x = tx;
                a.y = ty;
                q.push(a);
                vis[tx][ty] = 1;
                pre[tx][ty] = t;
            }
        }
    }
}
void print() {
    vector<node> path;
    node cur = {r, n};
    while(cur.x != 0 && cur.y != 0) {
        path.push_back(cur);
        cur = pre[cur.x][cur.y];
    }
    for(int i = path.size() - 1; i >= 0; i--) {
        cout << path[i].x << " " << path[i].y << endl;
    }
}
int main() {
    cin >> r >> n;
    for(int i = 1; i <= r; i++) {
        for(int j = 1; j <= n; j++) {
            cin >> c[i][j];
        }
    }
    bfs();
    print();
    return 0;
}
原文