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

Posted 辉小歌

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021算法竞赛入门班第四节课搜索练习题相关的知识,希望对你有一定的参考价值。

Jelly【简单bfs】


https://ac.nowcoder.com/acm/problem/201613

#include<bits/stdc++.h>
using namespace std;
const int N=110;
struct nodeint 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 nodeint 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;

以上是关于2021算法竞赛入门班第四节课搜索练习题的主要内容,如果未能解决你的问题,请参考以下文章

2021算法竞赛入门班第九节课线段树练习题

2021算法竞赛入门班第八节课数学习题

2021算法竞赛入门班第二节课递归分治二分练习题

2021算法竞赛入门班第十节课字符串练习题

2021算法竞赛入门班第一节课枚举贪心习题

2021算法竞赛入门班第三节课堆栈队列并查集等习题