FZU 2150(优化BFS)

15 篇文章 0 订阅
14 篇文章 0 订阅

题目链接:戳此进入

思路:数据范围比较小,每次进两个点BFS,搜完后判断一下,是否还有没有烧的草,如果有就标记。

优化思路:纯暴力会有点重复,我们只需要保证这两个点如果搜索过就跳过。

优化AC :时间差一半

#include<iostream>
#include<cstring>
#include<string.h>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
const int maxn = 15;
const int INF = 999;
char maze[maxn][maxn];
bool vis[maxn][maxn]; 
int dist[maxn][maxn];
bool supvis[maxn][maxn][maxn][maxn];
int n, m;
int X[4] = {1, -1, 0, 0};
int Y[4] = {0, 0, 1, -1};

struct node{
	int x, y;
	int step;
	node(){
		step = 0;
	}
};

bool check(node now){
	if(now.x < 1 || now.x > n || now.y < 1 || now.y > m){
		return 0;
	}
	return 1;
}
int BFS(node s1, node s2){
	memset(vis, 0, sizeof(vis));
	memset(dist, 0, sizeof(dist));
	vis[s1.x][s1.y] = 1;
	vis[s2.x][s2.y] = 1;
	dist[s1.x][s2.y] = 0;
	dist[s1.x][s2.y] = 0;
	queue<node> q;
	q.push(s1);
	q.push(s2);
	node front, next;
	int ans = 0;	
	while(!q.empty()){
		front = q.front();
		q.pop();
		for(int i = 0; i < 4; i ++){
			next.x = front.x + X[i];
			next.y = front.y + Y[i];
			if(!check(next))
				continue;
			if(vis[next.x][next.y]) 
				continue;
			if(maze[next.x][next.y] == '.')
				continue;
			vis[next.x][next.y] = 1;
			next.step = front.step + 1;
 			q.push(next);
			ans = max(ans, next.step);
		}
	}
	bool flag = 0;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			if(maze[i][j] == '#' && vis[i][j] == 0){
				flag = 1;
			}
		}
	}
	if(flag)
		return -1;
	else
		return ans;
}
int main(){
	int T;
	scanf("%d", &T); 
	int q = 1;
	while(T--){
		scanf("%d%d", &n, &m);
		getchar();
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= m;j ++){
				scanf("%c", &maze[i][j]); 
			}
			getchar();
		} 
		int ans = INF;
		memset(supvis, 0, sizeof(supvis));
		node fire1, fire2;
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= m; j++){
				if(maze[i][j] == '#'){
					fire1.x = i;
					fire1.y = j;	
					for(int k = 1; k <= n; k++){
						for(int z = 1; z <= m; z++){
							if(maze[k][z] == '#' && supvis[i][j][k][z] == 0){
								fire2.x = k;
								fire2.y = z;
								supvis[i][j][k][z] = 1;
								supvis[k][z][i][j] = 1;
								int flag = BFS(fire1, fire2);
								if(flag != -1) {
									ans = min(ans, flag); 
								}	
							}
						}
					}
				}
			}
		}
		
        printf("Case %d: ", q++);
		if(ans == INF){
			printf("-1\n");
		}else{
			printf("%d\n", ans); 
		}
	}
	return 0;
}

 

普通AC:

#include<iostream>
#include<cstring>
#include<string.h>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
const int maxn = 15;
const int INF = (1 << 31) - 1;
char maze[maxn][maxn];
bool vis[maxn][maxn]; 
int dist[maxn][maxn];
int n, m;
int X[4] = {1, -1, 0, 0};
int Y[4] = {0, 0, 1, -1};

struct node{
	int x, y;
	int step;
	node(){
		step = 0;
	} 
};

bool check(node now){
	if(now.x < 1 || now.x > n || now.y < 1 || now.y > m || maze[now.x][now.y] == '.'){
		return 0;
	}
	return 1;
}
int BFS(node s1, node s2){
	memset(vis, 0, sizeof(vis));
	memset(dist, 0, sizeof(dist));
	vis[s1.x][s1.y] = 1;
	vis[s2.x][s2.y] = 1;
	dist[s1.x][s2.y] = 0;
	dist[s1.x][s2.y] = 0;
	queue<node> q;
	q.push(s1);
	q.push(s2);
	node front, next;
	int ans = 0;	
	while(!q.empty()){
		front = q.front();
		q.pop();
		for(int i = 0; i < 4; i ++){
			next.x = front.x + X[i];
			next.y = front.y + Y[i];
			if(check(next) && vis[next.x][next.y] == 0){
				dist[next.x][next.y] = dist[front.x][front.y] + 1; 
				vis[next.x][next.y] = 1;
				q.push(next);
			}
		}
	}
	bool flag = 0;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			if(maze[i][j] == '#' && vis[i][j] == 0){
				flag = 1;
			}
			ans = max(dist[i][j], ans);
		}
	}
	if(flag)
		return -1;
	else
		return ans;
}
int main(){
	int T;
	cin >> T;
	int q = 1;
	while(T--){
		cin >> n >> m;
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= m;j ++){
				cin >> maze[i][j];
			}
		} 
		int ans = INF;
		node fire1, fire2;
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= m; j++){
				if(maze[i][j] == '#'){
					fire1.x = i;
					fire1.y = j;	
					for(int k = 1; k <= n; k++){
						for(int z = 1; z <= m; z++){
				
							if(maze[k][z] == '#'){
								fire2.x = k;
								fire2.y = z;
								int flag = BFS(fire1, fire2);
								if(flag != -1){
									ans = min(ans, flag); 
								}	
							}
						}
					}
				}
			}
		}
		cout << "Case " << q++ << ": ";
		if(ans == INF){
			cout << -1 << endl;
		}else{
			cout << ans << endl;
		}
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值