kuangbin搜索专题 I - Fire Game(BFS)(双起点的bfs)

Fire Game:

题目大意:(文末原题)

开始从两个点同时放火(两点可以是同一个点),给出地图,判断能否使所有的草都被点燃(火能够向上下左右四个方向移动),如果可以输出时间。

思路:

双起点的bfs,运用循环,模拟以不同的起点开始bfs,得出最短路径;

代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
#define inf 0x3f3f3f3f

const int maxn = 222;
	//t, n, m:输入     ans:记录答案	sum:草的数		total:已点燃的草的数 
int t, n, m, ans, sum, total; 
char mmap[maxn][maxn];		//保存每一个位置的状态 
bool vis[maxn][maxn];		//检验是否走过 

int dx[] = {1, 0, -1, 0};	//每次运动 
int dy[] = {0, 1, 0, -1};

vector<pair<int, int> > v;	//保存是草的位置 
		//注意:不能写成 vector<pair<int, int>>; >> 应该写为 > >; 

bool judge(int x, int y) {		//检查 
	if(mmap[x][y] == '#' && x >= 0 && x < n && y >= 0 && y < m && !vis[x][y])
		return true;
	return false;
}

struct Node{
	int x, y, cnt;
				 
	Node(int x = 0, int y = 0, int cnt = 0): x(x), y(y), cnt(cnt) {}
};

void bfs(int s1x, int s1y, int s2x, int s2y) {		//每次传入两个起点 
	queue<Node> q;
	q.push(Node(s1x, s1y, 0));
    
    q.push(Node(s2x, s2y, 0));
	
	vis[s1x][s1y] = vis[s2x][s2y] = 1;
	total = 0;
	
	while(!q.empty()) {
		Node now, temp;
			
		if(!q.empty()) {
			now = q.front();
			q.pop();
			total++;
		}
		
		if(total == sum) {		//如果相等则证明全部都燃烧 
			ans = min(now.cnt, ans);
			return;
		}
		
		for(int i = 0; i < 4; i++) {	//每个影响的四个方向 
			temp.cnt = now.cnt + 1;
			temp.x = now.x + dx[i];
			temp.y = now.y + dy[i];
			
			if(judge(temp.x, temp.y)) {
				q.push(temp);
				vis[temp.x][temp.y] = 1; 
			}
		}
	}
}

int main() {
	scanf("%d", &t);
	
	for(int c = 1; c <= t; c++) {
		sum = 0;
		v.clear();		//清空v 
		scanf("%d%d", &n, &m);
		
		for(int i = 0; i < n; i++) {
			getchar();
			for(int j = 0; j < m; j++) {
				scanf("%c", &mmap[i][j]);
			
				if(mmap[i][j] == '#') {
					sum++;						
					v.push_back(make_pair(i, j));	//只保存是草的位置 
				}
			}
		}
			
		printf("Case %d: ", c);
			
		if(sum < 3) {
			printf("0\n");
			continue;
		}
			
		int len = v.size();
		ans = inf;
			
		for(int i = 0; i < len - 1; i++) {		//二重循环分别从不同的两个起点开始,最后得出最小值 
			for(int j = i + 1; j < len; j++) {
				memset(vis, 0, sizeof(vis));
				bfs(v[i].first, v[i].second, v[j].first, v[j].second);
			}			
		}		
	
		if(ans == inf){
			printf("-1\n");
		}else {
			printf("%d\n", ans);
		}
	}
	
	return 0;
}

原题:

题目:

Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. Firstly they choose two grids which are consisting of grass and set fire. As we all know, the fire can spread among the grass. If the grid (x, y) is firing at time t, the grid which is adjacent to this grid will fire at time t+1 which refers to the grid (x+1, y), (x-1, y), (x, y+1), (x, y-1). This process ends when no new grid get fire. If then all the grid which are consisting of grass is get fired, Fat brother and Maze will stand in the middle of the grid and playing a MORE special (hentai) game. (Maybe it’s the OOXX game which decrypted in the last problem, who knows.)

You can assume that the grass in the board would never burn out and the empty grid would never get fire.

Note that the two grids they choose can be the same.

输入:

The first line of the date is an integer T, which is the number of the text cases.

Then T cases follow, each case contains two integers N and M indicate the size of the board. Then goes N line, each line with M character shows the board. “#” Indicates the grass. You can assume that there is at least one grid which is consisting of grass in the board.

1 <= T <=100, 1 <= n <=10, 1 <= m <=10;
输出:

For each case, output the case number first, if they can play the MORE special (hentai) game (fire all the grass), output the minimal time they need to wait after they set fire, otherwise just output -1. See the sample input and output for more details.
样例:

Input:          Output:

4

3 3

.#.

###

.#.  ---------------- Case 1: 1

3 3

.#.

#.#

.#.  ---------------- Case 2: -1

3 3

...

#.#

... ------------------ Case 3: 0

3 3

###

..#

#.# ----------------- Case 4: 2

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值