FZU 2150 Fire Game(搜索)(从两点出发)

16 篇文章 0 订阅
11 篇文章 0 订阅

题目链接

题解:本体数据范围较小,暴力搜索即可解决问题。注意在搜索时,由于有两个出发点,要将两个出发点同时塞进搜索的队列中去,在进行搜索,就可以同时计算两个起点的搜索。

代码如下:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 10 + 5;
char map1[maxn][maxn];
int p1,p2,q1,q2;
struct InQ{
	int x;
	int y;
	int len;
	InQ(int x1 = 0,int y1 = 0,int len1 = 0) {x = x1;y = y1;len = len1;}
};
int numg;
int ans;
int n,m;
int dx[] = {0,0,-1,1};
int dy[] = {-1,1,0,0};
int vis[maxn][maxn];
char c;
queue<InQ> q;
void bfs(){
	int maxv = 0,cnt = 0;
	memset(vis,0,sizeof(vis));
	while(!q.empty()) q.pop();
	q.push(InQ(p1,q1,0));vis[p1][q1] = 1;cnt++;
	q.push(InQ(p2,q2,0));vis[p2][q2] = 1;cnt++;
	while(!q.empty()){
		InQ e = q.front();q.pop();
		int x = e.x,y = e.y;
		for(int i = 0;i < 4;i++){
			int nx = e.x + dx[i],ny = e.y + dy[i];
			if(nx > 0 && nx <= n && ny > 0 && ny <= m && map1[nx][ny] == '#' && !vis[nx][ny]) {
				vis[nx][ny] = 1;
				q.push(InQ(nx,ny,e.len + 1));
				cnt++;
				maxv = max(maxv,e.len + 1);
			}
		}
	}
	if(cnt >= numg) ans = min(ans,maxv);
}
		
				
			
char s[maxn];
int main()
{
	int kase;scanf("%d",&kase);
	for(int T = 1;T <= kase;T++){
		numg = 0;ans = 1000000;
		printf("Case %d: ",T);
		scanf("%d%d",&n,&m);
		c = getchar();
		for(int i = 1;i <= n;i++){
			for(int j = 1;j <= m;j++){
				scanf("%c",&map1[i][j]);
				if(map1[i][j] == '#') numg++;
			}
			c = getchar();
		}
		for(int i = 1;i <= n;i++){
			for(int j = 1;j <= m;j++){
				if(map1[i][j] != '#') continue;
				p1 = i;q1 = j;
				for(int i2 = i;i2 <= n;i2++){
					for(int j2 = 1;j2 <= m;j2++){
						if(i2 == i && j2 < j) continue;
						if(map1[i2][j2] != '#') continue;
						p2 = i2;q2 = j2;
						bfs();
					}
				}
			}
		}
		if(ans == 1000000) printf("-1\n");
		else printf("%d\n",ans);
	}
	return 0;
}
		

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值