跳到正文

个人记录

2.3 上午 【广搜进阶1】

WUzongze约 13 分钟
原文
文章信息与版本

文章信息

收录
2026年2月4日
更新
2026年2月4日
篇幅
约 13 分钟

版本

  1. v2当前
  2. v1

比赛信息

比赛名称:2.3 上午 【广搜进阶1】

时间:2026.2.3 上午

链接:2.3 上午 【广搜进阶1】

bfs模板

C++
#include <bits/stdc++.h>
using namespace std;
struct node{
	int x,y;
};
int xx,yy,sx,sy,n;
int dx[] = {0,-1,1,0};
int dy[] = {1,0,0,-1};
char c;
int vis[10000][10000];
int dis[10000][10000];
int a[10000][10000];
bool check(int x,int y){
	return x >= 1 && x <= n && y <= n && y >= 1 && vis[x][y] == 0 && a[x][y] == 0;
}
void bfs(){
	queue <node> q;
	q.push({sx,sy});
	vis[sx][sy] = 1;
	while (q.size()){
		node u = q.front();
		q.pop();
		if (u.x == xx && u.y == yy){
			cout<<dis[u.x][u.y];
			return;
		}
		for (int i = 0;i < 4;i++){
			int xxx = u.x + dx[i];
			int yyy = u.y + dy[i];
			if (check(xxx,yyy)){
				vis[xxx][yyy] = 1;
				dis[xxx][yyy] = dis[u.x][u.y] + 1;
				q.push({xxx,yyy});
			}
		}
	}
}
int main(){
	cin>>n;
	for (int i = 1;i <= n;i++){
		for (int j = 1;j <= n;j++){
			cin>>c;
			if (c == '0'){
				a[i][j] = 0;
			}
			else{
				a[i][j] = 1;
			}
		}
	}
	cin>>sx>>sy>>xx>>yy;
	bfs();
	return 0;
}

题目

离开中山路

题号:P1746

链接:离开中山路

思路:(模板题)

代码:(见“bfs模板”)

马的遍历

题号:P1443

链接:马的遍历

思路:和第一题的区别是马走的是日字,有8种走法

代码:

C++
#include <bits/stdc++.h>
using namespace std;
struct node{
	int x,y;
};
int xx,yy,sx,sy,n,m;
int dx[] = {-2,-2,2,2,-1,1,-1,1};
int dy[] = {-1,1,-1,1,-2,-2,2,2};
char c;
int vis[10000][10000];
int dis[10000][10000];
bool flag[10000][10000];
int a[10000][10000];
bool check(int x,int y){
	return x >= 1 && x <= n && y <= m && y >= 1 && vis[x][y] == 0;
}
void bfs(){
	queue <node> q;
	q.push({sx,sy});
	vis[sx][sy] = 1;
	dis[sx][sy] = 0;
	while (q.size()){
		node u = q.front();
		q.pop();
		for (int i = 0;i < 8;i++){
			int xxx = u.x + dx[i];
			int yyy = u.y + dy[i];
			if (check(xxx,yyy)){
				vis[xxx][yyy] = 1;
				flag[xxx][yyy] = 1;
				dis[xxx][yyy] = dis[u.x][u.y] + 1;
				q.push({xxx,yyy});
			}
		}
	}
}
int main(){
	cin>>n>>m;
	cin>>sx>>sy;
	for (int i = 1;i <= n;i++){
		for (int j = 1;j <= m;j++){
			dis[i][j] = -1;
		}
	}
	bfs();
	for (int i = 1;i <= n;i++){
		for (int j = 1;j <= m;j++){
			cout<<dis[i][j]<<" ";
		}
		cout<<endl;
	}
	return 0;
}

血色先锋队

题号:P1332

链接:血色先锋队

思路:所有感染源一起入队即可

代码:

C++
#include <bits/stdc++.h>
using namespace std;
struct node{
	int x,y;
};
queue <node> q;
int xx,yy,sx,sy,n,m,a,b;
int dx[] = {0,-1,1,0};
int dy[] = {1,0,0,-1};
int vis[10000][10000];
int dis[10000][10000];
bool check(int x,int y){
	return x >= 1 && x <= n && y <= n && y >= 1 && vis[x][y] == 0;
}
void bfs(){
	while (q.size()){
		node u = q.front();
		q.pop();
		for (int i = 0;i < 4;i++){
			int xxx = u.x + dx[i];
			int yyy = u.y + dy[i];
			if (check(xxx,yyy)){
				vis[xxx][yyy] = 1;
				dis[xxx][yyy] = dis[u.x][u.y] + 1;
				q.push({xxx,yyy});
			}
		}
	}
}
int main(){
	cin>>n>>m>>a>>b;
	for (int i = 1;i <= a;i++){
		cin>>sx>>sy;
		q.push({sx,sy});
		vis[sx][sy] = 1;
		dis[sx][sy] = 0;
	}
	bfs();
	while(b--){
		cin>>xx>>yy;
		cout<<dis[xx][yy]<<endl;
	}
	return 0;
}

