跳到正文

算法·理论

bfs测试

shifuanOwO约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年7月8日
收录
2026年7月8日
更新
2026年11月10日
篇幅
约 9 分钟
获赞
1

版本

  1. v1当前

先来分数

题目pts
T1 T672724100
T2 P1588100
T3 P14326100
T4 P1227010
T5 T72577411
T6 P23600
总分321

呜呜呜,第6题就因为座标,爆0。

题目讲解

T1T672724

题意

一个n×nn×n的迷宫,1代表不能走,0代表能走,求从起点走到终点要走多少步(保证一定有解)。

思路

nx,ny(数组偏移)

nx[4]={0,0,-1,1}
ny[4]={-1,1,0,0}(前后左右)

BFS

continue的条件:

  1. 越界
  2. 走过
  3. 障碍

return条件:

  1. 栈为空
  2. 走到终点

code

C++
#include<bits/stdc++.h>
#define ll long long
#define usg unsigned long long
#define ch char
#define str string
#define bl bool
#define db double
#define iswscc ios::sync_with_stdio(0);cin.tie();cout.tie();
using namespace std;
const ll N=2e5+2;
ll n,sx,sy,ex,ey;
struct node
{
	ll x,y,s;
};
queue<node>q;
ch a[1001][1001];
ll t[1001][1001];
ll nx[4]={0,0,-1,1};
ll ny[4]={-1,1,0,0};
ll bfs()
{
	while(!q.empty())
	{
		node u=q.front();
		q.pop();
		for(ll i=0;i<4;i++)
		{
			ll xx=u.x+nx[i],yy=u.y+ny[i];
			if(xx<1||yy<1||xx>n||yy>n) continue;
			if(a[xx][yy]=='1'||t[xx][yy]) continue;
			if(xx==ex&&yy==ey) return u.s+1;
			q.push({xx,yy,u.s+1});
			t[xx][yy]=1;
		}
	}
	return -1;
}
int main()
{
	iswscc
	cin>>n;
	for(ll i=1;i<=n;i++)
	{
		for(ll j=1;j<=n;j++) cin>>a[i][j];
	}
	cin>>sx>>sy>>ex>>ey;
	if(sx==ex&&sy==ey)
	{
		cout<<0;
		return 0;
	}
	q.push({sx,sy,0});
	t[sx][sy]=1;
	cout<<bfs();
	return 0;
}

T2P1588

题意

TT组数据,每组数据给定起点和终点,移动方式如下(设当前位置为k):

  1. 移动到k+1k+1
  2. 移动到k−1k-1
  3. 移动到2×k2×k

求从x到y要走几次。

思路

位置移动
C++
node u=q.front();
for(ll i=1;i<=n;i++)
{
    ll xx;
    if(i==1) xx=u.x+1;
    else if(i==2) xx=u.x-1;
    else xx=u.x*2;
}
BFS

continue条件:

  1. 越界
  2. 来过

return条件:

  1. 栈为空
  2. 到达终点

细节

  1. 边界是0和max(起点,终点)*2因为k可以×2。
  2. 注意清空。

code

C++
#include<bits/stdc++.h>
#define ll long long
#define usg unsigned long long
#define ch char
#define str string
#define bl bool
#define db double
#define iswscc ios::sync_with_stdio(0);cin.tie();cout.tie();
using namespace std;
const ll N=2e5+2;
ll k,sx,ex;
struct node
{
	ll x,s;
};
queue<node>q;
ll t[N];
ll bfs()
{
	while(!q.empty())
	{
		node u=q.front();
		q.pop();
		for(ll i=1;i<4;i++)
		{
			ll xx;
			if(i==1) xx=u.x+1;
			if(i==2) xx=u.x-1;
			if(i==3) xx=u.x*2;
			if(xx<1||xx>max(sx,ex)*2) continue;
			if(t[xx]) continue;
			if(xx==ex) return u.s+1;
			q.push({xx,u.s+1});
			t[xx]=1;
		}
	}
	return -1;
}
int main()
{
	iswscc
	cin>>k;
	while(k--)
	{
		cin>>sx>>ex;
		q.push({sx,0});
		t[sx]=1;
		if(sx==ex) cout<<0;
		else cout<<bfs();
		cout<<"\n";
		while(!q.empty()) q.pop();
		memset(t,0,sizeof(t));
	}
	return 0;
}

T3 P14326

题意

给定一个由”.”和”#“组成的一个N×MN×M的矩阵,踩的顺序是”.#.#……”或”#.#.#……”(只能走到前后左右)。问能否从(1,1)(1,1)走到(n,m)(n,m)

BFS思路

continue条件:

  1. 越界
  2. 来过
  3. 与之前一格相同

return条件:

  1. 栈为空
  2. 到达终点

code

C++
#include<bits/stdc++.h>
#define ll long long
#define usg unsigned long long
#define ch char
#define str string
#define bl bool
#define db double
#define iswscc ios::sync_with_stdio(0);cin.tie();cout.tie();
using namespace std;
const ll N=2e5+2;
ll n,m;
struct node
{
	ll x,y,s;
};
queue<node>q;
ch a[501][501];
ll t[501][501];
ll nx[4]={0,0,-1,1};
ll ny[4]={-1,1,0,0};
ll bfs()
{
	while(!q.empty())
	{
		node u=q.front();
		q.pop();
		for(ll i=0;i<4;i++)
		{
			ll xx=u.x+nx[i];
			ll yy=u.y+ny[i];
			if(xx<1||yy<1||xx>n||yy>m) continue;
			if(t[xx][yy]||a[xx][yy]==a[u.x][u.y]) continue;
			if(xx==n&&yy==m) return u.s+1;
			q.push({xx,yy,u.s+1});
			t[xx][yy]=1;
		}
	}
	return -1;
}
int main()
{
	iswscc
	cin>>n>>m;
	for(ll i=1;i<=n;i++)
	{
		for(ll j=1;j<=m;j++) cin>>a[i][j];
	}
	if(1==n&&1==m)
	{
		cout<<0;
		return 0;
	}
	q.push({1,1,0});
	t[1][1]=1;
	cout<<bfs();
	return 0;
}

T6 P2360

其实没啥好讲的,只有3个点要讲:

  1. 因为是三维,所以结构体里面要添加z轴
  2. 坐标不是a[x][y][z],而是a[z][x][y];
  3. 数组偏移如下
C++
ll nx[6]={0,0,0,0,-1,1};
ll ny[6]={0,0,-1,1,0,0};
ll nz[6]={-1,1,0,0,0,0};

总结

还不错,前三道题都想到了正解,不过骗的分还不够多。

原文