2021算法竞赛入门班第四节课【搜索】练习题

Jelly【简单bfs】

在这里插入图片描述
https://ac.nowcoder.com/acm/problem/201613

#include<bits/stdc++.h>
using namespace std;
const int N=110;
struct node{
    
    int x,y,z,step;};
char a[N][N][N];
int st[N][N][N],n;
int dx[6]={
    
    -1,0,0,1,0,0};
int dy[6]={
    
    0,1,-1,0,0,0};
int dz[6]={
    
    0,0,0,0,-1,1};
int bfs()
{
    
    
	queue<node>q; q.push({
    
    1,1,1,1});
	st[1][1][1]=1;
	while(q.size())
	{
    
    
		auto temp=q.front(); q.pop();
		int x=temp.x,y=temp.y,z=temp.z,t=temp.step;
		if(x==n&&y==n&&z==n) return t;
		for(int i=0;i<6;i++)
		{
    
    
			int tempx=x+dx[i];
			int tempy=y+dy[i];
			int tempz=z+dz[i];
			if(tempx<1||tempx>n||tempy<1||tempy>n||tempz<1||tempz>n) continue;
			if(st[tempz][tempx][tempy]) continue;
			if(a[tempz][tempx][tempy]=='*') continue;
			st[tempz][tempx][tempy]=1;
			q.push({
    
    tempx,tempy,tempz,t+1});
		}
	}
    return -1;
}
int main(void)
{
    
    
	cin>>n;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			for(int z=1;z<=n;z++)
				cin>>a[i][j][z];
	cout<<bfs();
	return 0;
}

maze【建图求最短路】

在这里插入图片描述
https://ac.nowcoder.com/acm/problem/15665
根据题意建图,跑最短路即可

#include<bits/stdc++.h>
using namespace std;
const int N=1010;
const int M=1e5*4+10;
typedef pair<int,int> PII;
int dist[M],h[M],e[M],w[M],ne[M],idx,vis[M];
string a[N];
int n,m,t,st,ed;
void init()
{
    
    
	memset(vis,0,sizeof vis);
	memset(h,-1,sizeof h);
	idx=0;
}
void add(int a,int b,int c)
{
    
    
	e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}
int get(int x,int y) {
    
    return x*n+y;}
void build(int x,int y)
{
    
    
	int dx[2]={
    
    0,1},dy[2]={
    
    1,0};
	for(int i=0;i<2;i++)
	{
    
    
		int tempx=x+dx[i];
		int tempy=y+dy[i];
		if(tempx<0||tempx>=n||tempy<0||tempy>=m) continue;
		if(a[tempx][tempy]=='#') continue;
		int s1=get(x,y),s2=get(tempx,tempy);
		add(s1,s2,1),add(s2,s1,1);
	}
}
void Dijkstra(int st)
{
    
    
	memset(dist,0x3f,sizeof dist);
	priority_queue<PII,vector<PII>,greater<PII>>q; q.push({
    
    0,st});
	dist[st]=0;
	while(q.size())
	{
    
    
		auto temp=q.top(); q.pop();
		int u=temp.second;
		if(vis[u]) continue;
		vis[u]=1;
		for(int i=h[u];i!=-1;i=ne[i])
		{
    
    
			int j=e[i];
			if(dist[j]>dist[u]+w[i])
			{
    
    
				dist[j]=dist[u]+w[i];
				q.push({
    
    dist[j],j});
			}
		}
	}
}
int main(void)
{
    
    
	while(cin>>n>>m>>t)
	{
    
    
		init();
        for(int i=0;i<n;i++) cin>>a[i];
		for(int i=0;i<n;i++)
		{
    
    
			for(int j=0;j<m;j++)
			{
    
    
				if(a[i][j]!='#')  build(i,j);
			    if(a[i][j]=='S') st=get(i,j);
				if(a[i][j]=='T') ed=get(i,j);
			}
		}
		while(t--)
		{
    
    
			int x,y,xx,yy; cin>>x>>y>>xx>>yy;
			if(a[x][y]!='#'&&a[xx][yy]!='#') add(get(x,y),get(xx,yy),3);
		}
		Dijkstra(st);
        if(dist[ed]>0x3f3f3f3f/2) cout<<-1<<endl;
		else cout<<dist[ed]<<endl;
	}
	return 0;
}

wyh的迷宫【BFS】

在这里插入图片描述
https://ac.nowcoder.com/acm/problem/15434

#include<bits/stdc++.h>
using namespace std;
const int N=510;
char a[N][N];
int st[N][N],t,n,m; 
int stx,sty,edx,edy;
struct node{
    
    int x,y,step;};
int bfs()
{
    
    
	queue<node>q; q.push({
    
    stx,sty,0});
	st[stx][sty]=1;
	while(q.size())
	{
    
    
		auto temp=q.front(); q.pop();
		int x=temp.x,y=temp.y,t=temp.step;
		if(x==edx&&y==edy) return t;
		int dx[4]={
    
    -1,0,0,1};
		int dy[4]={
    
    0,1,-1,0};
		for(int i=0;i<4;i++)
		{
    
    
			int tempx=x+dx[i];
			int tempy=y+dy[i];
			if(tempx<0||tempx>=n||tempy<0||tempy>=m) continue;
			if(a[tempx][tempy]=='x') continue;
			if(st[tempx][tempy]) continue;
			st[tempx][tempy]=1;
			q.push({
    
    tempx,tempy,t+1});
		}
	}
	return -1;
}
int main(void)
{
    
    
	cin>>t;
	while(t--)
	{
    
    
		cin>>n>>m;
		memset(st,0,sizeof st);
		for(int i=0;i<n;i++)
			for(int j=0;j<m;j++)
			{
    
    
				cin>>a[i][j];
				if(a[i][j]=='s') stx=i,sty=j;
				if(a[i][j]=='t') edx=i,edy=j;
			}
		int ans=bfs();
		if(ans!=-1) puts("YES");
		else puts("NO");
				
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_46527915/article/details/121885940