D - Maze Master

3 篇文章 0 订阅
1 篇文章 0 订阅

D - Maze Master
题意:
从’.‘到’.'的最大距离。
思路:
BFS
这道bfs无起点无终点,数据范围小,所以遍历所有的bfs取bfs结果的最大值即可。
思路很明确,但是自己因为太久没做bfs的题代码实现能力有点弱,太慢了,要做一个优秀的Acmer不仅要头脑清楚,还要有fast coding的能力。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>

using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-10;
const int INF = 0x3f3f3f3f;
const ll mod  = 1e9 + 7;
const ll maxn = 1e6 + 5;

ll h,w;
char e[30][30];
int dis[30][30];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};


int bfs(int x,int y){
	memset(dis,INF,sizeof(dis));
	queue<P>q;
	q.push({x,y});
	dis[x][y]=0;
	while(!q.empty()){
		P top=q.front();
		q.pop();
		for(int i=0;i<4;i++){
			int tx=top.first+dx[i];
			int ty=top.second+dy[i];
			if(tx>0 && tx<=h && ty>0 && ty<=w && e[tx][ty]=='.' && dis[tx][ty]==INF){
				dis[tx][ty]=dis[top.first][top.second]+1;
				q.push({tx,ty});
			}
		}
	}
	int ans=0;
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			if(dis[i][j]!=INF) ans=max(ans,dis[i][j]);
		}
	}
	return ans;
}

int main(){
	cin>>h>>w;
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			cin>>e[i][j];
		}
	}
	int ans=0;
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			if(e[i][j]=='.') ans=max(ans,bfs(i,j));
		}
	}
	cout<<ans;
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值