[USACO06OCT] Cows on Skates G

题号:P6207

链接:Cows on Skates G

思路:需要用一个新的数组存储路径

代码:

C++
#include <bits/stdc++.h>
using namespace std;
struct node{
	int x,y;
};
int xx,yy,sx,sy,n,m;
int dx[] = {0,-1,1,0};
int dy[] = {1,0,0,-1};
char c;
int vis[1000][1000];
int dis[1000][1000];
node pre[1000][1000];
int a[1000][1000];
bool check(int x,int y){
	return x >= 1 && x <= n && y <= m && y >= 1 && vis[x][y] == 0 && a[x][y] == 0;
}
void write(int x,int y){
	if (x == 1 && y == 1){
		cout<<x<<" "<<y<<endl;
		return;
	}
	write(pre[x][y].x,pre[x][y].y);
	cout<<x<<" "<<y<<endl;
}
void bfs(){
	queue <node> q;
	q.push({1,1});
	vis[1][1] = 1;
	dis[1][1] = 0;
	while (q.size()){
		node u = q.front();
		q.pop();
		if (u.x == n && u.y == m){
			write(n,m);
			return;
		}
		for (int i = 0;i < 4;i++){
			int xxx = u.x + dx[i];
			int yyy = u.y + dy[i];
			if (check(xxx,yyy)){
				vis[xxx][yyy] = 1;
				dis[xxx][yyy] = dis[u.x][u.y] + 1;
				pre[xxx][yyy] = {u.x,u.y};
				q.push({xxx,yyy});
			}
		}
	}
}
int main(){
	cin>>n>>m;
	for (int i = 1;i <= n;i++){
		for (int j = 1;j <= m;j++){
			cin>>c;
			if (c == '.'){
				a[i][j] = 0;
			}
			else{
				a[i][j] = 1;
			}
		}
	}
	bfs();
	return 0;
}

[COCI 2006/2007 #1] Slikar

题号:04328

链接:[COCI 2006/2007 #1] Slikar

代码:

C++
#include <bits/stdc++.h>
using namespace std;
int dx[4] = {0, -1, 1, 0};
int dy[4] = {-1, 0, 0, 1};
const int maxn = 1e3 + 7;
bool vis[maxn][maxn];
char c[maxn][maxn];
int sx, sy;
int ex, ey;
int dis1[maxn][maxn]; // 洪水到达时间
int dis2[maxn][maxn]; // 画家到达时间
int n, m;
struct Node {
	int x, y;
};
// 洪水BFS的检查函数
bool check1(int x, int y) {
	return x >= 1 && x <= n && y >= 1 && y <= m && !vis[x][y] && c[x][y] != 'X'
	       && c[x][y] != 'D';
}
// 画家BFS的检查函数
bool check2(int x, int y, int t) {
	if (x < 1 || x > n || y < 1 || y > m) return false;
	if (vis[x][y]) return false;
	if (c[x][y] == 'X') return false;
	if (c[x][y] == 'D') return true; // 终点总是可以走
	// 对于其他点,需要洪水到达时间 > t(严格大于)
	return dis1[x][y] > t;
}
void bfs1() {
	queue<Node> Q;
	memset(vis, 0, sizeof(vis)); // 初始化vis数组
	memset(dis1, 0x3f, sizeof(dis1));

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (c[i][j] == '*') {
				vis[i][j] = 1;
				Q.push({i, j});
				dis1[i][j] = 0;
			}
		}
	}

	while (!Q.empty()) {
		Node u = Q.front();
		Q.pop();
		for (int i = 0; i < 4; i++) {
			int xx = u.x + dx[i];
			int yy = u.y + dy[i];
			if (check1(xx, yy)) {
				vis[xx][yy] = 1;
				Q.push({xx, yy});
				dis1[xx][yy] = dis1[u.x][u.y] + 1;
			}
		}
	}
}
void bfs2() {
	queue<Node> q;
	q.push({sx, sy});
	memset(vis, 0, sizeof(vis));
	memset(dis2, 0x3f, sizeof(dis2));
	vis[sx][sy] = 1;
	dis2[sx][sy] = 0;

	while (!q.empty()) {
		Node u = q.front();
		q.pop();

		// 修正:判断到达终点
		if (u.x == ex && u.y == ey) {
			cout << dis2[u.x][u.y];
			return;
		}



		for (int i = 0; i < 4; i++) {
			int xx = u.x + dx[i];
			int yy = u.y + dy[i];
			int nt = dis2[u.x][u.y] + 1; // 到达下一个点的时间

			if (check2(xx, yy, nt)) {
				vis[xx][yy] = 1;
				q.push({xx, yy});
				dis2[xx][yy] = nt;
			}
		}
	}

	cout << "KAKTUS";
}
int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> c[i][j];
			if (c[i][j] == 'D') {
				ex = i;
				ey = j;
			}
			if (c[i][j] == 'S') {
				sx = i;
				sy = j;
			}
		}
	}

	bfs1();
	bfs2();

	return 0;
}
原文