bfs+最大联通分量(最多财宝数目),bfs+最短路

一,bfs+最短路

题目链接

题意翻译

【题目描述】 这题是一个三维的迷宫题目,其中用‘.’表示空地,‘#’表示障碍物,‘S’表示起点,‘E’表示终点,求从起点到终点的最小移动次数,解法和二维的类似,只是在行动时除了东南西北移动外还多了上下。可以上下左右前后移动,每次都只能移到相邻的空位,每次需要花费一分钟,求从起点到终点最少要多久。

【输入】 多组测试数据。

一组测试测试数据表示一个三维迷宫:

前三个数,分别表示层数、一个面的长和宽,后面是每层的平面图。前三个数据为三个零表示结束。

【输出】 最小移动次数。

Translated by @剑圣夜雨声烦

输入输出样例

输入 #1

3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0

输出 #1复制

Escaped in 11 minute(s).
Trapped!

代码:注意step的变化(step保存最短长度)

# include<bits/stdc++.h>
using namespace std;
int vis[32][32][32];
char Map[32][32][32];
struct node{
	int x;
	int y;
	int z;
	char value;
	int step;
};
int dir[6][3]={{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1} };
int main(){
	int l,r,c;
	while(cin>>l>>r>>c){
	if(l==0&&r==0&&c==0)
	return 0;
	for(int i=1;i<=l;i++){
		for(int j=1;j<=r;j++){
			for(int k=1;k<=c;k++){
				cin>>Map[i][j][k];
			}
		}
	}
	queue<node>q;
	for(int i=1;i<=l;i++){
		for(int j=1;j<=r;j++){
			for(int k=1;k<=c;k++){
				if(Map[i][j][k]=='S'){
					vis[i][j][k]=1;
					q.push(node{i,j,k,'S'});
				}
			}
		}
	}
	
	while(!q.empty()){
		node v=q.front();
		
		if(v.value=='E'){
				
				cout<<"Escaped in "<<v.step<<" minute(s)."<<"\n";
				break;
			}
		q.pop();
		for(int i=0;i<6;i++){
			node next;
			int xx=dir[i][0]+v.x;
			int yy=dir[i][1]+v.y;
			int zz=dir[i][2]+v.z;
			next.x=xx;
			next.y=yy;
			next.z=zz;
			if(xx>=1&&xx<=l&&yy>=1&&yy<=r&&zz>=1&&zz<=c&&vis[xx][yy][zz]==0&&(Map[xx][yy][zz]=='.'||Map[xx][yy][zz]=='E')){
				vis[xx][yy][zz]=1;
				next.step=v.step+1;
				next.value=Map[xx][yy][zz];
				q.push(next);
			}
		}
	}
	if(q.empty())
	cout<<"Trapped!"<<"\n";
	
	}
}

二,bfs+最大联通分量(一次搜索最多财宝数目)

题目链接

J. Binbin's treasure map

描述

 Binbin is a clever boy who likes to wear a skirt. One day,he saw some beautiful skirts on Taobao,but he had not enough money, which made him sad.

  In order to make Binbin feel happy again, his's friend Xu1024 gave him atreasuremap. Thetreasure map is composed of three characters: '.', '#', '$'.

  Character '.' indicates that this position is empty and passable;

  Character '#' indicates that this position is blocked by a stone and not passable;

  Character '$' indicates that this position has 1 coin on it and is passable.

  The locations outside the treasure map are all empty and passable.

  Binbin can move up, down, left, and right, but not at an angle.

  Firstly, Binbin can choose any grid as starting point. Because Binbin wants to wear a skirt so much, he has a chance to teleport to a certain grid as another starting point.

  Binbin wants to know how many coins he could eventually collect to buy skirts.

  Can you help him? 

输入

The first line of each group contains two numbers N and M(1<=N,M<=500),representing thenumber of rows and the number of columns of treasure map. Then there are next N lines, each line contains M characters. which describe the treasure map. 

输出

Ouput a integer representing the maxinum coin(s) Binbin would collect.

输入

4 4 
.$.$
.##.
#$#.
.#.#

输出

3

输入

4 4
.#$#
####
..$$
$$$$

输出

7

找$的两个最大的连通分量,求和即可。

ac代码:

# include<bits/stdc++.h>
using namespace std;
struct node{
	int x;
	int y;
	char z;
};
bool cmp(int a,int b){
	return a>b;
}
int dir[4][2]={0,-1,-1,0,0,1,1,0};
int vis[510][510];
char Map[510][510];
int main(){
	int n,m;
	cin>>n>>m;
	int sum[250006]={0};
	int t=-1;
	char s;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>s;
			Map[i][j]=s;
		}
	}
	for(int i=0;i<=n+1;i++){
		if(i==0||i==n+1){
			for(int j=0;j<=m+1;j++){
				Map[i][j]='.';
			}
		}else{
			Map[i][0]='.';
			Map[i][m+1]='.';
		}
	}
	
//	cout<<Map[n-1][m-1];
	queue<node>q;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(Map[i][j]=='$'&&vis[i][j]==0){
				t++;
				vis[i][j]=1;
				sum[t]++;
		//		cout<<i<<' '<<j<<"\n";
				
				q.push(node{i,j,Map[i][j]});
				while(!q.empty()){
					node v=q.front();
					q.pop();
					for(int k=0;k<4;k++){
						int xx=dir[k][0]+v.x;
						int yy=dir[k][1]+v.y;
						if(xx>=0&&xx<=n+1&&yy>=0&&yy<=m+1&&(Map[xx][yy]=='$'||Map[xx][yy]=='.')&&vis[xx][yy]==0){
							vis[xx][yy]=1;
							if(Map[xx][yy]=='$')
							sum[t]++;
						//	cout<<Map[xx][yy]<<" ";
							q.push(node{xx,yy,Map[xx][yy]});
						}
					}
				}
			}
		}
	}
	//cout<<"r4re";
	sort(sum,sum+250006,cmp);
	cout<<sum[0]+sum[1];
} 

阿里云建站—为企业提供互联网“快”服务
2020年因为一场突如其来的疫情,不少企业受到了严重冲击,疫情的冲击力对传统“纯线
下”行业的危害最大,互联网女皇玛丽·米克(MaryMeeker)4月17日发布了著名的年度互
联网趋势报告,报告中指出:拥有强大的互联网线上线下融合能力的企业在疫情中的表现最好,
线上线下融合的趋势已经存在一段时间了,但是疫情让这种需求变得更加的迫切。
如果你的企业完全依附于传统的、纯线下的经验模式,那么在2020年你将“必死无疑”,
一场巨大的,前所未有的互联网革命已经到来!
阿里云建站为了助力各行各业复工复产,疫情期间“马不停蹄”为数以万计的企业快速完成
建站,为他们朝着“线上线下融合”或者“纯线上”的互联网经营模式迈进,打下了坚实的基础。
“云·速成美站”模板建站1天就能上线,不懂技术没关系,打字就能建网站。千套网站模
板免费提供,百元就能建官网,一价全包,无任何隐形消费。
“云·企业官网”定制建站1周就能上线,高端量身定制建站,千元建官网无需自己动手,
建站专家1对1网站策划及设计,专业省心之选。
疫情,是一场大浪淘沙,每一次危机背后都隐藏着机会,危机越大,机会也越大。大环境
已经改变,如果你不努力不去改变来迎合这个大环境,那你必将被淘汰。
阿里云助力企业建站,优惠多多,福利多多,详情请点击如下链接
https://www.aliyun.com/minisite/goods?userCode=tz7kl4ag

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值